user6203429
user6203429

Reputation:

How to use buttons on lock screen with audio player?

I use this code to show information about track and to using play/pause button.

-(void)info
{
UIImage *image = [UIImage imageNamed:@"image.png"];

NSInteger highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"int"] intValue];

MPMediaItemArtwork *albumArtwork = [[MPMediaItemArtwork alloc] initWithImage:image];

MPNowPlayingInfoCenter* mpic = [MPNowPlayingInfoCenter defaultCenter];
mpic.nowPlayingInfo = @{MPMediaItemPropertyArtist: @"1",
                        MPMediaItemPropertyTitle:[NSString stringWithFormat:@"1 %ld",(long)highScore],
                        MPMediaItemPropertyArtwork:albumArtwork,
                        MPMediaItemPropertyAlbumTitle: @"1",
                        MPNowPlayingInfoPropertyElapsedPlaybackTime:[NSNumber numberWithFloat: [self.audioPlayer getCurrentAudioTime]],
                        MPMediaItemPropertyPlaybackDuration : [NSNumber numberWithFloat:[self.audioPlayer getAudioDuration]]};
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

if (receivedEvent.type == UIEventTypeRemoteControl) {

    switch (receivedEvent.subtype) {

        case UIEventSubtypeRemoteControlPreviousTrack:
            NSLog(@"prev");
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"next");
            break;

        case UIEventSubtypeRemoteControlPlay:
            [_audioPlayer playAudio];
            break;

        case UIEventSubtypeRemoteControlPause:
            [_audioPlayer pauseAudio];
            break;

        default:
            break;
    }
}
}

I want to use buttons for rewind(like below image) like in Apple Music app.

enter image description here

If I tap on button audioPlayer move to the next track. If I long tap on button track is rewind. How to do it?

Upvotes: 3

Views: 201

Answers (1)

kb920
kb920

Reputation: 3089

Add this 2 lines in "- (void)viewDidLoad"

 [[AVAudioSession sharedInstance] setDelegate: self];
 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Upvotes: 1

Related Questions