Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-单击按钮,音乐从应用程序内传送到系统_Ios_Swift_Mpmusicplayercontroller - Fatal编程技术网

Ios Swift-单击按钮,音乐从应用程序内传送到系统

Ios Swift-单击按钮,音乐从应用程序内传送到系统,ios,swift,mpmusicplayercontroller,Ios,Swift,Mpmusicplayercontroller,目前,我在我的应用程序中进行了检查,查看iTunes是否正在播放,如果正在播放,请继续播放iTunes音乐,而不要播放游戏中的音乐。如果iTunes没有播放任何内容,则可以播放游戏中的音乐 我正在尝试将其合并到两个功能中,并有一个按钮可在以下两个功能之间切换: 在游戏音乐中播放iTunes 玩游戏音乐而不是iTunes 这当前在我的viewDidLoad()中 isplay()函数 我尝试将前面的代码合并到一个由按钮触发的函数中: func playInGameMusic() { l

目前,我在我的应用程序中进行了检查,查看iTunes是否正在播放,如果正在播放,请继续播放iTunes音乐,而不要播放游戏中的音乐。如果iTunes没有播放任何内容,则可以播放游戏中的音乐

我正在尝试将其合并到两个功能中,并有一个按钮可在以下两个功能之间切换:

  • 在游戏音乐中播放iTunes
  • 玩游戏音乐而不是iTunes
这当前在我的viewDidLoad()中

isplay()函数

我尝试将前面的代码合并到一个由按钮触发的函数中:

func playInGameMusic() {
    let path = NSBundle.mainBundle().pathForResource("music.mp3", ofType: nil)!
    let url = NSURL(fileURLWithPath: path)
    do {
        let sound = try AVAudioPlayer(contentsOfURL: url)
        music = sound
        sound.play()
        ingame = true
    } catch { }
}
但是,我不知道如何使用playSystemMusic()函数。有没有办法从我的应用程序中恢复iTunes?我知道他们是一个MPMusicLayerController类,但我不确定我应该使用什么来让iTunes从我的应用程序中的一个按钮播放

谢谢您的指导

func isPlaying() -> Bool {
    let musicPlayer = MPMusicPlayerController.systemMusicPlayer()
    if musicPlayer.playbackState == MPMusicPlaybackState.Playing {
        ingame = false;    
        return true
    }
    return false
}
func playInGameMusic() {
    let path = NSBundle.mainBundle().pathForResource("music.mp3", ofType: nil)!
    let url = NSURL(fileURLWithPath: path)
    do {
        let sound = try AVAudioPlayer(contentsOfURL: url)
        music = sound
        sound.play()
        ingame = true
    } catch { }
}