I am using a videview to play a video in my fragment when i press the phone home button while video playing and bring the app to foreground again video stops.What is guess is onPause of activity is called that makes the fragment state to reinitialize of some similar reason.Kindly suggest some solution.
Regards:Ali
Related
I want to stop media player to what it is and go on that fragment guided by button in below and again while returning i want to get same paused media playerenter image description here
You can achieve that by following below approach
1) Create Media Player instance on Activity rather on Fragment
By this on whatever fragment you are you can get Media Player instance at any time without worrying the actual fragment
I want to stop media player to what it is and go on that fragment guided by button in below
2) On clicking stop button(as you mentioned, I am expecting this button on any fragment) get the instance of Media Player and call Media Player stop method.
and again while returning i want to get same paused media player
3) When you get back the same fragment, you will already have the same instance of Media Player. And then you can do you desire operation
I hope that helps you
I have an audio playing in the background when I am clicking onto a new activity. I want while I am clicking to the next activity, for the audio, to stop.
However, when going back to the original activity I want the audio to start again from the beginning.
Using media player and Android Studio. Here's all the media player code I have so far:
MediaPlayer mediaPlayer = MediaPlayer.create(pagetwo.this,R.raw.audio1);
mediaPlayer.start();
You can use the basic lifecycle methods of your Activity to handle this use case:
Android will call onPause() if you open a new Activity and will call onResume() if you enter/come back to the first activity.
If you call mediaPlayer.start() in onResume() and mediaPlayer.stop() in onPause() you should have the desired behavior.
I have created an application that has 2 activities A and B.
From activity A I have a button to call B, in that B I have MediaPlayer to play a song list. When I press back button to back to A, then I press the button to call B again. All of my information's gone but the last Media Player keep playing, when I try to play another song, both Media play as the same. My problem is I want to keep the music playing when I press back button to A, and then I go back to B from A and play other songs without create new Media or just continue the last activity using the last Media Player.
check State diagram of the MediaPlayer. That should answer your problem. Read the javadoc about how and when to stop the player.
I would suggest you maintain a global MediaPlayer object in your Activity B.
public static final MediaPlayer player = new MediaPlayer():
In your activity B, whenever you make any change to the player just call
player.stop() or player.reset();
before starting the audio session.
i'm using media player to display .m3u8 streaming in my application,
when pressing Home button ,,or when the screen Goes off..i want to pause the media then when the user return to it ..it start playing..
I tried onPause and onResume to pause and start the Media player..
But it Gives a nullPointerExc on the onResume Method.
In onPause, save whatever metadata you need in order to reload the media into the MediaPlayer (file location, current position, etc).
In onResume, check the MediaPlayer instance for null. If it's null, reload your media from this information. Where you save this data is up to you, but SharedPreferences is probably the easiest to get started with.
I have an activity with MediaPlayer and Button. The MediaPlayer contains a VideoView (using MediaPlayer.getDisplay(VideoView.getHolder())). When the button is pressed it pause the MediaPlayer and open a browser. returning to app suppose to continue the video (MediaPlayer .start())
When the activity started the video is plays and shown as expected but when i press the button and then I press return to the app the video is not shown but the sound is working.
Any idea how to make the video visible?
-Z