Android mediacontroller pauses when display turns off? - android

is there a way to stop mediacontroller from pausing when the screen display turns off either by itself or manually. My app uses the mediacontroller to stream audio files from the internet so I need it to keep playing even if the screen is off.
Any help would be much appreciated.
here is the code for my mediacontroller class:
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.widget.MediaController;
public class WillMediaController extends MediaController {
public WillMediaController(Context context) {
super(context);
}
#Override
public void hide() {
// Do Nothing to show the controller all times
}
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
((Activity) getContext()).finish();
}else{
super.dispatchKeyEvent(event);
}
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN ||
event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
// don't show the controls for volume adjustment
return super.dispatchKeyEvent(event);
}
return true;
}
}
Thanks

I'm not sure if there's a way to prevent the MediaController from pausing itself when the activity pauses (ie. when the screen goes off), but I do have a couple of ideas:
First, you could try to disable the screen from timing out in your activity via:
getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
However, this probably won't prevent the user from hitting the lock button and manually turning the screen off and ending the audio playback.
Second, you could create a Service with a MediaPlayer to play your audio stream in the background and respond to play / pause commands from your app: http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

Related

Android MediaPlayer to mute on a single click on Volume down botton pressed

I am using Android's MediaPlayer in my RingtoneService (which is a background Service for Ringtone in my application)to ring a ringtone when my Alarm goes on and off!
on the start of Ringtone Service for alarming
MediaPlayer mediaSong;
mediaSong = MediaPlayer.create(this, R.raw.xyz);
mediaSong.start();
on stop after some time
mediaSong.stop();
Its perfect in ringing and stopping the tone.whereas, I would like to make my device silent or say mute when I click on hardware's volume button pressed when an alarm is still ringing.
some stated solutions didn't answer my requirement.Any help is appreciated, please.
Please let me know somewhere if I am unclear!
Thanks in advance!
You need to override onKeyDown method in your Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mediaSong.stop
return true;
}
return super.onKeyDown(keyCode, event);
}

Detect event when hardware volume button is pressed in android

i am developing an app which listen volume events whenever hardware volume button is pressed. app can be in foreground or background. i have followed [this] (Is there a broadcast action for volume changes?) but its not working correctly. following issues i am facing
1) If I press the volume button once - the event is triggered 4-6 times.
2) If current volume is maximum and i increase the volume then event doesn't fire..
Please help me.
Try to use following code -
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
for background - any way to detect volume key presses or volume changes with android service?
On a rooted device, you can use the Xposed framework to hook yourself into PhoneWindowManager. A good example is the Xposed Torch Module. Just decompile it and see how they do things.
You can detect this by polling AudioManager for current volume, it's not nice solution, but i don't know better.
private Handler handler;
public int volume;
private Runnable volumeUpdater = new Runnable() {
private int updatesInterval = 100;
#Override
public void run() {
if(volume != audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)){
//volume changed put logic here
}
handler.postDelayed(volumeUpdater, updatesInterval);
}
};

how to hide a media controller when a video starts in android?

I'm trying to play a video with the media controller sets to invisible, so when I override the back key, the video stops and the activity finishes. But, when I press the back button before spending three seconds, I need press the button twice, because the media controller is activated. So, how to hide the media controller when the video starts?
private void playRecording() {
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(output_file_name);
video_view.start();
mc.show(0);
mc.hide();
}
private void stopPlayingRecording() {
video_view.stopPlayback();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (is_playing) {
stopPlayingRecording();
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Once the video is started you can try.
video_view.setMediaController(null);
then if you want to show it again when a user presses on the screen you could implement a ontouchevent that will create one and show it for a few seconds and then set it back to null again

Volume preference - blocking volume keys

I have seen couple of similar problems with solutions, but I couldn't find one that would work in my situation.
I am making VolumePreference (extending DialogPreference) that let's user choose volume level for some alarm.
In other preference user chooses desired ringtone that is played during alarm. It is also played while user is choosing volume in VolumePreference, so he knows how it actually sounds.
In yet another preference user chooses if alarm should "override" phone's media volume level when playing - I do that, so if user wants to have fixed volume level for alarm, then it shouldn't be affected by changes made by volume keys and so on.
If user chooses to do that, before starting to play alarm in AlarmActivity, I set volume to max level with AudioManager and intercept all keyDown events of volume keys, restoring volume level after alarm finishes.
Problem is, I can't block volume keys within my VolumePreference as there is no onKeyDown method.
After some checking, I found registerMediaButtonEventReceiver method of AudioManager that "Register a component to be the sole receiver of MEDIA_BUTTON intents.", which I believe could help in my situation (making some empty receiver), and even make volume locking more universal (register when I want to start lock, unregister after unlock), but it is working from API8, while I am making app for API7 - which still hold over 10% of market from what I read, so I would like to stick to it.
Any ideas on how one could block volume changes in PreferenceDialog?
After some thinking solution proved to be really simple - one can override onKeyDown method of View created in onCreateDialogView of DialogPreference or set onKeyListener of that View.
First example:
LinearLayout layout = new LinearLayout(mContext)
{
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyUp(keyCode, event);
}
};
Second example (mDialogView is saved reference to layout from first example):
mDialogView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
return false;
}
});
I have chosen second solution, as I think it is more flexible for two reasons at least
I can just remove listener to stop blocking volume keys, while in first method I can't
in first solution I need to decide if I want to block keys input while creating View - not much use if VolumePreference is extending some other Preference that shouldn't block keys input

android WebView stop Flash plugin onPause

I have a WebView that contains a html file that contains a Flash plugin (a video).
When the user presses play on the Flash plugin, the video plays fine.
However when the user closes the app or moves to a new Activity, the Flash is still playing in the background. Going back to the view appears to create a new thread with the Flash plugin running on it.
How can we stop the Flash plugin running onPause?
Thanks
I ran into the same issue. The following worked for me:
#Override
protected void onDestroy() {
super.onDestroy();
final WebView webview = (WebView)findViewById(R.id.webPlayer);
// Calling .clearView does not stop the flash player must load new data
webview.loadData("", "text/html", "utf-8");
}
try this.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
webview.goBack();
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
//webview=null;
webview.reload();
}
return super.onKeyDown(keyCode, event);
}
Pavan

Categories

Resources