I have an activity that has a VideoView that is asynchronously preparing a video:
Uri mUri = "uri to streaming video"
VideoView mVideoView = (VideoView) rootView.findViewById(R.id.videoView);
mVideoView.setOnErrorListener(this);
mVideoView.setOnCompletionListener(this);
mVideoView.setVideoURI(mUri);
mVideoView.setMediaController(null);
mVideoView.setOnPreparedListener(this);
While it is "preparing" I show a ProgressDialog... if I press the back button during this state the following error is printed to ADB and the activity crashes silently with a short wait at a black screen:
E/MediaPlayer( 2204): stop called in state 4
E/MediaPlayer( 2204): error (-38, 0)
W/ActivityManager( 59): Activity pause timeout for HistoryRecord{45080368 com.myapp.VideoPlayerActivity}
What is the best way to stop a VideoView from preparing a video so you can exit an activity?
Note: I don't have access to the actual MediaPlayer object until the callback for the video being prepared is called:
#Override
public void onPrepared(MediaPlayer player)
... which hasn't happened while the MediaPlayer/VideoView is "preparing".
I have not tested it my self but you should be able to reset() the MediaPlayer when you are in preparing state.
Try calling MediaPlayer.prepare() before doing MediaPlayer.stop() when back button is pressed (implement onPause or onStop activity method)
Related
I am using a MediaPlayer inside a Fragment, I have managed to stop the music from playing, whenever the current fragment is changed.
But the problem is that, if I was within the fragment, and I press the back button to close the application, the music keeps streaming.
My code is the following:
#Override
public void setUserVisibleHint (boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
If (this.isVisible ()){
If (!isVisibleToUser){
sound.stop ();
}else {
sound.start ();
}
}
}
sound = MediaPlayer.create(getActivity
(), R.raw.music);
How can I stop the music from playing, when I exit the application?
You can stop your MediaPlayer from streaming in onPause() method of the fragment.
This will stop the mediaplayer when you open the other fragment or you destroy the fragment
I have a MediaPlayer that streams music in the background, when I press the home button the music stops, which what I want, but when I resume back to the application, the music is not there anymore. And when the current track ends, it doesn't loop.
My code is the following:
MediaPlayer backgroundMusic;
int length = 0;
//play background music
backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.background_music);
backgroundMusic.start();
backgroundMusic.setLooping(true);
#Override
protected void onPause() {
super.onPause();
if(backgroundMusic.isPlaying()){
backgroundMusic.pause();
length = backgroundMusic.getCurrentPosition();
}else{
return;
}
}
public void onPrepared(MediaPlayer backgroundMusic){
backgroundMusic.start();
backgroundMusic.seekTo(length);
}
How can I resume the music from where it stopped, when I press the Home Button, and Also make the music loop.
Thanks in advance.
Try to implement your code with service class if you want your player to play even in background or if you want to store the position save your data in static variable in onpause and restore it onResume of your Activity.
Preserve position of playing in shared preferences and not in local variable
I want my button to be "spammable". This means that if I tap on the button repeatedly the MediaPlayer starts all over again.
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (firstTextView.getText().equals("Hello world!")) {
firstTextView.setText("You clicked!");
} else {
firstTextView.setText("Hello world!");
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.start();
}
});
When I interrupt the MediaPlayer when it is playing, it stops and never starts again. Why?
EDIT: The problem is that I called stop(). Thanks for pointing that out.
As per the documentation, you need to re-prepare the MediaPlayer (emphasis mine):
Once in the Stopped state, playback cannot be started until
prepare() or prepareAsync() are called to set the MediaPlayer object
to the Prepared state again.
Seems you are stopping the player because you are calling mediaPlayer.stop() this makes the MediaPlayer state to go in Stopped state. It will continue to play again when you call prepare() or prepareAsync() and has its preparation callback fired to start the playing media.
I'm playing video from raw folder. Generally once its completed it remains in same activity. how to return back to previous activity automatically without pressing back button?
Are you using a VideoView to play the video in your 2nd activity? If so, you can use the OnCompletion event to call finish() on the activity, which will return you to the 1st activity.
Something like this should work:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
Log.i("VideoView", "onCompletion()");
finish();
}
});
Just be sure to replace R.id.videoView with the actual id in your layout file.
Hope that helps!
Use MediaPlayer.OnCompletionListener to Listen when Video Playing in finished in Activity 2 and start Previous activity on onCompletion as :
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
//start Previous Activity here
Current_Activity.this.finish();
}
}); // video finish listener
After playing a video onCompletion() method is invoked, in that just call finish() method in it.
I am working in android. I am creating a mediaPlayer which is running audio files. i have 10 buttons. i have assigned different url to each button. So when i press button1 then song of url with respect to button 1 is playing. and then i click on 2nd button then song of button 2 is also playing with song 1. but i want to stop song of button 1 when i press button 2.
this is the code i am using for this functionality:-
public void onClick(View v)
{
MediaPlayer mediaPlayer=new MediaPlayer();
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
int i = Integer.parseInt((v.getTag()).toString());
String str=urls[i];
try {
mediaPlayer.setDataSource(str);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
please check my code and let me know what is mistake done by me.
you have construct a new MediaPlayer object each time the user click you view
how could it be in the running state !!!
calling release() method on a MediaPlayer object it is in thre End state
Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
but in case you want to reuse a MediaPlayer object you should call the
call the following method in the same order
reset()
make the mediaPlayer enter the Idle state
setDataSource()
set your data source note : the mediaplayer shoud be in the idle state
prepare()
start()