how to show seek track duration on control center

Desmond picture Desmond · Jan 10, 2014 · Viewed 9.2k times · Source

How do i pass the song info such as song name and track duration to control center. Player plays music fine and the play and pause control works fine though.

Playing with apple's music app

enter image description here enter image description here

Playing with my app with the code below, how to pass song information in order to display it ?

enter image description here enter image description here

//AppDelegate
    -(void)setupAudio
    {
        // Set AVAudioSession
        NSError *sessionError = nil;
        [[AVAudioSession sharedInstance] setDelegate:self];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];

        // Change the default output audio route
        UInt32 doChangeDefaultRoute = 1;
        AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                                sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

            [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
            [self becomeFirstResponder];
    }

    //Make sure we can recieve remote control events
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event
    {
        //if it is a remote control event handle it correctly
        if (event.type == UIEventTypeRemoteControl) {
            if (event.subtype == UIEventSubtypeRemoteControlPlay)
            {
                NSLog(@"UIEventSubtypeRemoteControlPlay");
                [[AppMusicPlayer sharedService]playAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlPause)
            {
                NSLog(@"UIEventSubtypeRemoteControlPause");
                [[AppMusicPlayer sharedService]pauseAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
            {
                NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
            }
        }
    }

    AppMusicPlayer.m
    + (id) sharedService
    {
        static dispatch_once_t _singletonPredicate;
        static AppMusicPlayer *_sharedObject = nil;

        dispatch_once(&_singletonPredicate, ^{
            _sharedObject = [[AppMusicPlayer alloc]init];
        });

        return _sharedObject;
    }

    - (id)init
    {
        self = [super init];

        if (self) {
            // Work your initialising here as you normally would
        }

        return self;
    }

    - (void)playAudio
    {
        [self.audioPlayer play];
    }

    - (void)pauseAudio
    {
        NSLog(@"pause");
        [self.audioPlayer pause];
    }

    - (void)playAudioFromURL:(NSURL *)songURL
    {
        [self.audioPlayer stop];

        //Declare the audio file location and settup the player
        NSError *error;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:&error];
        [self.audioPlayer setNumberOfLoops:-1];

        if (error)
        {
            NSLog(@"%@", [error localizedDescription]);
               }
        else
        {
            //Load the audio into memory
            [self.audioPlayer prepareToPlay];
            [self.audioPlayer play];
        }
    }

-(void)setUpRemoteControl
{
    NSDictionary *nowPlaying = @{MPMediaItemPropertyArtist: songItem.artistName,
                                 MPMediaItemPropertyAlbumTitle: songItem.albumTitle,
                                 MPMediaItemPropertyPlaybackDuration:songItem.playbackDuration,
                                 MPNowPlayingInfoPropertyPlaybackRate:@1.0f,
                                 MPMediaItemPropertyArtwork:[songMedia valueForProperty:MPMediaItemPropertyArtwork]

                                 };

    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying];
}

Answer

Samir picture Samir · Feb 6, 2014

Are you playing songs from iPods music library ? If yes than You should you MPMusicPlayerViewController, it provides in-build functionality for seek bar properties as well as Info center.

I can see here in your code you have used AVAudioPlayer, We can set only below details using AVAudioPlayer class, but can't access seekbar change event in our app.

NSMutableDictionary *albumInfo = [[NSMutableDictionary alloc] init];
UIImage *artWork = [UIImage imageNamed:album.imageUrl];
[albumInfo setObject:album.title forKey:MPMediaItemPropertyTitle];
[albumInfo setObject:album.auther forKey:MPMediaItemPropertyArtist];
[albumInfo setObject:album.title forKey:MPMediaItemPropertyAlbumTitle];
[albumInfo setObject:artworkP forKey:MPMediaItemPropertyArtwork];
[albumInfo setObject:album.playrDur forKey:MPMediaItemPropertyPlaybackDuration]
[albumInfo setObject:album.elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:albumInfo]

Here we need to count album.playrDur and album.elapsedTime as per required format.