Extend MPMedia implementation to handle 'seek'
This commit is contained in:
+28
-8
@@ -8,32 +8,40 @@
|
||||
void register_os_remote_commands() {
|
||||
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
|
||||
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(PLAY);
|
||||
os_remote_command_callback(PLAY, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(PAUSE);
|
||||
os_remote_command_callback(PAUSE, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(TOGGLE);
|
||||
os_remote_command_callback(TOGGLE, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.stopCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(STOP);
|
||||
os_remote_command_callback(STOP, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(NEXT_TRACK);
|
||||
os_remote_command_callback(NEXT_TRACK, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
os_remote_command_callback(PREVIOUS_TRACK);
|
||||
os_remote_command_callback(PREVIOUS_TRACK, 0);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
|
||||
[commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
|
||||
MPChangePlaybackPositionCommandEvent *positionChangeEvent = (MPChangePlaybackPositionCommandEvent *)event;
|
||||
double posSecDouble = positionChangeEvent.positionTime;
|
||||
int posSeconds = (int)posSecDouble;
|
||||
os_remote_command_callback(SEEK, posSeconds);
|
||||
return MPRemoteCommandHandlerStatusSuccess;
|
||||
}];
|
||||
}
|
||||
@@ -41,7 +49,7 @@ void register_os_remote_commands() {
|
||||
/**
|
||||
* C bridge setting "Now Playing" information on macOS for media playback using the native APIs.
|
||||
*/
|
||||
void set_os_now_playing_info(const char *title, const char *artist, const char *coverArtFileURL) {
|
||||
void set_os_now_playing_info(const char *title, const char *artist, const char *coverArtFileURL, double trackDuration) {
|
||||
NSString *coverArtLocationString = [NSString stringWithUTF8String:coverArtFileURL];
|
||||
NSURL *coverArtURL = [NSURL URLWithString:coverArtLocationString];
|
||||
NSImage *coverArtImage = [[NSImage alloc] initWithContentsOfURL:coverArtURL];
|
||||
@@ -54,12 +62,24 @@ void set_os_now_playing_info(const char *title, const char *artist, const char *
|
||||
NSDictionary *nowPlayingInfo = @{
|
||||
MPMediaItemPropertyTitle: [NSString stringWithUTF8String:title],
|
||||
MPMediaItemPropertyArtist: [NSString stringWithUTF8String:artist],
|
||||
MPMediaItemPropertyArtwork: coverArt
|
||||
MPMediaItemPropertyArtwork: coverArt,
|
||||
MPMediaItemPropertyPlaybackDuration: @(trackDuration) // Expects 'NSNumber'
|
||||
};
|
||||
|
||||
infoCenter.nowPlayingInfo = nowPlayingInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* C bridge updating the OS playback position.
|
||||
* creates a mutable copy of the immutable dictionary and writes it back with updated position.
|
||||
*/
|
||||
void update_os_now_playing_info_position(double positionSeconds) {
|
||||
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
|
||||
NSMutableDictionary *updatedInfo = [infoCenter.nowPlayingInfo mutableCopy];
|
||||
updatedInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(positionSeconds);
|
||||
infoCenter.nowPlayingInfo = [updatedInfo copy];
|
||||
}
|
||||
|
||||
/**
|
||||
* C bridge setting the OS playback state to 'playing'.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user