MediaPlayer:在状态0错误中启动调用

时间:2013-04-23 12:36:12

标签: android android-mediaplayer

这就是代码的样子,logcat一直声明“在状态0中调用” 我尝试了不同的方式来启动视频。我错过了什么?我也试过一个OnPreparedListener,但它一直给我同样的错误。

package com.example.videoplaybacktests;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements SurfaceHolder.Callback{

MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;;

String stringPath = "/drawable/visource.mp4";
public String RTSP = "rtsp://v2.cache7.c.youtube.com/CjYLENy73wIaLQkFPOlBtC4qExMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJeOw_HjwJ6bUQw=/0/0/0/video.3gp"; 
public Uri vidURI = Uri.parse(RTSP);
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
     Button buttonPauseVideo = (Button)findViewById(R.id.pausevideoplayer);

     getWindow().setFormat(PixelFormat.UNKNOWN);
     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();

     buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 pausing = false;

 if(mediaPlayer.isPlaying()){
  mediaPlayer.reset();
 }

 mediaPlayer.setDisplay(surfaceHolder);

 try {
     mediaPlayer.setDataSource(RTSP);
     mediaPlayer.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
  e.printStackTrace();
 }
mediaPlayer.start();

}});


     buttonPauseVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 if(pausing){
  pausing = false;
  mediaPlayer.start();
 }
 else{
  pausing = true;
  mediaPlayer.pause();
 }
}});

 }



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

}

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

}

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

}
}

2 个答案:

答案 0 :(得分:1)

如果没有准备好mediaPlayer,你就无法召唤开始!这就是你得到错误的原因
请改用prepareAsync

   try {
         mediaPlayer.setDataSource(RTSP);
         mediaPlayer.prepareAsync();

     } 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
      e.printStackTrace();
     }

并在onPrepared()上开始播放:

public void onPrepared(MediaPlayer mp) {
     mediaPlayer.start();  
}

如果它仍未启动onVideoSizeChanged

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  mediaPlayer.start();
}

答案 1 :(得分:1)

package com.example.videoplaybacktests;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements SurfaceHolder.Callback{

MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;;

String stringPath = "/drawable/visource.mp4";
public String RTSP = "rtsp://v2.cache7.c.youtube.com/CjYLENy73wIaLQkFPOlBtC4qExMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJeOw_HjwJ6bUQw=/0/0/0/video.3gp"; 
public Uri vidURI = Uri.parse(RTSP);
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
     Button buttonPauseVideo = (Button)findViewById(R.id.pausevideoplayer);

     getWindow().setFormat(PixelFormat.UNKNOWN);
     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();

     buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub


 if(mediaPlayer.isPlaying()){
  mediaPlayer.stop();
 }

 mediaPlayer.setDisplay(surfaceHolder);

 try {
     mediaPlayer.setDataSource(RTSP);
     mediaPlayer.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
  e.printStackTrace();
 }

}});


     buttonPauseVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 if(mediaPlayer==null)
return;
else if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}else{
mediaPlayer.start();
}
}});

 }



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

}

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


}

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

}
}