在安卓手机上运行的时候会出现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();@Overrideprotected 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();}}@Overridepublic 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;}}@Overrideprotected void onDestroy() {super.onDestroy();if (mediaPlayer != null) {mediaPlayer.stop();mediaPlayer.release();}}}
2 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
你把onDestroy中的mediaPlayer.stop();和mediaPlayer.release();放到super.onDestroy()前面试试!
噜噜哒
TA贡献1784条经验 获得超7个赞
1、确定AndroidManefest.xml中添加了以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、去应用中手动开启存储控件的这个权限!
虽然第一步添加了权限,但是我的模拟机是7.0的,需要手动开启下权限。这个很重要。
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消