Swift – AVAudioPlayer,声音无法正常播放

Swift – AVAudioPlayer,声音无法正常播放,第1张

概述因为在应用程序处于活动状态时没有显示UILocalNotification,所以我正在尝试配置UIAlertController并在它出现时播放一些声音. 我在AppDelegate中没有问题来处理通知/创建警报.我的问题涉及声音.实际上,它无法正常播放. 这是我到目前为止: //...class AppDelegate: UIResponder, UIApplicationDelegate { 因为在应用程序处于活动状态时没有显示UIlocalnotification,所以我正在尝试配置UIAlertController并在它出现时播放一些声音.

我在AppDelegate中没有问题来处理通知/创建警报.我的问题涉及声音.实际上,它无法正常播放.

这是我到目前为止:

//...class AppDelegate: UIResponder,UIApplicationDelegate {var window: UIWindow?func application(application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    // OverrIDe point for customization after application launch.    // Notifications permissions    let types: UIUserNotificationType = UIUserNotificationType.sound | UIUserNotificationType.Alert    let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types,categorIEs: nil)    application.registerUserNotificationSettings(settings)    return true}func application(application: UIApplication!,dIDReceivelocalnotification notification: UIlocalnotification!) {    let state : UIApplicationState = application.applicationState    var au@R_502_6901@Player = AVAu@R_502_6901@Player()    if (state == UIApplicationState.Active) {        // Create sound        var error:NSError?        var au@R_502_6901@Player = AVAu@R_502_6901@Player()        AVAu@R_502_6901@Session.sharedInstance().setcategory(AVAu@R_502_6901@SessioncategoryAmbIEnt,error: nil)        AVAu@R_502_6901@Session.sharedInstance().setActive(true,error: nil)        let soundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound",ofType: "wav")!)        au@R_502_6901@Player = AVAu@R_502_6901@Player(contentsOfURL: soundURL,error: &error)        if (error != nil) {            println("There was an error: \(error)")        } else {            au@R_502_6901@Player.preparetoPlay()            au@R_502_6901@Player.play()        }        // Create alert        let alertController = UIAlertController(Title: "Alert Title",message: "Alert message.",preferredStyle: .Alert)        let noAction = UIAlertAction(Title: "No",style: .Cancel) { (action) in            // ...        }        let yesAction = UIAlertAction(Title: "Yes",style: .Default) { (action) in            // ...        }        alertController.addAction(noAction)        alertController.addAction(yesAction)        self.window?.rootVIEwController?.presentVIEwController(alertController,animated: true,completion: nil)    }}

有了这个,玩家通过这一行:au@R_502_6901@Player.play()
它的播放时间不到一秒钟.就像它突然被解除分配一样(?).

我试过以下两件事:

>在创建警报之前(或在显示警报之后)将AVAu@R_502_6901@Player状态切换回非活动状态:AVAu@R_502_6901@Session.sharedInstance().setActive(false,error:nil).如果我这样做,声音播放正确.但是,这种方法是一种同步(阻塞) *** 作,因此它会延迟其他事情(声音在声音后显示).显然不是一个好的解决方案.
>将au@R_502_6901@Player属性(var au@R_502_6901@Player = AVAu@R_502_6901@Player())移动到类级别,就在窗口下(var window:UIWindow?).如果我这样做,声音播放正确,警报也正确显示.

我不明白为什么它会这样.我错过了什么吗?这是解决我问题的正确方法吗?

提前感谢所有能帮助我理解/解决这个问题的人.

你已经为你的问题提供了正确的解决方案,那就是#2;拥有一个类级别的au@R_502_6901@Player属性.为什么会这样?
嗯,那是因为在代码中你设置了播放器,开始播放,显示d出窗口,一切都完成后,该方法退出其范围.自动内存管理(ARC)可以在退出该变量的范围时释放任何局部变量.如果您认为声音播放是异步的(例如,它将在不阻塞主线程的情况下播放),则在音频播放器播放完声音之前退出示波器. ARC注意到au@R_502_6901@Player是一个局部变量并在那时释放它,但声音尚未播放.

我希望我足够清楚,如果不是随意多问!

总结

以上是内存溢出为你收集整理的Swift – AVAudioPlayer,声音无法正常播放全部内容,希望文章能够帮你解决Swift – AVAudioPlayer,声音无法正常播放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1051132.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存