I am working with the MediaPlayer component and I am getting the errors (1, -38) and (-38, 0). I am using player.prepareAsync() and I call player.start() in the onPrepared() method. The curious is that I am getting the error when I reproduce a video for fourth time. I have no idea about what is happening. Anyone can help me?
This is my code:
public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, VideoControllerView.MediaPlayerControl, OnCompletionListener, OnErrorListener {
SurfaceView videoSurface;
MediaPlayer player;
VideoControllerView controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
if(player != null) {
player.release();
player = null;
}
player = new MediaPlayer();
controller= new VideoControllerView(this);
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
String path = getIntent().getStringExtra(Constants.PATH);
player.setDataSource(path);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
} catch(Exception e) {
e.printStackTrace();
}
videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
player.prepareAsync();
}
@Override
protected void onStop() {
super.onStop();
if(player != null) {
player.stop();
}
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(player != null) {
player.stop();
}
finish();
break;
default:
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
controller.show();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(this);
controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
player.start();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onCompletion(MediaPlayer mp) {
if(!controller.isStopped()) {
finish();
Intent intent = new Intent(getBaseContext(), MediaListActivity.class);
String path = getIntent().getStringExtra(Constants.PATH);
intent.putExtra(Constants.PATH, path.substring(0, path.lastIndexOf(Constants.SLASH)));
startActivity(intent);
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if(what == 100 || what == 1 || what == -38) {
player.stop();
player.release();
player = null;
finish();
}
return false;
}
And the LogCat:
02-23 12:00:39.184: E/MediaPlayer(31647): error (1, -38)
02-23 12:00:39.184: E/MediaPlayer(31647): Error (1,-38)
02-23 12:00:39.184: E/MediaPlayer(31647): stop called in state 0
02-23 12:00:39.184: E/MediaPlayer(31647): error (-38, 0)