【问题标题】:Android: Play local video with mediaplayerAndroid:使用媒体播放器播放本地视频
【发布时间】:2013-02-05 14:09:47
【问题描述】:

我正在尝试播放我保存在项目中的视频。我已经下载this (一个 .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 事件中使用的一种方法。但是当我按下播放时没有任何反应:(

【问题讨论】:

    标签: android video media-player


    【解决方案1】:

    只需将文件粘贴到 res/raw/big_buck_bunny.mp4 而不是 vid 文件夹并更改 您的 videoPath 到:

    video.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny);
    

    【讨论】:

    • 没有什么不同。即使视频坏了,我应该能够看到视频视图吗?我将按钮放在 xml 中的 videoview 上方,但下方似乎没有任何内容。
    【解决方案2】:

    问题可能出在 Android 操作系统缺陷,无法正常访问超过 1Mb 大小的文件Load files bigger than 1M from assets folder

    您可能需要将视频文件拆分为 1Mb 大小的部分。然后将这些部分合并到一个sdcard上的文件中播放。

    例如,我将big_buck_bunny.mp4 拆分为5 个部分big_buck_bunny.mp4.part0big_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();
    }
    

    【讨论】:

    • 数据已经压缩(例如.mp4)时不会出现此问题
    【解决方案3】:

    试试这个代码....

    第一次在 res 目录中将文件夹名称设为 raw,将您的视频复制到该文件夹​​中并试用此代码...

        video1=(VideoView)findViewById(R.id.myvideoview);
        video1.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/"+R.raw.YOUR_VIDEO_FILE_NAME));
        video1.setMediaController(new MediaController(this));
        video1.requestFocus();
        video1.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多