ios – 在使用Swift时,我的应用仍然播放声音时,如何允许背景音乐继续播放

ios – 在使用Swift时,我的应用仍然播放声音时,如何允许背景音乐继续播放,第1张

概述我创建了一个应用程序,我试图允许用户在玩我的游戏时继续听他们的音乐,但是当他们打“播放”,并且发出声音时会停止背景音乐.我正在使用Swift在iOS上开发.这是一段启动游戏声音的代码. func playSpawnedDot() { var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForRes 我创建了一个应用程序,我试图允许用户在玩我的游戏时继续听他们的音乐,但是当他们打“播放”,并且发出声音时会停止背景音乐.我正在使用Swift在iOS上开发.这是一段启动游戏声音的代码.

func playSpawnedDot() {    var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("spawnDot",ofType: "mp3")!)!    var error:NSError?    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound,error: &error)    audioPlayer.preparetoPlay()    if volumeBool {        audioPlayer.play()    }}
解决方法 您需要设置AVAudioSession类别,具有以下值之一: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html(AVAudioSession类参考).

默认值设置为AVAudioSessioncategorySoloAmbIEnt.如你所见:

[…] using this category implIEs that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing,use the AVAudioSessioncategoryAmbIEnt category instead.

在播放声音之前,您必须更改该类别.这样做:

AVAudioSession.sharedInstance().setcategory(AVAudioSessioncategoryAmbIEnt,error: nil)AVAudioSession.sharedInstance().setActive(true,error: nil)

每次播放声音时都不需要调用这些行.你可能只想做一次.

总结

以上是内存溢出为你收集整理的ios – 在使用Swift时,我的应用仍然播放声音时,如何允许背景音乐继续播放全部内容,希望文章能够帮你解决ios – 在使用Swift时,我的应用仍然播放声音时,如何允许背景音乐继续播放所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1064038.html

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

发表评论

登录后才能评论

评论列表(0条)

保存