如何启用MPMediaItemArtwork的“下一个/上一个”按钮?

时间:2018-08-20 06:52:29

标签: ios objective-c audio avaudioplayer

我在项目中使用AVAudioPlayer。 当应用程序进入后台或锁定设备应用程序时,使用AVAudioPlayer播放歌曲。

我的问题是,当我下载2首歌曲并使用AVAudioPlayer播放歌曲并锁定设备时,会播放歌曲,但是下一个和上一个按钮被禁用。

我尝试使用以下代码启用按钮:

[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;

还要设置贝娄代码:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

并像这样设置MPMediaItemArtwork详细信息:

MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
[songInfo setObject:@"song title" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

我还启用了BackgroundMode来自应用程序功能:

enter image description here

enter image description here

注意:当我终止该应用程序并再次运行,播放歌曲并锁定设备时,下一个和上一个按钮将启用并起作用,但第一次不会起作用。

然后我的问题是为什么不第一次启用“下一个/上一个”按钮?

预先感谢。

2 个答案:

答案 0 :(得分:1)

您可以按以下方式定位上一个和下一个轨道对象

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

commandCenter.previousTrackCommand.enabled = YES;
commandCenter.nextTrackCommand.enabled = YES;

[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    NSLog(@"Next Track");
    return MPRemoteCommandHandlerStatusSuccess;
}];

[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    NSLog(@"Previous Track");
    return MPRemoteCommandHandlerStatusSuccess;
}];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

//或

[commandCenter.nextTrackCommand addTarget:self action:@selector(play)];

[commandCenter.previousTrackCommand addTarget:self action:@selector(play)];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

您的代码看起来不错...!

答案 1 :(得分:0)

这是我在 Objective-C 中的方法:

if(@available(iOS 11, *)) {
    MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];

    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    
    [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    
    [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
}