fix scrobbler

This commit is contained in:
Adrian Ulrich 2017-01-26 20:18:07 +01:00
parent 541f846aaa
commit 7d90334867
2 changed files with 48 additions and 27 deletions

View File

@ -562,6 +562,25 @@ public class MediaUtils {
return matrixCursor;
}
/**
* Returns the id's used by Androids native media database for given song
*
* @param context the context to use
* @param song the song to query
* @return long { song_id, album_id, artist_id } - all set to -1 on error
*/
public static long[] getAndroidMediaIds(Context context, Song song) {
long[] result = { -1, -1, -1 };
String[] projection = new String[]{ MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ARTIST_ID };
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Audio.Media.DATA+"=?", new String[] { song.path }, null);
if (cursor.moveToFirst()) {
for (int i=0; i<result.length; i++)
result[i] = cursor.getLong(i);
}
cursor.close();
return result;
}
/**
* Retrieve ID of specified media type for requested song. This works only for
* media-oriented types: {@link #TYPE_ARTIST}, {@link #TYPE_ALBUM}, {@link #TYPE_SONG}

View File

@ -1061,11 +1061,7 @@ public final class PlaybackService extends Service
mRemoteControlClient.updateRemote(mCurrentSong, mState, mForceNotificationVisible);
if (mStockBroadcast)
stockMusicBroadcast();
if (mScrobble)
scrobble();
scrobbleBroadcast();
}
/**
@ -1099,31 +1095,37 @@ public final class PlaybackService extends Service
}
/**
* Send a broadcast emulating that of the stock music player.
* Scrobbles and broadcasts the currently playing song
*/
private void stockMusicBroadcast()
{
private void scrobbleBroadcast() {
Song song = mCurrentSong;
Intent intent = new Intent("com.android.music.playstatechanged");
intent.putExtra("playing", (mState & FLAG_PLAYING) != 0);
if (song != null) {
intent.putExtra("track", song.title);
intent.putExtra("album", song.album);
intent.putExtra("artist", song.artist);
intent.putExtra("songid", song.id);
intent.putExtra("albumid", song.albumId);
}
sendBroadcast(intent);
}
if (song == null)
return;
private void scrobble()
{
Song song = mCurrentSong;
Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
intent.putExtra("playing", (mState & FLAG_PLAYING) != 0);
if (song != null)
intent.putExtra("id", (int)song.id);
sendBroadcast(intent);
if (!mStockBroadcast && !mScrobble)
return;
long[] androidIds = MediaUtils.getAndroidMediaIds(getApplicationContext(), song);
if (mStockBroadcast) {
Intent intent = new Intent("com.android.music.playstatechanged");
intent.putExtra("playing", (mState & FLAG_PLAYING) != 0);
if (androidIds[0] != -1) {
intent.putExtra("track", song.title);
intent.putExtra("album", song.album);
intent.putExtra("artist", song.artist);
intent.putExtra("songid", androidIds[0]);
intent.putExtra("albumid", androidIds[1]);
}
sendBroadcast(intent);
}
if (mScrobble) {
Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
intent.putExtra("playing", (mState & FLAG_PLAYING) != 0);
if (androidIds[0] != -1)
intent.putExtra("id", androidIds[0]);
sendBroadcast(intent);
}
}
private void updateNotification()