2012-02-02 16 views
9

przechwycić nowy film w orientacji pionowej na urządzeniu z Androidem jak ten:Android orientacji pionowej wideo złego w VideoView

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

i to daje mi ten plik „/ mnt/sdcard/DCIM/Camera/wideo -2012-02-02-10-45-48.mp4"

Potem grać to tak:

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

Oto mój plik układ:

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

Po odtworzeniu w standardowej galerii Androida orientacja jest prawidłowa. Ale kiedy odtwarzam wideo w VideoView powyżej, jest obrócony o 90 stopni. Krajobraz działa świetnie, jedynym problemem są portrety wideo.

Jak mogę obrócić ten film wideo w VideoView?
Ponadto, w jaki sposób można programowo określić orientację?

+1

Czy udało Ci się znaleźć rozwiązanie tego problemu? Mam ten sam problem – Thatdude1

Odpowiedz

1

Najpierw należy określić orientację przechwyconego wideo. Większość nowych smartfonów używa orientacji poziomej kamery, chociaż istnieją wersje, które używają portretu. Aby określić orientację, można wziąć długość i szerokość ramki, a następnie porównać je. Kiedy zaczniesz sprawdzać, czy to wideo zorientowane na działanie i w zależności od zmiany działań orientacyjnych.

Kod Przykład:

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

Nie zapomnij, aby ukryć ActionBar stylów, lub programowo aktywności.

Powiązane problemy