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 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>