how to show seek track duration on control center

前端 未结 4 1810
无人及你
无人及你 2020-12-24 09:07

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.

Playin

相关标签:
4条回答
  • 2020-12-24 09:46

    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.

    0 讨论(0)
  • 2020-12-24 09:47

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

    0 讨论(0)
  • 2020-12-24 10:00

    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";
        }
    }
    
    0 讨论(0)
  • 2020-12-24 10:02

    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];
    
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题