如下,关于android start called in state 0的问题

在安卓手机上运行的时候会出现error:Tag:MediaPlayer Text:start call in state 0。
这是什么问题啊,觉得应该不是代码问题,但又找不出问题在哪,求大神指教。
代码如下:
package com.example.playaudiotest;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private Button play;

private Button pause;

private Button stop;

private MediaPlayer mediaPlayer = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initMediaPlayer();
}

private void initMediaPlayer() {
try {
File file = new File(Environment.getExternalStorageDirectory(), "Over the horizon.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}

}

繁星点点滴滴
浏览 116回答 2
2回答

慕容3067478

protected void onDestroy() {super.onDestroy();if (mediaPlayer != null) {mediaPlayer.stop();mediaPlayer.release();}}你把onDestroy中的mediaPlayer.stop();和mediaPlayer.release();放到super.onDestroy()前面试试!

噜噜哒

1、确定AndroidManefest.xml中添加了以下权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>2、去应用中手动开启存储控件的这个权限!虽然第一步添加了权限,但是我的模拟机是7.0的,需要手动开启下权限。这个很重要。
打开App,查看更多内容
随时随地看视频慕课网APP