当前位置: 代码迷 >> Android >> 初学android开发,视频播放有关问题
  详细解决方案

初学android开发,视频播放有关问题

热度:113   发布时间:2016-05-01 21:46:48.0
初学android开发,视频播放问题
想在手机上播放一段视频,无奈一直报错,代码如下
animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <SurfaceView android:id = "@+id/surface"
  android:layout_width = "wrap_content"
  android:layout_height = "wrap_content"
  android:visibility="visible" 
  android:layout_centerVertical="true" 
  android:layout_centerHorizontal="true">
  </SurfaceView>
</RelativeLayout>





animation.java:

package wood.recorder.animation;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Animation extends Activity implements OnCompletionListener {
  /** Called when the activity is first created. */
SurfaceView mPreview;
SurfaceHolder holder;
String path;
MediaPlayer mPlayer;

  @Override 
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.animation);
mPreview = (SurfaceView)findViewById(R.id.surface);
holder = mPreview.getHolder();
path = "/sdcard/title.mp4";
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(path);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOException");
e.printStackTrace();
}
mPlayer.setOnCompletionListener(this);
mPlayer.setDisplay(holder);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();


   
  }

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.pause();
mp.stop();
mp.release();
// mp.reset();

}

@Override
public void onStop(){
mPlayer.stop();
super.onStop();
}
}
DDMS报错情况如下:
07-08 09:05:33.818: ERROR/PlayerDriver(31): Command PLAYER_PREPARE completed with an error or info PVMFErrResourceConfiguration
07-08 09:05:33.818: ERROR/MediaPlayer(242): error (1, -16)
07-08 09:05:33.829: INFO/System.out(242): IOException
07-08 09:05:33.829: WARN/System.err(242): java.io.IOException: Prepare failed.: status=0x1
07-08 09:05:33.838: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
07-08 09:05:33.848: WARN/System.err(242): at android.media.MediaPlayer.prepare(Native Method)
07-08 09:05:33.848: WARN/System.err(242): at wood.recorder.animation.Animation.onCreate(Animation.java:30)
07-08 09:05:33.848: WARN/System.err(242): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 09:05:33.848: WARN/System.err(242): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-08 09:05:33.848: WARN/System.err(242): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-08 09:05:33.848: WARN/System.err(242): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
  相关解决方案