如何在音频播放器的锁定屏幕上使用按钮?

时间:2016-12-28 18:13:38

标签: ios objective-c avaudioplayer

我使用此代码显示有关曲目和使用播放/暂停按钮的信息。

-(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;
    }
}
}

我想在Apple Music应用程序中使用按钮进行倒带(如下图所示)。

enter image description here

如果我按下按钮audioPlayer移动到下一曲目。如果我长按按钮轨道是倒带。怎么做?

1 个答案:

答案 0 :(得分:1)

添加这2行  " - (void)viewDidLoad"

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