2013-08-04 36 views
0

我試圖在VideoView上使用MediaPlayer播放視頻。不幸的是我只能播放音頻。沒有顯示視頻。我需要使用Mediaplayer,因爲我的視頻處於受保護而不是Worldreadable位置(並且在播放之前需要複製大)並需要流式傳輸。Android MediaPlayer(使用VideoView)no視頻,僅限音頻播放

(視頻是好的,可以播放。在像宏碁A210這樣的Android平板電腦上,對於無法讀取的文件不太明智,我可以通過使用VideoViews setVideoURI方法直接播放視頻,我需要下面的代碼來播放視頻在例如三星片)

有人可以告訴我我做錯了什麼? Thx提前。

public class VideoPlayer extends Activity implements SurfaceHolder.Callback { 
private VideoView objVideoView; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    this.setContentView(R.layout.activity_videoplayer); 
    objVideoView = (VideoView) findViewById(R.id.myVideoView); 

    String strVideoNames = ""; 

    if (savedInstanceState == null) { 
     Bundle extras = getIntent().getExtras(); 
     if(extras != null) strVideoNames = extras.getString("strVideoNames"); 
    } else { 
     strVideoNames = (String) savedInstanceState.getSerializable("strVideoNames"); 
    } 

    playVideo(getVideoUrl(strVideoNames)); 
} 

ArrayList<String> listVideoNames; 

public void playVideo(String strVideoNames) { 

    if (strVideoNames.contains(";")) {   
     String[] strAVideoUrls = strVideoNames.split(";"); 
     listVideoNames = new ArrayList<String>(Arrays.asList(strAVideoUrls));   
    } else { 
     listVideoNames = new ArrayList<String>(); 
     listVideoNames.add(strVideoNames); 
    } 

    playVideoWithMediaPlayer(); 

} 

MediaPlayer objMediaPlayer; 

public void playVideoWithMediaPlayer() { 

    if (listVideoNames.size() > 0) {   
     try {   
      SurfaceHolder objSurfaceHolder = objVideoView.getHolder(); 

      objSurfaceHolder.addCallback(this); 
      objSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

      objMediaPlayer = new MediaPlayer(); 
     } catch (Exception e) { 
      alert(e.getMessage()); 
     } 
    } 
} 

public void surfaceCreated(SurfaceHolder holder) { 

    try {  
     File fileVideo = new File(getVideoUrl(listVideoNames.get(0))); 
     FileInputStream instreamVideo = new FileInputStream(fileVideo); 
     objMediaPlayer.setDataSource(instreamVideo.getFD()); 

     objMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      public void onCompletion(MediaPlayer _objMediaPlayer) { 
       listVideoNames.remove(0); 
       if (listVideoNames.size() > 0) { 
        playVideoWithMediaPlayer(); 
       } else { 
        _objMediaPlayer.release(); 
       } 
      } 
     }); 

     objMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      public void onPrepared(MediaPlayer _objMediaPlayer) { 
       Log.d("MediaPlayer","Prepared ..."); 
       objMediaPlayer.start(); 
      } 
     }); 

     objMediaPlayer.prepare(); 

    } catch (Exception e) { 
     alert(e.getMessage()); 
    } 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} 
public void surfaceDestroyed(SurfaceHolder holder) { objMediaPlayer.stop(); } 
} 

回答

13

具有u嘗試使用:

objVideoView.setZOrderMediaOverlay(true); 
objVideoView.videoView.setZOrderOnTop(true); 
中的onCreate(...)方法

+1

我不知道爲什麼沒有人upvoted這個答案。它工作完美。 – Corbella

0

通過擴展VideoView類,創建一個自定義的VideoPlayer並使用它:

public class VideoPlayer extends VideoView { 

    public VideoPlayer(Context context) { 
     super(context); 
     init(); 
    } 

    @Override 
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      TyrooLog.i(TAG, "onMeasure"); 
      int width = getDefaultSize(videoWidth, widthMeasureSpec); 
      int height = getDefaultSize(videoHeight, heightMeasureSpec); 
      if (videoWidth > 0 && videoHeight > 0) { 
       if (videoWidth * height > width * videoHeight) { 
        TyrooLog.i(TAG, "video too tall, correcting"); 
        height = width * videoHeight/videoWidth; 
       } else if (videoWidth * height < width * videoHeight) { 
        TyrooLog.i(TAG, "video too wide, correcting"); 
        width = height * videoWidth/videoHeight; 
       } else { 
        TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight); 
       } 
      } 
      TyrooLog.i(TAG, "setting size: " + width + 'x' + height); 
      setMeasuredDimension(width, height); 
     } 
    } 
} 
+0

您是否注意到您回答了一個近5年的問題並已得到充分回答? –

相關問題