Skip to content

Commit

Permalink
Merge pull request #86 from ghenry22/dev
Browse files Browse the repository at this point in the history
Merge dev
  • Loading branch information
ghenry22 authored Nov 17, 2022
2 parents d875a8c + 0371bc2 commit 1713dd7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ MusicControls.create({

- Destroy the media controller:
```javascript
MusicControls.destroy(onSuccess, onError);
MusicControls.destroy();
```

- Subscribe events to the media controller:
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-music-controls2"
version="3.0.6">
version="3.0.7">
<name>Music Controls</name>
<keywords>cordova,music,controller,controls,media,plugin,notification,lockscreen,now,playing</keywords>
<repo>https://github.com/ghenry22/cordova-plugin-music-controls2</repo>
Expand Down Expand Up @@ -35,7 +35,7 @@
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="com.homerours.musiccontrols.MusicControlsNotificationKiller"></service>
<service android:name="com.homerours.musiccontrols.MusicControlsNotificationKiller" android:stopWithTask="true" android:foregroundServiceType="mediaPlayback"></service>
</config-file>

<framework src="com.android.support:support-v4:28.0.0" />
Expand Down
27 changes: 23 additions & 4 deletions src/android/MusicControls.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import android.media.session.MediaSession.Token;

import android.util.Log;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -48,12 +50,12 @@ public class MusicControls extends CordovaPlugin {
private AudioManager mAudioManager;
private PendingIntent mediaButtonPendingIntent;
private boolean mediaButtonAccess=true;
private android.media.session.MediaSession.Token token;

private Activity cordovaActivity;

private MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback();


private void registerBroadcaster(MusicControlsBroadcastReceiver mMessageReceiver){
final Context context = this.cordova.getActivity().getApplicationContext();
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-previous"));
Expand Down Expand Up @@ -101,7 +103,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
final MusicControlsServiceConnection mConnection = new MusicControlsServiceConnection(activity);

this.cordovaActivity = activity;
this.notification = new MusicControlsNotification(this.cordovaActivity, this.notificationID) {
/* this.notification = new MusicControlsNotification(this.cordovaActivity, this.notificationID) {
@Override
protected void onNotificationUpdated(Notification notification) {
mConnection.setNotification(notification, this.infos.isPlaying);
Expand All @@ -111,7 +113,7 @@ protected void onNotificationUpdated(Notification notification) {
protected void onNotificationDestroyed() {
mConnection.setNotification(null, false);
}
};
}; */

this.mMessageReceiver = new MusicControlsBroadcastReceiver(this);
this.registerBroadcaster(mMessageReceiver);
Expand All @@ -120,17 +122,34 @@ protected void onNotificationDestroyed() {
this.mediaSessionCompat = new MediaSessionCompat(context, "cordova-music-controls-media-session", null, this.mediaButtonPendingIntent);
this.mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

MediaSessionCompat.Token _token = this.mediaSessionCompat.getSessionToken();
this.token = (android.media.session.MediaSession.Token) _token.getToken();

setMediaPlaybackState(PlaybackStateCompat.STATE_PAUSED);
this.mediaSessionCompat.setActive(true);

this.mediaSessionCompat.setCallback(this.mMediaSessionCallback);

this.notification = new MusicControlsNotification(this.cordovaActivity, this.notificationID, this.token) {
@Override
protected void onNotificationUpdated(Notification notification) {
mConnection.setNotification(notification, this.infos.isPlaying);
}

@Override
protected void onNotificationDestroyed() {
mConnection.setNotification(null, false);
}
};

// Register media (headset) button event receiver
try {
this.mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Intent headsetIntent = new Intent("music-controls-media-button");
this.mediaButtonPendingIntent = PendingIntent.getBroadcast(context, 0, headsetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
this.mediaButtonPendingIntent = PendingIntent.getBroadcast(
context, 0, headsetIntent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE : PendingIntent.FLAG_UPDATE_CURRENT
);
this.registerMediaButtonEvent();
} catch (Exception e) {
this.mediaButtonAccess=false;
Expand Down
30 changes: 16 additions & 14 deletions src/android/MusicControlsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.net.Uri;
import android.media.session.MediaSession.Token;

import android.app.NotificationChannel;

Expand All @@ -36,16 +37,18 @@ public class MusicControlsNotification {
protected MusicControlsInfos infos;
private Bitmap bitmapCover;
private String CHANNEL_ID;
private Token token;

// Public Constructor
public MusicControlsNotification(Activity cordovaActivity, int id){
public MusicControlsNotification(Activity cordovaActivity, int id, Token token){
this.CHANNEL_ID = UUID.randomUUID().toString();
this.notificationID = id;
this.cordovaActivity = cordovaActivity;
Context context = cordovaActivity;
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
this.token = token;
this.notificationManager = (NotificationManager) cordovaActivity.getSystemService(Context.NOTIFICATION_SERVICE);

// use channelid for Oreo and higher
// use channelID for Oreo and higher
if (Build.VERSION.SDK_INT >= 26) {
// The user-visible name of the channel.
CharSequence name = "Audio Controls";
Expand Down Expand Up @@ -145,8 +148,7 @@ private Bitmap getBitmapFromURL(String strURL) {
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
return BitmapFactory.decodeStream(input);
} catch (Exception ex) {
ex.printStackTrace();
return null;
Expand All @@ -157,7 +159,7 @@ private void createBuilder(){
Context context = cordovaActivity;
Notification.Builder builder = new Notification.Builder(context);

// use channelid for Oreo and higher
// use channelID for Oreo and higher
if (Build.VERSION.SDK_INT >= 26) {
builder.setChannelId(this.CHANNEL_ID);
}
Expand All @@ -173,7 +175,7 @@ private void createBuilder(){
if (infos.dismissable){
builder.setOngoing(false);
Intent dismissIntent = new Intent("music-controls-destroy");
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, 1, dismissIntent, 0);
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, 1, dismissIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.setDeleteIntent(dismissPendingIntent);
} else {
builder.setOngoing(true);
Expand Down Expand Up @@ -216,7 +218,7 @@ private void createBuilder(){
Intent resultIntent = new Intent(context, cordovaActivity.getClass());
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.setContentIntent(resultPendingIntent);

//Controls
Expand All @@ -226,35 +228,35 @@ private void createBuilder(){
/* Previous */
nbControls++;
Intent previousIntent = new Intent("music-controls-previous");
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, 0);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.prevIcon, android.R.drawable.ic_media_previous), "", previousPendingIntent);
}
if (infos.isPlaying){
/* Pause */
nbControls++;
Intent pauseIntent = new Intent("music-controls-pause");
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(context, 1, pauseIntent, 0);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(context, 1, pauseIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.pauseIcon, android.R.drawable.ic_media_pause), "", pausePendingIntent);
} else {
/* Play */
nbControls++;
Intent playIntent = new Intent("music-controls-play");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 1, playIntent, 0);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 1, playIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.playIcon, android.R.drawable.ic_media_play), "", playPendingIntent);
}

if (infos.hasNext){
/* Next */
nbControls++;
Intent nextIntent = new Intent("music-controls-next");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 1, nextIntent, 0);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 1, nextIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.nextIcon, android.R.drawable.ic_media_next), "", nextPendingIntent);
}
if (infos.hasClose){
/* Close */
nbControls++;
Intent destroyIntent = new Intent("music-controls-destroy");
PendingIntent destroyPendingIntent = PendingIntent.getBroadcast(context, 1, destroyIntent, 0);
PendingIntent destroyPendingIntent = PendingIntent.getBroadcast(context, 1, destroyIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.closeIcon, android.R.drawable.ic_menu_close_clear_cancel), "", destroyPendingIntent);
}

Expand All @@ -264,7 +266,7 @@ private void createBuilder(){
for (int i = 0; i < nbControls; ++i) {
args[i] = i;
}
builder.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(args));
builder.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(args).setMediaSession(this.token));
}
this.notificationBuilder = builder;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ios/MusicControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
- (void) handleMusicControlsNotification:(NSNotification *) notification;
- (void) registerMusicControlsEventListener;
- (void) deregisterMusicControlsEventListener;
- (void) becomeFirstResponder;
- (void) resignFirstResponder;

@end

Expand Down
14 changes: 14 additions & 0 deletions src/ios/MusicControls.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ - (void) handleMusicControlsNotification: (NSNotification *) notification {
}
}

- (void) viewDidAppear:(BOOL)animated
{
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
}

- (void) viewWillDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}

//There are only 3 button slots available so next/prev track and skip forward/back cannot both be enabled
//skip forward/back will take precedence if both are enabled
- (void) registerMusicControlsEventListener {
Expand Down

0 comments on commit 1713dd7

Please sign in to comment.