how to show seek track duration on control center
Asked Answered
T

4

10

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];
}
Trueblue answered 10/1, 2014 at 9:55 Comment(2)
edit with screenshotsTrueblue
Check this awesome answer: #18801389Ananna
T
6

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.

Topple answered 6/2, 2014 at 12:54 Comment(2)
when i call the advanceToNextItem then audio detail doesn't show on lock screen.Endsley
@ABJ Didn't get you, what is advanceToNextItem? Do you mean ">>" next song button? If it is, then it will call a delegate method from there you again need to update next song information like above.Topple
S
2

You are looking for this:

- (IBAction)playButtonTouched:(id)sender {

   Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

    if (playingInfoCenter) {


        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];


        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];

        [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


    }
}
Scald answered 2/10, 2014 at 9:32 Comment(0)
D
1

I happen to be working on the same thing today and don't know the answer, but in my app the Control Center does show the app name. I want to be able to show the title of the thing I'm playing. I even have a picture I'd like to show, like an album cover. I don't think I'm doing anything different than you, I didn't code anything to send it the app name, for example.

Wait a second... I see there's something called MPNowPlayingInfoCenter that we should look into..

Datura answered 10/1, 2014 at 17:45 Comment(1)
Hi lepton, i had updated my question with what i want and found that i could't seek the track.....Trueblue
R
0

Not sure but may be this code help you little bit..

   - (void) handle_NowPlayingItemChanged: (id) notification
{
    MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
    NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
    if (titleString)
    {
        titleLabel.text = [NSString stringWithFormat:@"Title: %@",titleString];
    } 
    else 
    {
        titleLabel.text = @"Title: Unknown title";
    }
}
Rizal answered 10/1, 2014 at 11:7 Comment(1)
Hi @iMove, thanks for the reply, but this is not what i'm looking for. Had updated my question with screenshotsTrueblue

© 2022 - 2024 — McMap. All rights reserved.