ios - 来自视频 : Default to speaker 的 Swift 音频
<p><p>我正在尝试在 iPhone 上播放视频,如果视频中的音频通过 iPhone 扬声器输出,那将是理想的选择。</p>
<p>但是,我在控制台上收到此错误:</p>
<blockquote>
<p>audioSession error: The operation couldn’t be completed. (OSStatus error -50.)</p>
</blockquote>
<p>这是我的代码::</p>
<pre><code> let player = AVPlayer(url: video_url)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
// get the lenght of the video
self.video_duration = getMediaDuration(url: video_url) + 0.25
// notification for when video is over.
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinish), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
// the video controller...
let controller = AVPlayerViewController()
controller.view.frame = self.view.frame
controller.player = player
controller.showsPlaybackControls = false
controller.view.tag = 99
self.video_ctrl = controller
// add controller as subview
self.view.addSubview(controller.view)
controller.player?.play()
</code></pre>
<p>我用谷歌搜索了错误,但没有看到任何结果。
任何帮助表示赞赏。</p>
<p>稍后在同一个应用程序中,我使用上述 <strong>AVAudioSession</strong> 的相同行播放音频文件,效果很好。之后,如果我重播视频,声音会像我想要的那样从扬声器上发出。但为什么不是第一次发生呢?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需使用选项<code>.defaultToSpeaker</code>:</p>
<pre><code>do {
try AVAudioSession.sharedInstance().setActive(false)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .defaultToSpeaker)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print("AVAudioSession error: \(error.localizedDescription)")
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 来自视频 : Default to speaker 的 Swift 音频,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43399737/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43399737/
</a>
</p>
页:
[1]