10

次のように、Android デバイスで縦向きの新しいビデオをキャプチャします。

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 でこのビデオを回転するにはどうすればよいですか?
また、プログラムで向きを決定するにはどうすればよいですか?

4

1 に答える 1

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 を非表示にすることを忘れないでください。

于 2015-03-06T14:12:03.360 に答える