Amazon fire remote affecting background process - android

I have a media application for the Amazon Fire tv and tv stick. I have successfully captured the buttons and have customized the events accordingly.
Issue arises in the case when some other media app such as pandora is running in the background. When I fastforward,rewind etc in my app , even pandora gets changed in the process.Amazon has declined the app for the same reason.How do I get to set the focus of the remote in the current app only.
The following is my code for remote
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
boolean handled = false;
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_DPAD_LEFT:
mPlayerView.seek((int)mPlayerView.getPosition()-3000);
handled = true;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e("right","pressed");
mPlayerView.seek((int)mPlayerView.getPosition()+3000);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
mPlayerView.seek((int)mPlayerView.getPosition()+60000);
handled=true;
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
mPlayerView.seek((int)mPlayerView.getPosition()-60000);
handled=true;
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
mPlayerView.play();
handled=true;
break;
case KeyEvent.KEYCODE_MENU:
subtitle=!subtitle;
if(subsexists){
if(subtitle) {
mPlayerView.setCurrentCaptions(1);
Toast.makeText(this,"Subtitles ON",Toast.LENGTH_LONG).show();
}
else {
mPlayerView.setCurrentCaptions(0);
Toast.makeText(this,"Subtitles OFF",Toast.LENGTH_LONG).show();
}
}
handled=true;
break;
}
return handled || super.onKeyDown(keyCode, event);
}

See this section of the FireTV Developer FAQ. Specifically you need to implement code to:
When app starts playing, request audio focus with AudioManager.requestAudioFocus()
If audio focus was granted, register a media button receiver with AudioManager.registerMediaButtonEventReceiver()
you also need to make sure that you gracefully give up control as well if another media player app has the users attention
Listen for the loss of audio focus with AudioManager.onAudioFocusChangeListener()
If your app loses audio focus, stop playback and unregister the media buttons with AudioManager.unregisterMediaButtonEventReceiver()

Related

Android TV How To Prevent Other Applications In The Background From Using Media Buttons

On my Android TV if I launch Pandora in the background and let music play, then launch my app.
From my application if I press pause or next it receives these on my applicaiotn but also affects Pandora.
How can I prevent background applications from receiving these inputs.
I tried obtaining audio focus which effectively stops the music but if button input is made again music will resume.
Any ideas?
Here is my code capturing remote inputs.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
moveToNextImage(true);
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
moveToPreviousImage(true);
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
playPauseController();
break;
default:
break;
}
}
return super.onKeyDown(keyCode, event);
Found it! To prevent other applications from receiving remote inputs while my app is active, add the handled boolean.
Other option was gaining audio focus, but since I was not using audio it was not needed.
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean handled = false;
if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
moveToNextImage(true);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
moveToPreviousImage(true);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
playPauseController();
handled = true;
break;
default:
break;
}
}
return handled || super.onKeyDown(keyCode, event);

How to check if sound should be ducked?

When we loose audio focus due to AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK we should reduce sound volume, untile audiofocus will be restored to AudioManager.AUDIOFOCUS_GAIN. However, it is fired if we registered listener before event.
How to check if audio focus is in state AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK if we registered after AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK event?
AudioManager.OnAudioFocusChangeListener audiofocusListener =
new AudioManager.OnAudioFocusChangeListener() {
#Override public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
}
break;
}
}
};
If I understand your question correctly, you want to know whether some other app is currently holding audio focus (and thus the audio output of your app should be ducked) before you actually have registered your audio focus listener.
If that's the case (some other app currently owns the audio focus), then your request for acquiring the audio focus will be denied, that is, your call to AudioManager.requestAudioFocus with audiofocusListener will return with AUDIOFOCUS_REQUEST_FAILED.

How to control Media Volume when a Bluetooth device is connected?

I have setted the StreamControlVolume in the OnCreate of my Activity in order to control Music/Media with volume buttons.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
But when I connect a Bluetooth device to my application and Sco is routed, the VolumeControlStream switches to bluetooth, I have tried to change the VolumeControlStream after the Sco is on, but it still changes Bluetooth volume when I press the volume buttons like in the image below:
I have tried to setControlStream right before the volume changes (overriding dispatchKeyEvent) but it didn't worked
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
default:
return super.dispatchKeyEvent(event);
}
}

Too much delay playing a sound after pressing button

I'm creating a sampler app for school. I've wrote code which plays a sample when I press and hold a button and stops when I release it. My problem is that it has too much latency. It takes too long after I press the button to play the sound.
My audio files are mp3's.
Here's my code:
smpl1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent theMotion)
{
switch (theMotion.getAction())
{
case MotionEvent.ACTION_DOWN:
sample = MediaPlayer.create(MainActivity.this, R.raw.bassdrum);
smpl1.setText("ON");
smpl1.setTextColor(Color.GREEN);
sample.start();
break;
case MotionEvent.ACTION_UP:
smpl1.setText("OFF");
smpl1.setTextColor(Color.RED);
sample.stop();
break;
}
return true;
}
Create the sample object before the button press, then just use the start / stop functionality in your handler code.

Problem with default Android onKey... sound

I'm having hard time figuring out how to get rid of the default sound played while any key is pressed. I'm using following method but it still plays the same "beep" sound.
#Override public boolean onKeyDown( int keyCode, KeyEvent event )
{
switch ( keyCode )
{
case 25:
ChangeImageUP( );
break;
case 24:
ChangeImageDOWN( );
break;
default:
return super.onKeyDown( keyCode, event );
}
return true;
}
Please help.
// Update
I have figure out that if I overwrite onKeyUp in my main activity the beep is gone. But when i start a second activity the beep returns, even after using that onKeyUp method.
That is controlled by the operating system and the users settings. You might not be able to modify it..

Categories

Resources