2012-02-02 57 views
9

我捕捉Android设备这样在纵向新的视频:Android的人像影像显示方向错在VideoView

Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE); 
startActivityForResult(intent, 1886); 

,这让我这个文件:“到/ mnt/SD卡/ DCIM /相机/视频-2012-02-02-10-45-48.mp4"

然后我玩这样的:

private VideoView videoView = (VideoView) findViewById(R.id.videoView); 
String videoUrl = "/mnt/sdcard/DCIM/Camera/video-2012-02-02-10-45-48.mp4"; 
videoView.setMediaController(new MediaController(this));  
videoView.setVideoURI(Uri.parse(videoUrl)); 
videoView.start(); 

这是我的布局文件:

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

<VideoView 
    android:id="@+id/videoView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" /> 

</RelativeLayout> 

当我在标准的Android图库中播放时,其方向是正确的。但是当我在上面的VideoView中播放视频时,它旋转了90度。景观效果很好,唯一的问题是肖像视频。

如何在VideoView中旋转此视频?
另外,如何以编程方式确定方向?

+1

你能找到解决这个问题的办法吗?我有同样的问题 – Thatdude1 2015-02-14 18:47:26

回答

1

您需要首先确定捕获的视频的方向。尽管存在使用肖像的版本,但大多数新智能手机都使用横向照相机。为了确定方向,你可以采取帧的长度和宽度,然后比较它们。当你开始检查是否有活动方向的视频,并且取决于方向活动的变化。

代码示例:

public class MainActivity extends ActionBarActivity { 

    String videoUrl = "/mnt/sdcard/DCIM/100ANDRO/MOV_9195.mp4"; 
    int videoWidth; 
    int videoHeight; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getVideoAspectRatio(); 
     if (isVideoLandscaped()) { 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     } 

     setContentView(R.layout.activity_main); 
     VideoView videoView = (VideoView) findVewById(R.id.videoView); 
     videoView.setMediaController(new MediaController(this)); 
     videoView.setVideoURI(Uri.parse(videoUrl)); 
     videoView.start(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private void getVideoAspectRatio() { 
     MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); 
     mediaMetadataRetriever.setDataSource(this, Uri.parse(videoUrl)); 
     String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); 
     String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); 
     videoWidth = Integer.parseInt(width); 
     videoHeight = Integer.parseInt(height); 
    } 

    private boolean isVideoLandscaped() { 
     if (videoWidth > videoHeight) { 
      return true; 
     } else return false; 
    } 
} 

不要忘了隐藏动作条的风格,或以编程方式活动。