Question: How to use buttons on lock screen with audio player?

Question

How to use buttons on lock screen with audio player?

Answers 1
Added at 2016-12-28 19:12
Tags
Question

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?

Answers
nr: #1 dodano: 2016-12-29 06:12

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

 [[AVAudioSession sharedInstance] setDelegate: self];
 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Source Show
◀ Wstecz