我在Android设备上捕捉PORTRAIT方向的新视频,如下所示:
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1886);
它给了我这个文件:" /mnt/sdcard/DCIM/Camera/video-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中旋转此视频?
另外,我如何以编程方式确定方向?
答案 0 :(得分: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;
}
}
不要忘记在样式中隐藏ActionBar,或者以编程方式隐藏活动。