AVAudioPlayer on Lock Screen

跟風遠走 提交于 2019-11-29 03:28:14

问题


I've implemented an audio player using AVAudioPlayer (not AVPlayer). I'm able to handle the remote control events with the following method. It works quite alright so far, however I see two more subtypes for these events: UIEventSubtypeRemoteControlEndSeekingForward and UIEventSubtypeRemoteControlEndSeekingBackward.

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl)
    {
        if (event.subtype == UIEventSubtypeRemoteControlPlay)
        {
            [self playAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlPause)
        {
            [self pauseAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
        {
            [self togglePlayPause];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
        {
            [self rewindTheAudio]; //this method rewinds the audio by 15 seconds.
        }
        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
        {
            [self fastForwardTheAudio]; //this method fast-forwards the audio by 15 seconds.
        }

}

So the questions:

  1. In order to have things work right, am I supposed to implement those two subtypes, too?

  2. This method only enables the rewind, play/pause, and fast forward buttons on lock screen, but it doesn't display the file title, artwork, and duration. How can I display that info using AVAudioPlayer or AVAudioSession (I don't really want one more library/API to implement this)?

    2-a. I discovered MPNowPlayingInfoCenter while searching and I don't know much about it. Do I have to use it to implement those stuff above? :-[


回答1:


You are correct, MPNowPlayingInfoCenter is the only way to do this. So go ahead and link with MediaPlayer.framework. In the class that handles playing tracks, import <MediaPlayer/MediaPlayer.h>. Whenever your track changes, do this:

NSDictionary *info = @{ MPMediaItemPropertyArtist: artistName,
                            MPMediaItemPropertyAlbumTitle: albumName,
                            MPMediaItemPropertyTitle: songName };

    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;


来源:https://stackoverflow.com/questions/20363058/avaudioplayer-on-lock-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!