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
Related
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?
When MediaPlayer is preparing to start, i want go back to "activity main" but my application is crash.
I try this code, but not working :s
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
seekHandler.removeCallbacks(run, null);
}
}
return super.onKeyDown(keyCode, event);
}
Just a hunch, but try removing your seek callbacks before calling release.
You should add logcat output and the code where the error occurs to get better answers.
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
I currently have a mediaplayer streaming some mp3 files. What I would like to do is if the User presses the home button to exit the app, the music stops playing. how would I go about doing this?
In your app' onPause() or onDestroy() call mediaPlayer.stop().
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "home button pressed"); }
return super.onKeyDown(keyCode, event); }
Something like this, except instead of log, do whatever you do in your implementation to kill the service/asynctask/thread handling your music.
Add this method to your class to override your home button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
media.stop();
finish();
//or complete end your application
//System.runFinalizersOnExit(true);
//System.exit(0);
return true;
}
return false;
}
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);
}
}