简体   繁体   English

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

[英]MPMusicPlayerController.play() not working after killing iOS music app

I have an app that allows you to search for the title of a song in the music library and play it.我有一个应用程序,可以让您在音乐库中搜索歌曲的标题并播放它。 I play the selected song using this code:我使用以下代码播放所选歌曲:

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()
}

The above function gets called in tableView(:didSelectRowAtIndexPath) .上面的函数在tableView(:didSelectRowAtIndexPath)被调用。 I have confirmed that the correct ID and song title is retrieved when a song is selected.我已确认在选择歌曲时检索到了正确的 ID 和歌曲名称。

Here is my problem.这是我的问题。 If I go into my app and select a song to play after killing the iOS music app , the song does not play.如果我在关闭 iOS 音乐应用程序后进入我的应用程序并选择要播放的歌曲,则该歌曲不会播放。 If I then choose a different song , that different song plays no problem.如果我然后选择不同的歌曲,那不同的歌曲播放没有问题。 If I choose the same song over and over again it never plays.如果我一遍又一遍地选择同一首歌,它就永远不会播放。

musicPlayer is a systemMusicPlayer declared in my class. musicPlayer是在我的班级中声明的systemMusicPlayer

Is this an iOS bug?这是iOS错误吗? I have no idea what is going on.我不知道发生了什么。

Here is the workaround I found.这是我找到的解决方法。

What it does is set a timer once I try to start playing the music.一旦我尝试开始播放音乐,它的作用就是设置一个计时器。 That timer calls a function that tests if the music is playing.该计时器调用一个函数来测试音乐是否正在播放。 If the music is playing, the timer is invalidated.如果正在播放音乐,则计时器无效。 If not, it re-queues the item I want to play (a collection of items in this example) and tries to play again.如果没有,它会重新排队我想要播放的项目(本示例中的项目集合)并尝试再次播放。

I've pulled this code from my app and abstracted it, so it may not compile as is but hopefully it gets the point across.我已经从我的应用程序中提取了这段代码并对其进行了抽象,因此它可能无法按原样编译,但希望它能够理解这一点。 I'm going to file this bug with Apple (after creating a small sample project) and recommend you do the same.我将向 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()     
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM