Permalink
Browse files

moved play and queue tracks to intents

  • Loading branch information...
1 parent c6d6a18 commit 89371464215bf7d45bc4348713534d3d1410bd2a @augusto committed Jul 13, 2011
View
@@ -21,12 +21,7 @@
</activity>
<activity android:name=".BrowseTracks"/>
<activity android:name=".BrowseAlbums"/>
- <service android:name=".services.AudioPlayer">
- <intent-filter >
- <action android:name="com.augusto.mymediaplayer.AudioPlayer.PLAY_ALBUM"/>
- <action android:name="com.augusto.mymediaplayer.AudioPlayer.QUEUE_ALBUM"/>
- </intent-filter>
- </service>
+ <service android:name=".services.AudioPlayer"/>
</application>
</manifest>
@@ -95,15 +95,14 @@ public boolean onContextItemSelected(MenuItem item) {
private void queueAlbum(Album album) {
Intent intent = new Intent(AudioPlayer.QUEUE_ALBUM);
intent.putExtra("id", album.getId());
- this.startService(intent);
-
+ //this.startService(intent);
+ this.sendBroadcast(intent);
}
private void playAlbum(Album album) {
Intent intent = new Intent(AudioPlayer.PLAY_ALBUM);
intent.putExtra("id", album.getId());
- this.startService(intent);
+ //this.startService(intent);
+ this.sendBroadcast(intent);
}
-
-
}
@@ -48,7 +48,7 @@ public void onCreate(Bundle savedInstanceState) {
albumId = getIntentAlbumId();
MusicRepository musicRepository = new MusicRepository();
- tracks = musicRepository.findTracksFilteredBy(this, albumId);
+ tracks = musicRepository.findTracksByAlbumId(this, albumId);
ListAdapter adapter = new TrackListAdapter(tracks,layoutInflater);
setListAdapter(adapter);
@@ -110,6 +110,7 @@ public boolean onContextItemSelected(MenuItem item) {
Track track = tracks[info.position];
+ Intent intent;
switch(itemId) {
case R.string.play:
playTrack(track);
@@ -125,13 +126,17 @@ public boolean onContextItemSelected(MenuItem item) {
}
private void playTrack(Track track) {
- audioPlayer.play(track);
+ Intent intent = new Intent(AudioPlayer.PLAY_TRACK);
+ intent.putExtra("id", track.getId());
+ this.sendBroadcast(intent);
notify(track);
}
private void addTrack(Track track) {
- audioPlayer.addTrack(track);
+ Intent intent = new Intent(AudioPlayer.QUEUE_TRACK);
+ intent.putExtra("id", track.getId());
+ this.sendBroadcast(intent);
notify(track);
}
@@ -28,8 +28,22 @@
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.NUMBER_OF_SONGS};
+
+ public Track findTracksId(Context context, long trackId) {
+ Track[] tracks = findTracks(context, trackId, null);
+
+ if( tracks.length == 0) {
+ return null;
+ }
+
+ return tracks[0];
+ }
+
+ public Track[] findTracksByAlbumId(Context context, Long albumId) {
+ return findTracks(context, null, albumId);
+ }
- public Track[] findTracksFilteredBy(Context context, Long albumId) {
+ private Track[] findTracks(Context context, Long trackId, Long albumId) {
String where = null;
String[] selectionArgs = null;
@@ -38,6 +52,10 @@
where = MediaStore.Audio.Media.ALBUM_ID + " = ?";
selectionArgs = new String[]{ albumId.toString() };
orderBy = MediaStore.Audio.Media.TRACK;
+ } else if ( trackId != null ){
+ where = MediaStore.Audio.Media._ID + " = ?";
+ selectionArgs = new String[]{ trackId.toString() };
+ orderBy = MediaStore.Audio.Media.TRACK;
}
ContentResolver contentResolver = context.getContentResolver();
@@ -122,5 +140,4 @@ public Album findAlbumById(Activity activity, Long albumId) {
public Album[] findAllAlbums(Activity activity) {
return findAlbumsFilteredBy(activity, null);
}
-
}
@@ -7,7 +7,10 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
@@ -22,7 +25,9 @@
public class AudioPlayer extends Service implements OnCompletionListener {
public static final String INTENT_BASE_NAME = "com.augusto.mymediaplayer.AudioPlayer";
public static final String UPDATE_PLAYLIST = INTENT_BASE_NAME + ".PLAYLIST_UPDATED";
- public static final String QUEUE_ALBUM = INTENT_BASE_NAME + ".QUEUE_ALBUM";
+ public static final String QUEUE_TRACK = INTENT_BASE_NAME + ".QUEUE_TRACK";
+ public static final String PLAY_TRACK = INTENT_BASE_NAME + ".PLAY_TRACK";
+ public static final String QUEUE_ALBUM = INTENT_BASE_NAME + ".QUEUE_ALBUM";
public static final String PLAY_ALBUM = INTENT_BASE_NAME + ".PLAY_ALBUM";
private final String TAG = "AudioPlayer";
@@ -31,6 +36,7 @@
private MusicRepository musicRepository = new MusicRepository();
private MediaPlayer mediaPlayer;
private boolean paused = false;
+ private AudioPlayerBroadcastReceiver broadcastReceiver = new AudioPlayerBroadcastReceiver();
public class AudioPlayerBinder extends Binder {
public AudioPlayer getService() {
@@ -50,47 +56,22 @@ public IBinder onBind(Intent intent) {
@Override
public void onCreate() {
Log.v(TAG, "AudioPlayer: onCreate() called");
+ IntentFilter intentFilter = new IntentFilter();
+ intentFilter.addAction(PLAY_TRACK);
+ intentFilter.addAction(QUEUE_TRACK);
+ intentFilter.addAction(PLAY_ALBUM);
+ intentFilter.addAction(QUEUE_ALBUM);
+ registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "AudioPlayer: onStart() called, instance=" + this.hashCode());
-
- String action = intent.getAction();
- if( PLAY_ALBUM.equals(action)) {
- long albumId = intent.getLongExtra("id", -1);
- Log.d(TAG, "Received intent to play album: " + albumId);
- playAlbum(albumId);
- } else if( QUEUE_ALBUM.equals(action)) {
- long albumId = intent.getLongExtra("id", -1);
- Log.d(TAG, "Received intent to queue album: " + albumId);
- queueAlbum(albumId);
- } else {
- Log.d(TAG, "Action not recognized: " + action);
- }
}
- private void playAlbum(long albumId) {
- Track[] tracks = musicRepository.findTracksFilteredBy(this, albumId);
- this.tracks.clear(); // I DON'T LIKE THIS!!!
- stop();
- for( Track track : tracks) {
- this.tracks.add(track);
- }
- play();
- }
-
- private void queueAlbum(long albumId) {
- Track[] tracks = musicRepository.findTracksFilteredBy(this, albumId);
- for( Track track : tracks) {
- addTrack(track);
- }
- }
-
@Override
public void onDestroy() {
Log.i(TAG, "AudioPlayer: onDestroy() called");
-
release();
}
@@ -217,4 +198,55 @@ public void seek(int timeInMillis) {
mediaPlayer.seekTo(timeInMillis);
}
}
+
+ private void playTrack(long trackId) {
+ Track track = musicRepository.findTracksId(this, trackId);
+ play(track);
+ }
+
+ private void queueTrack(long trackId) {
+ Track track = musicRepository.findTracksId(this, trackId);
+ addTrack(track);
+ }
+
+ private void playAlbum(long albumId) {
+ Track[] tracks = musicRepository.findTracksByAlbumId(this, albumId);
+ this.tracks.clear(); // I DON'T LIKE THIS!!!
+ stop();
+ for( Track track : tracks) {
+ this.tracks.add(track);
+ }
+ play();
+ }
+
+ private void queueAlbum(long albumId) {
+ Track[] tracks = musicRepository.findTracksByAlbumId(this, albumId);
+ for( Track track : tracks) {
+ addTrack(track);
+ }
+ }
+
+ private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ long id = intent.getLongExtra("id", -1);
+ Log.d(TAG, "Received intent for action " + intent.getAction() + " for id: " + id);
+
+ if( PLAY_ALBUM.equals(action)) {
+ playAlbum(id);
+ } else if( QUEUE_ALBUM.equals(action)) {
+ queueAlbum(id);
+ } else if( PLAY_TRACK.equals(action)) {
+ playTrack(id);
+ } else if( QUEUE_TRACK.equals(action)) {
+ queueTrack(id);
+ } else {
+ Log.d(TAG, "Action not recognized: " + action);
+ }
+ }
+
+ }
}
+

0 comments on commit 8937146

Please sign in to comment.