Stop playback on noisy audio broadcast, fixes #6
This commit is contained in:
parent
b474a6533b
commit
24c2ccf9a4
1 changed files with 37 additions and 2 deletions
|
@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
package com.github.nutomic.controldlna.localroute;
|
package com.github.nutomic.controldlna.localroute;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
|
@ -38,6 +39,7 @@ import android.support.v7.media.MediaItemStatus;
|
||||||
import android.support.v7.media.MediaRouteProvider;
|
import android.support.v7.media.MediaRouteProvider;
|
||||||
import android.support.v7.media.MediaRouter.ControlRequestCallback;
|
import android.support.v7.media.MediaRouter.ControlRequestCallback;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -66,23 +68,52 @@ public class Controller extends MediaRouteProvider.RouteController implements
|
||||||
|
|
||||||
private MediaPlayer mPlayer = new MediaPlayer();
|
private MediaPlayer mPlayer = new MediaPlayer();
|
||||||
|
|
||||||
|
private NoisyAudioReceiver mNoisyAudioReceiver;
|
||||||
|
|
||||||
|
class NoisyAudioReceiver extends BroadcastReceiver {
|
||||||
|
private Controller mController;
|
||||||
|
|
||||||
|
public void register(Controller controller, Context context) {
|
||||||
|
mController=controller;
|
||||||
|
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
|
||||||
|
context.registerReceiver( this, intentFilter );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister(Context context) {
|
||||||
|
context.unregisterReceiver(this);
|
||||||
|
mController=null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
|
||||||
|
if (mController != null)
|
||||||
|
mController.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Controller(String routeId, Context context) {
|
public Controller(String routeId, Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mRouteId = routeId;
|
mRouteId = routeId;
|
||||||
mAudio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
mAudio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||||
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||||
mPlayer.setOnPreparedListener(this);
|
mPlayer.setOnPreparedListener(this);
|
||||||
|
mNoisyAudioReceiver = new NoisyAudioReceiver();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRelease() {
|
public void onRelease() {
|
||||||
|
|
||||||
mPlayer.release();
|
mPlayer.release();
|
||||||
|
mNoisyAudioReceiver.unregister(mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSelect() {
|
public void onSelect() {
|
||||||
mAudio.requestAudioFocus(mFocusListener, AudioManager.STREAM_MUSIC,
|
mAudio.requestAudioFocus(mFocusListener, AudioManager.STREAM_MUSIC,
|
||||||
AudioManager.AUDIOFOCUS_GAIN);
|
AudioManager.AUDIOFOCUS_GAIN);
|
||||||
|
mNoisyAudioReceiver.register(this, mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -125,8 +156,7 @@ public class Controller extends MediaRouteProvider.RouteController implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (intent.getAction().equals(MediaControlIntent.ACTION_PAUSE)) {
|
else if (intent.getAction().equals(MediaControlIntent.ACTION_PAUSE)) {
|
||||||
mPlayer.pause();
|
pause();
|
||||||
mState = MediaItemStatus.PLAYBACK_STATE_PAUSED;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (intent.getAction().equals(MediaControlIntent.ACTION_RESUME)) {
|
else if (intent.getAction().equals(MediaControlIntent.ACTION_RESUME)) {
|
||||||
|
@ -198,4 +228,9 @@ public class Controller extends MediaRouteProvider.RouteController implements
|
||||||
mState = MediaItemStatus.PLAYBACK_STATE_PLAYING;
|
mState = MediaItemStatus.PLAYBACK_STATE_PLAYING;
|
||||||
mPlayer.setOnCompletionListener(this);
|
mPlayer.setOnCompletionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void pause() {
|
||||||
|
mPlayer.pause();
|
||||||
|
mState = MediaItemStatus.PLAYBACK_STATE_PAUSED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue