Przechwytuję nowy film w orientacji PORTRAIT na urządzeniu z Androidem w następujący sposób:
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1886);
i daje mi ten plik: „/mnt/sdcard/DCIM/Camera/video-2012-02-02-10-45-48.mp4”
Następnie gram 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ładu:
<?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>
Kiedy gram w standardowej galerii Androida,orientacja jest prawidłowa. Ale kiedy odtwarzam wideo w powyższym VideoView, jest on obrócony o 90 stopni. Krajobraz działa świetnie, jedynym problemem są filmy portretowe.
Jak mogę obrócić ten film w VideoView?
Jak mogę programowo określić orientację?
Odpowiedzi:
1 dla odpowiedzi № 1Najpierw musisz określić orientacjęuchwyconego wideo. Większość nowych smartfonów używa aparatu w orientacji poziomej, chociaż istnieją wersje wykorzystujące portret. Aby określić orientację, możesz wziąć długość i szerokość ramki, a następnie porównać je. Gdy zaczniesz sprawdzać, czy to wideo zorientowane na aktywność oraz w zależności od zmiany działań na orientację.
Przykład kodu:
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 ukryć paska ActionSar w stylach lub programowo w działaniu.