Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show seek track duration on control center

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 hereenter image description here

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

enter image description hereenter 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];
}
like image 712
Desmond Avatar asked Jan 10 '14 09:01

Desmond


1 Answers

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.

like image 126
Samir Avatar answered Sep 21 '22 16:09

Samir