2011-03-31 174 views
4

我正在使用媒体播放器来传输视频。它只播放音频而不播放视频。谁能帮忙?我的代码如下。安卓媒体播放器显示音频,但没有视频

public class VideoViewApplication extends Application { 

    @Override 
    public void onCreate() { 
    } 

    @Override 
    public void onTerminate() { 
    } 
} 


public class VideoViewDemo extends Activity implements 

    OnErrorListener,OnBufferingUpdateListener, OnCompletionListener, 
    MediaPlayer.OnPreparedListener, SurfaceHolder.Callback { 

    private static final String TAG = "VideoViewDemo"; 

    private MediaPlayer mp; 
    private EditText mPath; 
    private SurfaceHolder holder; 

    private ImageButton mPlay; 
     private ImageButton mPause; 
    private ImageButton mReset; 
    private ImageButton mStop; 
    private String current; 
    private SurfaceView mPreview; 


    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     mPreview = (SurfaceView) findViewById(R.id.surface); 

     mPath = (EditText) findViewById(R.id.path); 
     mPath.setText("rtsp://video2.americafree.tv/AFTVHorrorH26496.sdp"); 

     mPlay = (ImageButton) findViewById(R.id.play); 
     mPause = (ImageButton) findViewById(R.id.pause); 
     mReset = (ImageButton) findViewById(R.id.reset); 
     mStop = (ImageButton) findViewById(R.id.stop); 


     mPlay.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       playVideo(); 
      } 
     }); 
     mPause.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mp != null) { 
        mp.pause(); 
       } 
      } 
     }); 
     mReset.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mp != null) { 
        mp.seekTo(0); 
       } 
      } 
     }); 
     mStop.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mp != null) { 
        current = null; 
        mp.stop(); 
        mp.release(); 
       } 
      } 
     }); 

     // getWindow().setFormat(PixelFormat.TRANSPARENT); 
     holder = mPreview.getHolder(); 
     holder.addCallback(this); 
     holder.setFixedSize(100, 100); 

     runOnUiThread(new Runnable(){ 
      public void run(){ 
       playVideo(); 
      } 
     }); 
    } 

    private void playVideo() { 
     try { 
      final String path = mPath.getText().toString(); 
      Log.v(TAG, "path: " + path); 

      if (path.equals(current) && mp != null) { 
       mp.start(); 
       return; 
      } 
      current = path; 

      mp = new MediaPlayer(); 
      mp.setOnErrorListener(this); 
      mp.setOnBufferingUpdateListener(this); 
      mp.setOnCompletionListener(this); 
      mp.setOnPreparedListener(this); 
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      mp.setScreenOnWhilePlaying(true); 

      mp.setDisplay(mPreview.getHolder()); 
      mp.setDataSource(path); 
      mp.prepare(); 

      Log.v(TAG, "Duration: ===>" + mp.getDuration()); 
      mp.start(); 

     } catch (Exception e) { 
      Log.e(TAG, "error: "+ e.getMessage(), e); 
      if (mp != null) { 
       mp.stop(); 
       mp.release(); 
      } 
     } 
    } 

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
     Log.d(TAG, "surfaceChanged called"); 
    } 

    public void surfaceCreated(SurfaceHolder arg0) { 
     Log.d(TAG, "surfaceCreated called"); 
    } 

    public void surfaceDestroyed(SurfaceHolder arg0) { 
     Log.d(TAG, "surfaceDestroyed called"); 
    } 

    public void onPrepared(MediaPlayer arg0) { 
     Log.d(TAG, "onPrepared called"); 
    } 

    public void onCompletion(MediaPlayer arg0) { 
     Log.d(TAG, "onCompletion called"); 
    } 

    public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) { 
     Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent); 
    } 

    public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { 
     Log.e(TAG, "onError---> what:"+what+" extra:"+extra); 
     if (mediaPlayer != null) { 
      mediaPlayer.stop(); 
      mediaPlayer.release(); 
     } 
     return true; 
    } 


} 



<?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" 
     > 
    <EditText android:id="@+id/path" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
      /> 
    <SurfaceView 
      android:id="@+id/surface" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
    </SurfaceView> 

    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      > 
     <ImageButton android:id="@+id/play" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:src="@drawable/play"/> 
     <ImageButton android:id="@+id/pause" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:src="@drawable/pause"/> 
     <ImageButton android:id="@+id/reset" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:src="@drawable/reset"/> 
     <ImageButton android:id="@+id/stop" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:src="@drawable/stop"/> 
    </LinearLayout> 
</LinearLayout> 
+0

如果您试图在模拟器上运行这个,这是正常的行为。仿真器无法在大多数开发机器上渲染视频。对于测试视频应用程序,你真的需要一个设备。 – CommonsWare 2011-04-01 00:06:41

+0

我在三星银河标签上运行它。我怀疑这与布局xml和持有者大小有关。 – Vicky 2011-04-01 00:08:18

+0

如果设备无法完全处理视频的编码细节,有时会发生这种情况。作为测试,拍摄一小段主题视频,将其加载到设备上,然后用播放器播放。 – 2011-04-29 04:59:48

回答

11

我有这个问题,并通过使用此折旧方法设置类型来解决它。

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

值得一试,如果它有效,你可以调查为什么类型不会自动设置,因为它应该是。

+0

就是这样。答案应该被接受 – znat 2012-12-06 15:35:37

2

我有同样的问题,这是由于表面没有准备好播放视频。

您应该尝试从surfaceCreated()处理函数调用playVideo()。

public void surfaceCreated(SurfaceHolder holder) { 
     runOnUiThread(new Runnable(){ 
      public void run(){ 
       playVideo(); 
      } 
     }); 
} 
+0

我有同样的问题,这个解决方案为我工作。非常感谢!! – Steve 2012-01-13 14:47:43

0

我有这个问题,在我的释放功能可见=去surfaceview加入解决了这个问题:

public void release() { 
    if (mMediaPlayer != null) { 
    setVisibility(View.GONE); 
     mMediaPlayer.reset(); 
     mMediaPlayer.release(); 
     mMediaPlayer = null; 
     mCurrentState = STATE_IDLE;} 
    } 

和onprepared功能设置可见=可见:

videoView.setOnPreparedListener(new OnPreparedListener() { 
      public void onPrepared(MediaPlayer mp) { 
       audio=false; video=false; int ty=mp.getTrackInfo().length; 
       for (int i=0; i<ty;i++) 
       { 
        if (mp.getAudioTrack()>-1) {audio=true;} 
        if (mp.getVideoTrack()>-1) {video=true;} 
       } 

       if (((audio==false)&&(skip==true))||((video==false)&&(skip2==true))||((video==true)&&(skip4==true))) 
       { notifybar("..."); 
        nexttr();} else { 
       if (vis==true) { 
       if (video==false) { 
        if (mVisualizerView.getVisibility()!=View.VISIBLE) {mVisualizerView.setVisibility(View.VISIBLE);} 
         mVisualizerView.link(videoView.getAudioSessionId()); 
         vis2=true; 
         } else if (vis2==true){ 
         mVisualizerView.release(); 
         mVisualizerView.setVisibility(View.GONE); 
         vis2=false; 
         }} 
       //this 
       if (video==true) { 
       if (videoView.getVisibility()!=View.VISIBLE) {videoView.setVisibility(View.VISIBLE);} 
       } else {if (videoView.getVisibility()!=View.INVISIBLE) {videoView.setVisibility(View.INVISIBLE);} 
       }