2012-02-02 27 views
9

tôi chụp một video mới theo hướng dọc trên một thiết bị Android như thế này:Android chân dung video định hướng sai trong VideoView

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

và nó mang lại cho tôi tập tin này: "/ mnt/sdcard/DCIM/Camera/video -2012-02-02-10-45-48.mp4"

Sau đó, tôi chơi nó như thế này:

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

Dưới đây là file layout của tôi:

<?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> 

Khi tôi phát nó trong thư viện Android chuẩn, định hướng là chính xác. Nhưng khi tôi phát video trong VideoView ở trên, nó quay 90 độ. Cảnh quan hoạt động tuyệt vời, vấn đề duy nhất là video chân dung.

Làm cách nào để xoay video này trong VideoView?
Ngoài ra, làm cách nào tôi có thể xác định định hướng theo chương trình?

+1

Bạn có thể tìm giải pháp cho vấn đề này không? Tôi có cùng một vấn đề – Thatdude1

Trả lời

1

Trước tiên, bạn cần xác định hướng của video đã quay. Hầu hết các điện thoại thông minh mới đều sử dụng hướng ngang cho máy ảnh mặc dù có các phiên bản sử dụng hình ảnh chân dung. Để xác định hướng bạn có thể lấy chiều dài và chiều rộng của khung, sau đó so sánh chúng. Khi bạn bắt đầu kiểm tra xem video định hướng hoạt động đó và tùy thuộc vào thay đổi hoạt động định hướng.

Mã số Ví dụ:

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

Đừng quên để ẩn ActionBar trong phong cách, hoặc theo trình tự trong hoạt động.

Các vấn đề liên quan