我想让玩家在我的游戏中使用他们的音乐,所以我想知道如何从他的音乐库中检测玩家是否在播放音乐,这样我就可以让他的音乐继续播放,如果不让我的音乐在后台播放的话。
我使用这段代码在我的GameViewController
中播放音乐
let bgMusicURL: NSURL = NSBundle.mainBundle().URLForResource("Squart-GameMusic", withExtension: "wav")!
do {
Data.GameMusic = try AVAudioPlayer(contentsOfURL: bgMusicURL, fileTypeHint: nil)
} catch {
return print("no music file")
}
Data.GameMusic.numberOfLoops = 1
Data.GameMusic.prepareToPlay()
Data.GameMusic.play()
问题是,当我尝试从音乐图书馆播放音乐时,声音就停止了,它让我的应用程序播放音乐而不是音乐库。
发布于 2015-08-25 16:19:38
检查音乐是否在像这样的背景中播放
if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
print("Music is playing")
} else {
print("Play your music")
}
别忘了进口:
import MediaPlayer
如果您想同时播放声音,那么使用下面的代码来播放声音文件
func playSound {
var soundID: SystemSoundID = 0
let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "myFileName", "mp3", nil)
AudioServicesCreateSystemSoundID(soundURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
https://stackoverflow.com/questions/32208674
复制相似问题