how to hide a media controller when a video starts in android? - 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

Related

Why does my webview finish unexpectedly?

I place a fragment with a webview widget to load my webpages and create a host fragmentActivity as my webpages' container. Now that there is a page, including a media player, seems like to support various of content for us. Here comes the problem, if I click the play button, the media player works well itself, however, when i adjust the power of volume, either higher or lower, the host activity finish unexpectedly. Complementally, I just handle the back event in the method of onKeyEvent.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(webview.canGoBack()){
webview.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Anybody helps me?

Android mediacontroller pauses when display turns off?

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

Android back button and MediaController

I know how to take control of the back button. I have a VideoView embedded in a FrameLayout. My question is when the video pops up, the video controls are present for a few seconds. Hitting the back button while they are visible hides the video controls. Is there a way to ignore that function and do the next back action as if the video controls weren't visible?
The reason I ask is if I really do want to go back, I must hit the back button twice; once to hide the controls and second to actually go back
Based on the source code, this should work:
Extend MediaController (for the purposes of this answer, call it RonnieMediaController)
Override dispatchKeyEvent() in RonnieMediaController
Before chaining to the superclass, check for KeyEvent.KEYCODE_BACK, and if that is encountered, tell your activity to finish()
Use RonnieMediaController instead of MediaController with your VideoView
Personally, I'd just leave it alone, as with this change your user cannot make a RonnieMediaController disappear on demand.
You can simply write:
mVideoView.setMediaController(new MediaController(this){
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
((Activity) getContext()).finish();
return super.dispatchKeyEvent(event);
}
});
No need to create new class.
The previous solutions no longer work with Android Pie +, you must instead :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mediaController.addOnUnhandledKeyEventListener((v, event) -> {
//Handle BACK button
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
{
mediaController.hide(); //Hide mediaController,according to your needs, you can also called here onBackPressed() or finish()
}
return true;
});
}
You can also have the Activity handle the event:
mVideoView.setMediaController(new MediaController(this){
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
((Activity) getContext()).onBackPressed();
return true;
}
}
return super.dispatchKeyEvent(event);
}
});
Then handle it in your Activity:
#Override
public void onBackPressed() {
// clean up or send result here
finish();
}
In Xamarin.Android, you can deal with this problem like this
public class CustomMediaController : MediaController
{
private FragmentActivity act;
public CustomMediaController(Context context, FragmentActivity myActivity) : base(context)
{
act = myActivity;
}
public override bool DispatchKeyEvent(KeyEvent e)
{
if(e.KeyCode == Keycode.Back)
{
act.OnBackPressed();
}
return base.DispatchKeyEvent(e);
}
}

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

Android: Stop playback on Home or Back button

When I press back or home in my application, it hides, but the MediaPlayer keeps going. Is there a way to know when these buttons have been pressed and stop playback before closing?
just implement this method in your class where media player is calling...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{ //Back key pressed
//Things to Do
if(mediaPlayer!= null)
{
mediaPlayer.stop();
mediaPlayer=null;
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
You should be able to override the onPause() function in your Activity. onPause() will be called when you Activity is hidden and you can pause the MediaPlayer there.
For example,
#Override
protected void onPause() {
super.onPause(); // Don't forget this line
myMediaPlayer.pause() // Or whatever the function is to pause it
}
Don't forget to add super.onPause; if you forget it will cause a FC.

Categories

Resources