ios 如何启用MPMediaItemArtwork下一个/上一个按钮?

n3schb8v  于 2023-02-10  发布在  iOS
关注(0)|答案(3)|浏览(142)

我正在使用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

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

那么我的问题是为什么不启用下一个/上一个按钮的第一次?
提前致谢。

20jt8wwn

20jt8wwn1#

你能像下面那样瞄准上一个和下一个目标吗

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];

您的代码看起来很好...!

c0vxltue

c0vxltue2#

下面是我在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;
    }];
}
j91ykkif

j91ykkif3#

这是一个可行的解决方案,最新的Swift ios代码

UIApplication.shared.beginReceivingRemoteControlEvents()
        MPRemoteCommandCenter.shared()
        MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
        MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
        MPRemoteCommandCenter.shared().playCommand.addTarget {event in
            
         //   self.playerPlay()
              return .success
            }
            MPRemoteCommandCenter.shared().pauseCommand.addTarget {event in
                
          //      self.playerPause()
                
              return .success
            }
            MPRemoteCommandCenter.shared().nextTrackCommand.addTarget {event in
              return .success
            }
            MPRemoteCommandCenter.shared().previousTrackCommand.addTarget {event in
              return .success
            }
        
        var nowPlayingInfo = [String: Any]()
// prepare title
        nowPlayingInfo[MPMediaItemPropertyTitle] = currentTrackTitle ?? "Buffering..."
// prepare Live stream
        nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = true
        
// To set a Image
        let img = UIImage(named: "place_holder")
        let artwork = MPMediaItemArtwork.init(boundsSize: img!.size, requestHandler: { (size) -> UIImage in
            return img!
            })
        nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

相关问题