Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
MPMusicLayerController.play()在杀死iOS音乐应用程序后不工作_Ios_Swift_Mpmusicplayercontroller - Fatal编程技术网

MPMusicLayerController.play()在杀死iOS音乐应用程序后不工作

MPMusicLayerController.play()在杀死iOS音乐应用程序后不工作,ios,swift,mpmusicplayercontroller,Ios,Swift,Mpmusicplayercontroller,我有一个应用程序,可以让你在音乐库中搜索歌曲的标题并播放它。我使用以下代码播放所选歌曲: func playSongByPersistentID(id: Int) { //id is the persistent id of chosen song let predicate = MPMediaPropertyPredicate(value: id, forProperty: MPMediaItemPropertyPersistentID) let songQuery = MPM

我有一个应用程序,可以让你在音乐库中搜索歌曲的标题并播放它。我使用以下代码播放所选歌曲:

func playSongByPersistentID(id: Int) { //id is the persistent id of chosen song
    let predicate = MPMediaPropertyPredicate(value: id, forProperty: MPMediaItemPropertyPersistentID)
    let songQuery = MPMediaQuery()
    songQuery.addFilterPredicate(predicate)

    if songQuery.items?.count < 1 {
        print("Could Not find song") // make alert for this
        return
    } else {
        print("gonna play \(songQuery.items?[0].title)")
    }

    musicPlayer.prepareToPlay()
    musicPlayer.setQueueWithItemCollection(songQuery.collections![0])
    musicPlayer.play()
}
func playSongByPersistentID(id:Int){//id是所选歌曲的持久id
let predicate=MPMediaPropertyPredicate(值:id,forProperty:MPMediaItemPropertyTypersTestId)
让songQuery=MPMediaQuery()
songQuery.addFilterPredicate(谓词)
如果songQuery.items?计数<1{
打印(“找不到歌曲”)//为此发出警报
返回
}否则{
打印(“将播放\(songQuery.items?[0].title)”)
}
MusicLayer.prepareToPlay()
musicPlayer.setQueueWithItemCollection(songQuery.collections![0])
musicPlayer.play()
}
表格视图(:didselectRowatineXpath)
中调用上述函数。我已确认在选择歌曲时检索到正确的ID和歌曲标题

这是我的问题。如果我在关闭iOS音乐应用程序后进入应用程序并选择要播放的歌曲,则该歌曲不会播放。如果我选择了一首不同的歌曲,那么这首不同的歌曲播放没有问题。如果我一遍又一遍地选择同一首歌,它就永远不会播放

musicPlayer
是在我的类中声明的
systemMusicPlayer


这是一个iOS错误吗?我不知道发生了什么。

这是我找到的解决办法

当我开始播放音乐时,它会设置一个计时器。该计时器调用一个函数来测试音乐是否正在播放。如果正在播放音乐,则计时器无效。如果没有,它将对我要播放的项目(本例中的项目集合)重新排队,并尝试再次播放

我已经从我的应用程序中提取了这段代码并对其进行了抽象,因此它可能无法按原样编译,但希望它能理解这一点。我将把这个bug提交给Apple(在创建了一个小样本项目之后),并建议您也这样做

func playMusic()
{    
    musicPlayer = MPMusicPlayerController.applicationMusicPlayer()

    musicPlayer.setQueueWithItemCollection(MPMediaItemCollection(items: songsForPlayingWithMusicPlayer))

    musicPlayer.play()

    testForMusicPlayingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(1), target: self, selector: "testForMusicPlaying", userInfo: nil, repeats: false)
}

func testForMusicPlaying()
{
    if musicPlayer.playbackState != .Playing
    {
        testForMusicPlayingTimer.invalidate()

        musicPlayer = MPMusicPlayerController.applicationMusicPlayer()

        musicPlayer.setQueueWithItemCollection(MPMediaItemCollection(items: songsForPlayingWithMusicPlayer))

        musicPlayer.play()

        testForMusicPlayingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(1), target: self, selector: "testForMusicPlaying", userInfo: nil, repeats: false)
    }
    else
    {
        testForMusicPlayingTimer.invalidate()     
    }
}

我认为这是一个错误。我正在我的应用程序中尝试类似的东西——如果我遇到任何解决方法,我会告诉你的。谢谢。这很烦人。当你归档你的雷达时,你能把它添加到openradar中吗,我会复制它。