AKAudioPlayer:扬声器没有声音,只有耳机
AKAudioPlayer: No sound in speakers, only with headphones
使用 AudioKit 进行声音管理时,我注意到这段非常简单的代码存在问题(错误?)。
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)一切正常。但是我听不到扬声器 中的音频 (只有一种风声)。一旦我插入耳机并再次点击按钮,我通常会听到音频。然后,我拔下耳机,点击按钮,我听不到声音(只有风声)。
我在iPhone5S上面对这个
我不在iPad5上面对这个,我不在模拟器上面对这个。我没有其他设备可以尝试重现。
两种设备:iOS 11.1.2 和 AudioKit:v4.0.4
注意:我的扬声器在任何其他应用程序上都能正常工作,我已经检查了音量。未插入耳机时,默认输出设备设置为扬声器。
根据 Configuring an Audio Session 指南,默认情况下,AVAudioSessionCategory 设置为
In iOS, setting the Ring/Silent switch to silent mode silences any audio being played by the app.
由于 iPad 和模拟器没有此开关,它们将始终通过扬声器播放,但对于硬件 iPhone,它们只会在静音模式下通过耳机播放而不通过扬声器播放,除非会话类别已更改。
您可以直接使用 AVAudioSession 更改会话类别:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
catch {
print("failed to set category")
}
或者您可以使用 AudioKit 的 AKSettings 包装器:
AudioKit.start()
do {
try AKSettings.setSession(category: AKSettings.SessionCategory.playback)
} catch {
print("failed to set category")
}
根据AVSessionCategory docs,播放类别是
The category for playing recorded music or other sounds that are
central to the successful use of your app.
所以它会假定用户希望在使用该应用程序时覆盖静音模式。
使用 AudioKit 进行声音管理时,我注意到这段非常简单的代码存在问题(错误?)。
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)一切正常。但是我听不到扬声器 中的音频 (只有一种风声)。一旦我插入耳机并再次点击按钮,我通常会听到音频。然后,我拔下耳机,点击按钮,我听不到声音(只有风声)。
我在iPhone5S上面对这个
我不在iPad5上面对这个,我不在模拟器上面对这个。我没有其他设备可以尝试重现。
两种设备:iOS 11.1.2 和 AudioKit:v4.0.4
注意:我的扬声器在任何其他应用程序上都能正常工作,我已经检查了音量。未插入耳机时,默认输出设备设置为扬声器。
根据 Configuring an Audio Session 指南,默认情况下,AVAudioSessionCategory 设置为
In iOS, setting the Ring/Silent switch to silent mode silences any audio being played by the app.
由于 iPad 和模拟器没有此开关,它们将始终通过扬声器播放,但对于硬件 iPhone,它们只会在静音模式下通过耳机播放而不通过扬声器播放,除非会话类别已更改。
您可以直接使用 AVAudioSession 更改会话类别:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
catch {
print("failed to set category")
}
或者您可以使用 AudioKit 的 AKSettings 包装器:
AudioKit.start()
do {
try AKSettings.setSession(category: AKSettings.SessionCategory.playback)
} catch {
print("failed to set category")
}
根据AVSessionCategory docs,播放类别是
The category for playing recorded music or other sounds that are central to the successful use of your app.
所以它会假定用户希望在使用该应用程序时覆盖静音模式。