2011-04-18 80 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); 
     } 
    } 
}