AVAudioSession重新激活问题

编程入门 行业动态 更新时间:2024-10-27 18:33:05
本文介绍了AVAudioSession重新激活问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到以下错误:

[0x19bf89310] AVAudioSession.mm:646:-[AVAudioSession setActive:withOptions:error:]:停用正在运行I/O的音频会话.在停用音频会话之前,应停止或暂停所有I/O.

[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.

我已经阅读了这篇文章:

I have read through this post:

  • iOS8 AVAudioSession setActive错误

但是,那里的解决方案无法解决我的问题.这是:

however the solution there did not solve my problem. which is:

  • 小型应用程序(游戏)
  • 在AudioHelper.swift中处理的音频背景音乐
  • 使用SKActino.playSoundFile
  • 播放的其他"blip"声音
  • small application (game)
  • audio background music handled in AudioHelper.swift
  • additional 'blip' sounds played with SKActino.playSoundFile

复制

  • 开始游戏(背景音乐开始播放)
  • 关闭游戏(音乐停止)
  • 重新开始游戏(音乐再次开始)
  • 出现上面的错误
  • SKAction.playSoundFile不再总是有效(有些可以,有些不能)

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

my audio handling code (aka 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

I am initializing & pausing & starting through the 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(false0)移至委托方法.新的帮助程序类如下所示:

i had to move the setActive(false0) to the delegate method. new helper class looks like this:

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) } }

现在可以使用

更多推荐

AVAudioSession重新激活问题

本文发布于:2023-10-26 09:18:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529738.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AVAudioSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!