NPike
NPike

Reputation: 13254

MediaPlayer: stop called in state 4

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".

Upvotes: 6

Views: 8724

Answers (2)

frayab
frayab

Reputation: 2512

Try calling MediaPlayer.prepare() before doing MediaPlayer.stop() when back button is pressed (implement onPause or onStop activity method)

Upvotes: 1

tidbeck
tidbeck

Reputation: 2398

I have not tested it my self but you should be able to reset() the MediaPlayer when you are in preparing state.

Upvotes: 2

Related Questions