Implemented MediaButtonEventReceiver
This commit is contained in:
parent
119892f6b5
commit
8b5a7d8a41
|
@ -33,8 +33,14 @@
|
||||||
<activity android:name="de.podfetcher.activity.MediaplayerActivity" android:launchMode="singleTask" android:configChanges="orientation"/>
|
<activity android:name="de.podfetcher.activity.MediaplayerActivity" android:launchMode="singleTask" android:configChanges="orientation"/>
|
||||||
|
|
||||||
<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" />
|
<service android:enabled="true" android:name="de.podfetcher.service.PlaybackService" >
|
||||||
|
</service>
|
||||||
<activity android:name=".activity.PreferenceActivity"></activity>
|
<activity android:name=".activity.PreferenceActivity"></activity>
|
||||||
<activity android:name=".activity.DownloadLogActivity"></activity>
|
<activity android:name=".activity.DownloadLogActivity"></activity>
|
||||||
|
<receiver android:name=".service.MediaButtonReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MEDIA_BUTTON"/>
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package de.podfetcher.service;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
|
/** Receives media button events. */
|
||||||
|
public class MediaButtonReceiver extends BroadcastReceiver {
|
||||||
|
private static final String TAG = "MediaButtonReceiver";
|
||||||
|
public static final String EXTRA_KEYCODE = "de.podfetcher.service.extra.MediaButtonReceiver.KEYCODE";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
Log.d(TAG, "Received intent");
|
||||||
|
KeyEvent event = (KeyEvent) intent.getExtras().get(
|
||||||
|
Intent.EXTRA_KEY_EVENT);
|
||||||
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
||||||
|
Intent serviceIntent = new Intent(context, PlaybackService.class);
|
||||||
|
int keycode = event.getKeyCode();
|
||||||
|
serviceIntent.putExtra(EXTRA_KEYCODE, keycode);
|
||||||
|
context.startService(serviceIntent);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import android.R;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
@ -17,6 +18,7 @@ import android.media.AudioManager.OnAudioFocusChangeListener;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.KeyEvent;
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
@ -62,6 +64,8 @@ public class PlaybackService extends Service {
|
||||||
private NotificationCompat.Builder notificationBuilder;
|
private NotificationCompat.Builder notificationBuilder;
|
||||||
|
|
||||||
private AudioManager audioManager;
|
private AudioManager audioManager;
|
||||||
|
private ComponentName mediaButtonReceiver;
|
||||||
|
|
||||||
private MediaPlayer player;
|
private MediaPlayer player;
|
||||||
|
|
||||||
private FeedMedia media;
|
private FeedMedia media;
|
||||||
|
@ -96,6 +100,10 @@ public class PlaybackService extends Service {
|
||||||
player.setOnPreparedListener(preparedListener);
|
player.setOnPreparedListener(preparedListener);
|
||||||
player.setOnCompletionListener(completionListener);
|
player.setOnCompletionListener(completionListener);
|
||||||
player.setOnSeekCompleteListener(onSeekCompleteListener);
|
player.setOnSeekCompleteListener(onSeekCompleteListener);
|
||||||
|
mediaButtonReceiver = new ComponentName(getPackageName(),
|
||||||
|
MediaButtonReceiver.class.getName());
|
||||||
|
audioManager.registerMediaButtonEventReceiver(mediaButtonReceiver);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -103,6 +111,7 @@ public class PlaybackService extends Service {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
Log.d(TAG, "Service is about to be destroyed");
|
Log.d(TAG, "Service is about to be destroyed");
|
||||||
|
audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver);
|
||||||
audioManager.abandonAudioFocus(audioFocusChangeListener);
|
audioManager.abandonAudioFocus(audioFocusChangeListener);
|
||||||
player.release();
|
player.release();
|
||||||
}
|
}
|
||||||
|
@ -140,12 +149,19 @@ public class PlaybackService extends Service {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
|
||||||
|
if (keycode != -1) {
|
||||||
|
Log.d(TAG, "Received media button event");
|
||||||
|
handleKeycode(keycode);
|
||||||
|
} else {
|
||||||
|
|
||||||
long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
|
long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
|
||||||
long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
|
long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
|
||||||
boolean playbackType = intent
|
boolean playbackType = intent.getBooleanExtra(EXTRA_SHOULD_STREAM,
|
||||||
.getBooleanExtra(EXTRA_SHOULD_STREAM, true);
|
true);
|
||||||
if (mediaId == -1 || feedId == -1) {
|
if (mediaId == -1 || feedId == -1) {
|
||||||
Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service.");
|
Log.e(TAG,
|
||||||
|
"Media ID or Feed ID wasn't provided to the Service.");
|
||||||
if (media == null || feed == null) {
|
if (media == null || feed == null) {
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
|
@ -180,9 +196,33 @@ public class PlaybackService extends Service {
|
||||||
Log.w(TAG, "Something went wrong. Shutting down...");
|
Log.w(TAG, "Something went wrong. Shutting down...");
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return Service.START_NOT_STICKY;
|
return Service.START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Handles media button events */
|
||||||
|
private void handleKeycode(int keycode) {
|
||||||
|
switch (keycode) {
|
||||||
|
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
|
||||||
|
if (status == PlayerStatus.PLAYING) {
|
||||||
|
pause();
|
||||||
|
} else if (status == PlayerStatus.PAUSED) {
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KeyEvent.KEYCODE_MEDIA_PLAY:
|
||||||
|
if (status == PlayerStatus.PAUSED) {
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KeyEvent.KEYCODE_MEDIA_PAUSE:
|
||||||
|
if (status == PlayerStatus.PLAYING) {
|
||||||
|
pause();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called by a mediaplayer Activity as soon as it has prepared its
|
* Called by a mediaplayer Activity as soon as it has prepared its
|
||||||
* mediaplayer.
|
* mediaplayer.
|
||||||
|
@ -343,7 +383,6 @@ public class PlaybackService extends Service {
|
||||||
|
|
||||||
if (focusGained == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
if (focusGained == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
||||||
Log.d(TAG, "Audiofocus successfully requested");
|
Log.d(TAG, "Audiofocus successfully requested");
|
||||||
|
|
||||||
Log.d(TAG, "Resuming/Starting playback");
|
Log.d(TAG, "Resuming/Starting playback");
|
||||||
SharedPreferences.Editor editor = getApplicationContext()
|
SharedPreferences.Editor editor = getApplicationContext()
|
||||||
.getSharedPreferences(PodcastApp.PREF_NAME, 0).edit();
|
.getSharedPreferences(PodcastApp.PREF_NAME, 0).edit();
|
||||||
|
|
Loading…
Reference in New Issue