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文件如下:


android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content" android:text="@string/hello" />
android:layout_width="fill_parent" android:layout_height="400dip">

android:layout_height="wrap_content">





查了查资料和网上的解决办法如下:
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错误

你可能感兴趣的:(android开发,Java,Android,OS,XML)