2011-04-18 75 views
1

我有我的活動視頻視圖用於顯示儲存在我的res.raw文件夾這樣的視頻:僅限VideoView音頻,無視頻?

MediaController controller=new MediaController(this); 
video.setMediaController(controller); 
String filePath="android.resource://" + getPackageName() + "/" + R.raw.video3; 
video.setVideoURI(Uri.parse(filePath)); 
video.requestFocus(); 
video.start(); 

的問題是,我只能聽到聲音,但視頻沒有顯示。

這可能是什麼原因?

編輯:這是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <Button 
    android:id="@+id/btnPlayAudio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Play Audio" 
     > 
     </Button> 
     <Button 
     android:id="@+id/btnPlayVideo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Play Video" 
     > 
     </Button> 

     <VideoView android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/videoView" 

     /> 
</LinearLayout> 
+0

並且你指定的MediaPlayer到VideoView?您是否看過:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html – Blundell 2011-04-18 12:40:57

+0

您是否嘗試使用standart android player播放此視頻?第二個問題:你在模擬器或設備上運行你的代碼?模擬器可能在播放視頻時遇到問題。 – 2011-04-18 12:44:05

+0

@Blundell:我只是使用上面的代碼。 @Anton:我嘗試了一個真實的設備 – 2011-04-18 12:46:06

回答

3

好,我知道了,

問題是我的VideoView有寬度和高度噸至WRAP_CONTENT當我改FILL_PARENT,視頻出現

感謝

-1

你它過於複雜:-)

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); 
mp.start(); 

Linky:Play from Raw Resource

+0

感謝您的回覆,我試過了您的代碼,但仍然只聽到了音頻,沒有顯示任何視頻 – 2011-04-18 12:11:48

+0

您的視頻格式是什麼?你有試過不同的視頻嗎? – Blundell 2011-04-18 12:12:21

+0

視頻是3GP,我嘗試了幾個視頻,包括MP4,但同樣的事情發生,我在HTC Desire手機上測試這個。 – 2011-04-18 12:13:35

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); 
     } 
    } 
}