Jay Bhalani
Jay Bhalani

Reputation: 4171

How to enable MPMediaItemArtwork Next / Previous button?

I am using AVAudioPlayer in my project. When application go in background or lock the device application play song using AVAudioPlayer.

My problem is that when i am download 2 song and play song using AVAudioPlayer and lock the device, song will be played but next and previous button is disabled.

I try enable button using bellow code :

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

And Also set Bellow code :

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

And set MPMediaItemArtwork detail Like This :

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

I also enable BackgroundMode From App Capability :

enter image description here

enter image description here

Note : When i kill the app and run again, and play song and lock the device, next and previous button enable and work, but not work in first time.

Then my question is why not enable Next/Previous button for first time ?

Thank in Advance.

Upvotes: 1

Views: 661

Answers (3)

Vishnu
Vishnu

Reputation: 408

This is a working solution, Latest Swift ios code

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

Upvotes: 1

xuzepei
xuzepei

Reputation: 1209

Here is my way in 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;
    }];
}

Upvotes: 0

Vidhyanand
Vidhyanand

Reputation: 5369

Can you target for previous and next track objects as below

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

//OR

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

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

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Your code looks fine...!

Upvotes: 1

Related Questions