gpt4 book ai didi

ios - AVAudioSession 重新激活问题

转载 作者:可可西里 更新时间:2023-11-01 00:53:36 25 4
gpt4 key购买 nike

我收到以下错误:

[0x19bf89310] 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.

我已经阅读了这篇文章:

但是那里的解决方案并没有解决我的问题。这是:

  • 小应用(游戏)
  • 在 AudioHelper.swift 中处理音频背景音乐
  • 使用 SKActino.playSoundFile 播放的附加“blip”声音

复制

  • 开始游戏(背景音乐开始播放)
  • 结束游戏(音乐停止)
  • 重新开始游戏(音乐再次开始)
  • 出现上述错误
  • SKAction.playSoundFile 不再总是有效(有些有效,有些无效)

我的音频处理代码(又名 AudioHelper.swift)

class AudioHelper: NSObject, AVAudioPlayerDelegate {
var player : AVAudioPlayer?

class var defaultHelper : AudioHelper {
struct Static {
static let instance : AudioHelper = AudioHelper()
}

return Static.instance
}

override init() {
super.init()
}

func initializeAudio() {
var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_float", ofType: ".mp3")!)
self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
self.player?.numberOfLoops = -1
self.player?.play()
}

func stopAudio() {
self.player?.stop()
self.player?.prepareToPlay()
AVAudioSession.sharedInstance().setActive(false, error: nil)
}

func startAudio() {
AVAudioSession.sharedInstance().setActive(true, error: nil)
self.player?.play()
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
self.stopAudio()
}
}

我正在通过 AppDelegate

初始化、暂停和启动
class AppDelegate: UIResponder, UIApplicationDelegate {


var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
AudioHelper.defaultHelper.initializeAudio()
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
AudioHelper.defaultHelper.stopAudio()
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
AudioHelper.defaultHelper.startAudio()
}
}

我哪里错了?

最佳答案

我必须将 setActive(false) 移至委托(delegate)方法。新的助手类如下所示:

import Foundation
import AVFoundation

class AudioHelper: NSObject, AVAudioPlayerDelegate {
var player : AVAudioPlayer?


class var defaultHelper : AudioHelper {
struct Static {
static let instance : AudioHelper = AudioHelper()
}

return Static.instance
}

override init() {
super.init()
}

func initializeAudio() {
var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_float", ofType: ".mp3")!)
self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
self.player?.numberOfLoops = -1
self.player?.play()

}

func stopAudio() {
self.player?.stop()
self.player?.prepareToPlay()
}

func startAudio() {
AVAudioSession.sharedInstance().setActive(true, error: nil)
self.player?.play()
}


func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
AVAudioSession.sharedInstance().setActive(false, error: nil)
}
}

现在工作

关于ios - AVAudioSession 重新激活问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com