Added a now playing sticky broadcast to support last.fm scrobbling and musixmatch.

This commit is contained in:
Karim Abou Zeid 2015-05-20 16:07:59 +02:00
commit bccf4fae5c
2 changed files with 27 additions and 4 deletions

View file

@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest package="com.kabouzeid.gramophone"
xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kabouzeid.gramophone"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application <application
android:name=".App" android:name=".App"
android:allowBackup="true" android:allowBackup="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/Theme.MaterialMusic.Light"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.MaterialMusic.Light"
tools:ignore="UnusedAttribute"> tools:ignore="UnusedAttribute">
<activity <activity

View file

@ -52,6 +52,8 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
public static final String ACTION_SKIP = "com.kabouzeid.gramophone.action.SKIP"; public static final String ACTION_SKIP = "com.kabouzeid.gramophone.action.SKIP";
public static final String ACTION_REWIND = "com.kabouzeid.gramophone.action.REWIND"; public static final String ACTION_REWIND = "com.kabouzeid.gramophone.action.REWIND";
public static final String ACTION_QUIT = "com.kabouzeid.gramophone.action.QUIT"; public static final String ACTION_QUIT = "com.kabouzeid.gramophone.action.QUIT";
public static final String META_CHANGED = "com.android.music.metachanged";
public static final String PLAYSTATE_CHANGED = "com.android.music.playstatechanged";
public static final int SHUFFLE_MODE_NONE = 0; public static final int SHUFFLE_MODE_NONE = 0;
public static final int SHUFFLE_MODE_SHUFFLE = 1; public static final int SHUFFLE_MODE_SHUFFLE = 1;
public static final int REPEAT_MODE_NONE = 0; public static final int REPEAT_MODE_NONE = 0;
@ -227,6 +229,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying()); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying());
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.STOP); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.STOP);
notifyChange(PLAYSTATE_CHANGED);
} }
public boolean isPlaying() { public boolean isPlaying() {
@ -277,6 +280,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying()); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying());
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.STOP); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.STOP);
notifyChange(PLAYSTATE_CHANGED);
} else { } else {
acquireWakeLock(30000); acquireWakeLock(30000);
playNextSong(false); playNextSong(false);
@ -313,6 +317,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
playingNotificationHelper.updatePlayState(false); playingNotificationHelper.updatePlayState(false);
MusicPlayerWidget.updateWidgetsPlayState(this, false); MusicPlayerWidget.updateWidgetsPlayState(this, false);
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
notifyChange(PLAYSTATE_CHANGED);
Toast.makeText(getApplicationContext(), getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show();
return; return;
@ -321,9 +326,11 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
updateNotification(); updateNotification();
updateWidgets(); updateWidgets();
updateRemoteControlClient(); updateRemoteControlClient();
notifyChange(META_CHANGED);
} else { } else {
playingNotificationHelper.updatePlayState(false); playingNotificationHelper.updatePlayState(false);
MusicPlayerWidget.updateWidgetsPlayState(this, false); MusicPlayerWidget.updateWidgetsPlayState(this, false);
notifyChange(PLAYSTATE_CHANGED);
} }
} }
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.TRACK_CHANGED); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.TRACK_CHANGED);
@ -481,6 +488,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying()); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying());
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.PLAY); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.PLAY);
notifyChange(PLAYSTATE_CHANGED);
savePosition(); savePosition();
releaseWakeLock(); releaseWakeLock();
} }
@ -645,6 +653,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying()); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying());
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.PAUSE); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.PAUSE);
notifyChange(PLAYSTATE_CHANGED);
} }
} }
@ -657,6 +666,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying()); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying());
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
notifyOnMusicRemoteEventListeners(MusicRemoteEvent.RESUME); notifyOnMusicRemoteEventListeners(MusicRemoteEvent.RESUME);
notifyChange(PLAYSTATE_CHANGED);
} else { } else {
playSong(); playSong();
} }
@ -781,6 +791,18 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
} }
} }
private void notifyChange(final String what) {
//to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch
final Song currentSong = playingQueue.get(getPosition());
final Intent intent = new Intent(what);
intent.putExtra("id", currentSong.id);
intent.putExtra("artist", currentSong.artistName);
intent.putExtra("album", currentSong.albumName);
intent.putExtra("track", currentSong.title);
intent.putExtra("playing", isPlaying());
sendStickyBroadcast(intent);
}
public int getAudioSessionId() { public int getAudioSessionId() {
if (player == null) if (player == null)
return AudioEffect.ERROR_BAD_VALUE; return AudioEffect.ERROR_BAD_VALUE;