Created Playback Service

This commit is contained in:
Daniel Oeh 2012-05-30 14:57:11 +02:00
parent d92de110df
commit d98fa57468
5 changed files with 122 additions and 28 deletions

View File

@ -29,5 +29,6 @@
<activity android:name="de.podfetcher.activity.ItemviewActivity"/> <activity android:name="de.podfetcher.activity.ItemviewActivity"/>
<service android:enabled="true" android:name="de.podfetcher.service.DownloadService" /> <service android:enabled="true" android:name="de.podfetcher.service.DownloadService" />
<service android:enabled="true" android:name="de.podfetcher.service.PlaybackService" />
</application> </application>
</manifest> </manifest>

View File

@ -17,6 +17,7 @@ import android.widget.TextView;
import android.widget.ImageView; import android.widget.ImageView;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.service.DownloadObserver; import de.podfetcher.service.DownloadObserver;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.DownloadRequester; import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.fragment.FeedlistFragment; import de.podfetcher.fragment.FeedlistFragment;
@ -52,6 +53,16 @@ public class ItemviewActivity extends SherlockActivity {
getDownloadStatus(); getDownloadStatus();
} }
}); });
butPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchIntent = new Intent(v.getContext(), PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, item.getMedia().getId());
launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, item.getFeed().getId());
v.getContext().startService(launchIntent);
}
});
} }
/** Extracts FeedItem object the activity is supposed to display */ /** Extracts FeedItem object the activity is supposed to display */

View File

@ -132,13 +132,13 @@ public class FeedManager {
/** Updates information of an existing FeedImage */ /** Updates information of an existing FeedImage */
public long setFeedImage(Context context, FeedImage image) { public long setFeedImage(Context context, FeedImage image) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setImage(image); return adapter.setImage(image);
} }
/** Updates information of an existing FeedMedia object. */ /** Updates information of an existing FeedMedia object. */
public long setFeedMedia(Context context, FeedMedia media) { public long setFeedMedia(Context context, FeedMedia media) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setMedia(media); return adapter.setMedia(media);
} }
@ -165,12 +165,23 @@ public class FeedManager {
/** Get a Feed Item by its id and its feed*/ /** Get a Feed Item by its id and its feed*/
public FeedItem getFeedItem(long id, Feed feed) { public FeedItem getFeedItem(long id, Feed feed) {
for(FeedItem item : feed.getItems()) { for(FeedItem item : feed.getItems()) {
if(item.getId() == id) { if(item.getId() == id) {
return item; return item;
}
} }
Log.w(TAG, "Couldn't find FeedItem with id " + id); }
Log.e(TAG, "Couldn't find FeedItem with id " + id);
return null;
}
/** Get a FeedMedia object by the id of the Media object and the feed object */
public FeedMedia getFeedMedia(long id, Feed feed) {
for (FeedItem item : feed.getItems()) {
if(item.getMedia().getId() == id) {
return item.getMedia();
}
}
Log.e(TAG, "Couldn't find FeedMedia with id " + id);
return null; return null;
} }

View File

@ -25,7 +25,7 @@ import android.util.Log;
public class FeedlistFragment extends SherlockListFragment { public class FeedlistFragment extends SherlockListFragment {
private static final String TAG = "FeedlistActivity"; private static final String TAG = "FeedlistFragment";
public static final String EXTRA_SELECTED_FEED = "extra.de.podfetcher.activity.selected_feed"; public static final String EXTRA_SELECTED_FEED = "extra.de.podfetcher.activity.selected_feed";
private FeedManager manager; private FeedManager manager;

View File

@ -0,0 +1,71 @@
package de.podfetcher.service;
import java.io.File;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.net.Uri;
import android.os.IBinder;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedManager;
/** Controls the MediaPlayer that plays a FeedMedia-file */
public class PlaybackService extends Service {
/** Logging tag */
private static final String TAG = "PlaybackService";
/** Contains the id of the FeedMedia object. */
public static final String EXTRA_MEDIA_ID = "extra.de.podfetcher.service.mediaId";
/** Contains the id of the Feed object of the FeedMedia. */
public static final String EXTRA_FEED_ID = "extra.de.podfetcher.service.feedId";
private MediaPlayer player;
private FeedMedia media;
private Feed feed;
private FeedManager manager;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service created.");
manager = FeedManager.getInstance();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (player != null) {
player.stop();
}
long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
if (mediaId == -1 || feedId == -1) {
Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service.");
} else {
feed = manager.getFeed(feedId);
media = manager.getFeedMedia(mediaId, feed);
player = MediaPlayer.create(this, Uri.fromFile(new File(media.getFile_url())));
player.setOnPreparedListener(preparedListener);
Log.d(TAG, "Preparing to play file");
//player.prepareAsync();
}
return Service.START_NOT_STICKY;
}
private MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Resource prepared");
mp.start();
}
};
}