how to show seek track duration on control center

*爱你&永不变心* 提交于 2019-11-29 23:21:40

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.

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];


    }
}

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..

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