`
jiapumin
  • 浏览: 344404 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java.io.IOException: Prepare failed.: status=0x1异常解决方法

阅读更多
在参考例子做视频播放的时候出现如下异常:
 error (1, -2147483648)
java.io.IOException: Prepare failed.: status=0x1
     at android.media.MediaPlayer.prepare(Native Method)
     at com.android.MainActivity.onClick(MainActivity.java:56)
     at android.view.View.performClick(View.java:2486)
     at android.view.View$PerformClick.run(View.java:9122)
     at android.os.Handler.handleCallback(Handler.java:587)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:130)
     at android.app.ActivityThread.main(ActivityThread.java:3694)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:507)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
     at dalvik.system.NativeStart.main(Native Method)
 setDataSource called in state 8

源码如下:
package com.mediaplayer_video;

import java.io.File;
import java.io.FileInputStream;

import com.android.R;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener,
		SurfaceHolder.Callback {
	/** Called when the activity is first created. */
	private Button buttonStart;
	private Button buttonStop;
	private SurfaceHolder surfaceHolder;
	private MediaPlayer mediaPlayer;
	private SurfaceView surfaceView;
	private boolean isPause = false;
	private String strPath = "/sdcard/aa.3gp";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		buttonStart = (Button) findViewById(R.id.buttonStart);
		buttonStart.setOnClickListener(this);
		buttonStop = (Button) findViewById(R.id.buttonStop);
		buttonStop.setOnClickListener(this);

		surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setFixedSize(176, 144);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mediaPlayer = new MediaPlayer();
	}

	@Override
	public void onClick(View v) {
		if (v == buttonStart) {
			isPause = false;
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.reset();
			}
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setDisplay(surfaceHolder);

			try {

				mediaPlayer.setDataSource(strPath);
								mediaPlayer.prepare();
			} catch (Exception e) {
				e.printStackTrace();
			}
			mediaPlayer.start();
		} else if (v == buttonStop) {
			if (isPause == false) {
				mediaPlayer.pause();
				isPause = true;
				System.out.println("mediaPlayer pause!");
			} else {
				mediaPlayer.start();
				isPause = false;
			}
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}
}

xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<SurfaceView android:id="@+id/surfaceView"
		android:layout_width="fill_parent" android:layout_height="400dip">
	</SurfaceView>
	<LinearLayout android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<Button android:id="@+id/buttonStart" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="播放">
		</Button>
		<Button android:id="@+id/buttonStop" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="停止">
		</Button>
	</LinearLayout>
</LinearLayout>

查了查资料和网上的解决办法如下:
mp.setDataSource(PATH_TO_FILE);
    mp.prepare();
这个地方修改如下:
File file = new File(strPath);
FileInputStream fis = new FileInputStream(file);
mediaPlayer.setDataSource(fis.getFD());
mp.prepare();
或者
把mediaPlayer.prepare;改成
mediaPlayer.prepareAsync();
这种情况会报个prepare called in state 8错误
分享到:
评论
4 楼 夜色蓝 2014-02-27  
也遇到同样的问题,按照楼主的方法,问题已解决。
3 楼 zwyhhmm 2013-07-03  
音乐文件如果在SD卡里要用绝对地址,如:/mnt/sdcard/gauss.mp3;
如果在项目的资源文件raw里,那面就用R.raw.file读取;
如果是网络地址就这样:mediaPlayer.setDataSource(Url);需要添加访问网络权限;这三种情况我都试过,是可行的,如果还有错误那就应该检查一下代码了;
2 楼 tianshengwo123 2012-08-15  
也出现了同样的问题,同问!
1 楼 yangxiaoliang123 2012-03-22  
楼主你好,如果我要读取服务器的地址播放歌曲和视频该怎么做呢!

相关推荐

Global site tag (gtag.js) - Google Analytics