8

プロジェクトに保存したビデオを再生しようとしています。これ(.mp4テストビデオ)をダウンロード して、プロジェクトのルートにvidという名前のフォルダーをプロジェクト内に作成しました。次に、このコードを使用しました。

public void PlayLocalVideo(View view)
    {
    VideoView video=(VideoView) findViewById(R.id.video1);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video);
    video.setMediaController(mediaController);
    video.setKeepScreenOn(true);
    video.setVideoPath("android.resource://uk.co.SplashActivity/vid/big_buck_bunny.mp4");
    video.start();
    video.requestFocus();
}

私のxmlは次のようになります:

<VideoView
    android:id="@+id/video1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

PlayLocalVideoは、ボタンのonclickイベントで使用したメソッドです。しかし、再生を押しても何も起こりません:(

4

3 に答える 3

12

ファイルをvid フォルダーの代わりにres/raw/big_buck_bunny.mp4に貼り付けて、videoPath を次のように変更します。

video.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny);
于 2013-02-05T15:38:18.123 に答える
1

1Mb 以上のファイルに正常にアクセスできない Android OS の不具合の可能性がありますassets フォルダから 1M 以上のファイルを読み込みます

おそらく、ビデオ ファイルを 1Mb サイズの部分に分割する必要があります。次に、この部分を sdcard 上の 1 つのファイルにマージして再生します。

たとえば、私はbig_buck_bunny.mp45 つの部分big_buck_bunny.mp4.part0に分割big_buck_bunny.mp4.part1しました。それらをマージするには、このメソッドを使用できます

private void copyVideoFromAssets(String inFilePrefix, String outFileName) throws IOException {
    // Get list of files in assets and sort them
    final String[] assetsFiles = getAssets().list("");
    Arrays.sort(assetsFiles);

    // Open the empty file as the output stream
    final OutputStream output = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024 * 128];

    for (String file: assetsFiles) {
        if (file.startsWith(inFilePrefix)) {
            // Open part of file stored in assets as the input stream
            final InputStream input = getAssets().open(file);

            // Transfer bytes from the input file to the output file
            int length = input.read(buffer);
            while (length > 0) {
                output.write(buffer, 0, length);
                length = input.read(buffer);
            }
            input.close();
        }
    }

    // Close the streams
    output.flush();
    output.close();
}

public void PlayLocalVideo(View view)
    try {
        copyVideoFromAssets("big_buck_bunny.mp4.part", "/mnt/sdcard/big_buck_bunny.mp4");
    } catch (IOException e) {
        e.printStackTrace();
    }

    VideoView video=(VideoView) findViewById(R.id.video);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video);
    video.setMediaController(mediaController);
    video.setKeepScreenOn(true);
    video.setVideoPath("/mnt/sdcard/big_buck_bunny.mp4");
    video.start();
    video.requestFocus();
}
于 2013-02-05T15:49:15.293 に答える