Android MediaPlayer错误(1,-38)和(-38,0)

时间:2015-02-23 11:11:09

标签: android media-player android-mediaplayer

我正在使用MediaPlayer组件,我收到错误(1,-38)和(-38,0)。我正在使用player.prepareAsync(),我在onPrepared()方法中调用了player.start()。奇怪的是,当我第四次复制视频时,我收到错误。我不知道发生了什么。有人可以帮帮我吗?

这是我的代码:

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;
}

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)

0 个答案:

没有答案