首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

java.io.IOException: Prepare failed. status=0x1错误解决办法

2012-08-26 
java.io.IOException: Prepare failed.: status0x1异常解决方法在参考例子做视频播放的时候出现如下异常:

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";@Overridepublic 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();}@Overridepublic 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;}}}@Overridepublic void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}@Overridepublic 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错误
1 楼 yangxiaoliang123 2012-03-22   楼主你好,如果我要读取服务器的地址播放歌曲和视频该怎么做呢!

热点排行