加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – AVAudioSession音频调光错误:停用已运行I / O的音频会

发布时间:2020-12-14 17:24:04 所属栏目:百科 来源:网络整理
导读:我正在播放几种声音,每种声音都会使背景音频变暗.完成后,我恢复背景音频.每当其中一个音频文件播放时,背景会变暗(根据需要).当最后一个音频完成播放时,恢复背景音频(也是期望的).然而,大约5秒后,它会抛出此错误并再次使音频变暗(不是我想要的,因为所有声音现
我正在播放几种声音,每种声音都会使背景音频变暗.完成后,我恢复背景音频.每当其中一个音频文件播放时,背景会变暗(根据需要).当最后一个音频完成播放时,恢复背景音频(也是期望的).然而,大约5秒后,它会抛出此错误并再次使音频变暗(不是我想要的,因为所有声音现在都已完成).

ERROR: [0x19c9af310] AVAudioSession.mm:646: -[AVAudioSession
setActive:withOptions:error:]: Deactivating an audio session that has
running I/O. All I/O should be stopped or paused prior to deactivating
the audio session.

据我所知,我正在停止并删除所有音频.

我在这里找到1篇帖子:

iOS8 AVAudioSession setActive error

但解决方案对我不起作用.这是我的音频播放器课程.如果你可以告诉我什么可能,我会很感激.

import Foundation
import AVFoundation

private var _singleton:O_Audio? = O_Audio()
private var _avAudioPlayers:Array<AVAudioPlayer> = []

//Manages dimming and resuming background audio
class O_Audio:NSObject,AVAudioPlayerDelegate
{
    class var SINGLETON:O_Audio
    {
        if(_singleton == nil)
        {
            _singleton = O_Audio()
        }
        return _singleton!
    }

    class func dimBackgroundAudio()
    {
        AVAudioSession.sharedInstance().setActive(true,error: nil)
    }

    class func restoreBackgroundAudio()
    {
        AVAudioSession.sharedInstance().setActive(false,error: nil)
    }

    class func playSound(path:String)
    {
        var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(path,ofType: "m4a")!)
        var audioPlayer = AVAudioPlayer(contentsOfURL: sound,error: nil)
        _avAudioPlayers.append(audioPlayer)
        audioPlayer.delegate = O_Audio.SINGLETON
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }

    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!,successfully flag: Bool)
    {
        //this was from the 1 stack post I found but these
        //two lines do not solve my problem
        player.stop()
        player.prepareToPlay()

        var index:Int!
        for i in 0..._avAudioPlayers.count - 1
        {
            if(_avAudioPlayers[i] == player)
            {
                index = i
                break
            }
        }

        _avAudioPlayers.removeAtIndex(index)

        if(_avAudioPlayers.count == 0)
        {
            O_Audio.restoreBackgroundAudio()
        }
    }

    func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!,error: NSError!)
    {
        println("error")
    }
}

重要更新

所以我发现了我认为是问题的粗略原因.我们的应用程序基于Cordova构建.所以我们有很多Safari(浏览器)调用.每当我们播放视频(通过Safari播放)时,就会发生此错误.似乎Safari以某种方式调暗音频并保持运行的I / O线程.

解决方法

问题是MPMoviePlayerController对象正在播放.事实上,任何AVPlayerItem都会导致这种情况.如果您播放电影,并尝试调暗音频,您将收到此错误.

目前,在iOS上播放的任何电影都有一个不可变的音轨(即使电影文件中没有音频).这永远导致鸭子问题(它是源代码中的一个错误).我尝试了很多解决方法,没有任何效果.我确定这是一个Xcode源错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读