My Activity contains webView. Sometimes webView contains video. When watching the video in webView and press the back button, I go on to another Activity, but the video continues to play and I hear the sound of video. How can I solve this problem? Thank you in advance.
Couple different options:
if you want to completely stop the video, make sure you call finish() when clicking back.
You can do that like this on your Activity:
#Override
public void onBackPressed() {
this.finish();
}
Activity will be destroyed and video will stop.
If you want to stop the video but not destroy the activity, edit your question and show some of your code so I can give you more options.
This is a solution that works for me: How to stop youtube video playing in Android webview?
mWebView.onPause();
Related
I am playing video on video view, the issue which i am facing is that when user press back button, the application gets stuck for some time and then onStop method is called of that activity, i don't know why the application is taking time in calling onStop method. Please can anyone help me out.
What jaydroider said is right. You have to override onBackPressed method as follows
#Override
public void onBackPressed() {
<stop your video here>
super.onBackPressed();
}
It is important that you stop the video before calling super.onBackPressed(); so that your application does not get stuck.
You should stop video on onPause() not on onBackPressed(). Because user may use the Home key while using your app. This will stop playing video whenever your screen is not visible to user.
I'm using a Webview, playing different html5 videos (internal tag and Youtube videos).
When closing the Webview I tried loading a blank page + hiding the view + pausing the webview. The Webview seems to be closed correctly and the video stops playing.
The problem is on Galaxy S4 + S2, when opening the Webview again and playing another video. The new video is been paused immediately after being played. When I press "play" again, the video is automatically paused again. This will happen with every video that I'll try to load (Youtube and internal ), until I'll kill my app. After killing my app everything will work normally again.
Is it possible that the first view/video is not terminated so each new video will automatically be paused, in order to prevent the two to play at the same time?
Tnx!
Yaniv
After struggling with this issue for few days, I found the solution. It appears that when closing the Webview, the Mediaplay probably did not have focus, which prevented him from pausing/dismissing. This prevented all other Webviews with Mediaplayers from playing (automatically paused each time trying to play, with no ability to resume).
When adding this code:
#Override
protected void onPause() {
super.onPause();
((AudioManager)getSystemService(
Context.AUDIO_SERVICE)).requestAudioFocus(
new OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {}
}, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
The problem was completely solved. The Mediaplayer was dismissed/paused (not sure which, have to check), and a new Webview with a new Mediaplayer can gain focus and play normally.
Hope it could help other people struggling with this issue..
I have a VideoView in one of my Activities and if I try to press the back Button it doesn't take me to the previous Activity and it doesn't give any error messages in the log cat either.
I have tried overriding the onBackPressed() method and I have tried calling the prev Activity using an Intent, but even then the back Button didn't do anything.
This problem only occurs when the video is playing and even after it is finished playing. The only time the back Button works is before the video starts playing. My VideoView also has a MediaController set to it.
Any ideas ?
Thanks!
From CommansWare
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.
Here is the link to the original post.
I have a problem that I want to call a User Defined function on Pause Button Pressed while playing a video file in our App, How can I do that? Please suggest me for right result.
Thanks in advance for any suggestions
VideoView v=new VideoView(this);
Take one Button to pause Video and then call
v.pause();
now after this you can do anything
I have an application with a list of video files with play button. When the user clicks play button, a separate activity starts through intent where the video is played. What I want is that when I click the back button when the video file finishes, I want the mainActivity to be refreshed (the mainActivity is the activity that started the activity for playing video file).
Any useful suggestions please??
Put your "refresh"-code in the Main-Activity's onStart-method.
See Androids Activity Lifecycle.
You could use startActivityForResult(Intent, int) and then handle the different results.