【问题标题】:How to enable MPMediaItemArtwork Next / Previous button?如何启用 MPMediaItemArtwork 下一个/上一个按钮?
【发布时间】:2018-08-20 06:52:28
【问题描述】:

我在我的项目中使用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 From App Capability:

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

那么我的问题是为什么不第一次启用 Next/Previous 按钮?

提前致谢。

【问题讨论】:

标签: ios objective-c audio avaudioplayer


【解决方案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];

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

【讨论】:

  • 感谢您做出这个回答。但我已经完成了这个问题。使用相同的代码
【解决方案2】:

这是我在 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;
    }];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2013-02-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多