2012-02-02 50 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; 
    } 
} 

不要忘了隱藏動作條的風格,或以編程方式活動。