AKAudioPlayer:扬声器无声音,仅带耳机 [英] AKAudioPlayer: No sound in speakers, only with headphones

查看:151
本文介绍了AKAudioPlayer:扬声器无声音,仅带耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AudioKit进行声音管理,我注意到这段非常简单的代码存在问题(错误?)。

Using AudioKit for sound management, I noticed an issue (bug?) with this very simple piece of code.

import AudioKit

class MainViewController: UIViewController {

var audioFile: AKAudioFile?
var audioPlayer: AKAudioPlayer?

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func onPlayButtonClick(_ sender: Any) {

    do {
        audioFile = try AKAudioFile(forReading: Bundle.main.url(forResource: "3e", withExtension: "mp3")!)

        audioPlayer = try AKAudioPlayer(file: audioFile!)

        AudioKit.output = audioPlayer
        AudioKit.start()

        audioPlayer!.play()


    } catch {
        print(error)
    }
}
}

使用play()方法启动播放时,日志和进程中的所有内容(播放器持续时间,currentTime)都是正常的。但是我听不到扬声器中的音频(只有一种风声)。插入耳机并再次点击按钮后,我通常会听到音频。然后,我拔下耳机并点击按钮,我听不到声音(只有风声)。

When launching the playback with play() method, everything is normal in logs and in the process (player duration, currentTime). BUT I do not hear the audio in speakers (only a kind of wind noise). As soon as I plug my headphones and tap the button again, I normally hear the audio. Then, I unplug my headphones and tap the button, I don’t hear audio (only that wind noise).

我在iPhone 5S上遇到了这个问题

在iPad5上我面对这个问题,在模拟器上我面对这个问题。我没有其他设备可以尝试重现。

I do not face this on iPad5, I do not face this on simulators. I don’t have the other devices to try to reproduce.

两个设备:iOS 11.1.2和AudioKit:v4.0.4

Both devices: iOS 11.1.2 and AudioKit : v4.0.4

注意:我的扬声器可以与任何其他应用正常工作,并且我已经检查了音量。

NB: my speakers works normally with any other app and I’ve checked the volume. The default outputDevice is set to Speakers when headphones are not plugged.

推荐答案

根据配置音频会话指南,默认情况下,AVAudioSessionCategory设置为

According to the Configuring an Audio Session guide, by default, the AVAudioSessionCategory is set so that


在iOS中,将铃声/静音开关设置为静音模式会使应用程序播放的任何音频静音。

In iOS, setting the Ring/Silent switch to silent mode silences any audio being played by the app.

由于iPad和模拟器没有此开关,因此它们始终可以在扬声器上播放,但是对于硬件iPhone,它们只能在耳机和耳机上播放在静音模式下,不对扬声器讲话,除非更改了会话类别。

Since iPads and the simulator don't have this switch they will always play to the speaker, but in the case of hardware iPhones, they will only play to the headphones and not to the speakers when in silent mode, unless the session category is changed.

您可以直接使用AVAudioSession更改会话类别:

You can change the session category directly with AVAudioSession:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
catch {
    print("failed to set category")
}

,也可以使用AudioKit的AKSettings包装器:

or you can use AudioKit's AKSettings wrapper:

AudioKit.start()
do {
    try AKSettings.setSession(category: AKSettings.SessionCategory.playback)
} catch {
    print("failed to set category")
}

根据 AVSessionCategory文档,播放类别为


该类别

The category for playing recorded music or other sounds that are central to the successful use of your app.

因此,它将假定播放音乐或其他声音对于成功使用您的应用是至关重要的。用户想要使用应用程序时将覆盖静音模式。

so it will assume the user wants the silent mode to be overridden when using the app.

这篇关于AKAudioPlayer:扬声器无声音,仅带耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆