【问题标题】:Why? I can't play downloaded mp4 video file using Intent为什么?我无法使用 Intent 播放下载的 mp4 视频文件
【发布时间】:2014-07-03 13:51:03
【问题描述】:

我必须开发一个从 URL 下载视频文件的应用程序 下载后,我正在通过 Intent 播放。

但每次我收到相同的消息时:“您无法播放此视频。”

下载代码:

protected String doInBackground(String... params) {

    File file = new File(getFilesDir(), generateFileName(params[0]));
        try {
            URL u = new URL(params[0]);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
            fos.write(buffer);
            fos.flush();
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
 }

视频播放方法:

public void playVideo(String fileName) {

       Uri data = Uri.parse(new File(fileName).getAbsolutePath());
       Log.i("DATA: ",""+data);

       Intent intent = new Intent();
       intent.setAction(android.content.Intent.ACTION_VIEW);
       intent.setDataAndType(data, "video/mp4");
       startActivity(intent); 
}

【问题讨论】:

  • 尝试通过浏览器下载此视频并播放。如果它不起作用,看起来视图没有兼容格式。如果可行,请发布您的 readFully 方法
  • 您的文件路径是否正确?
  • 你成功了吗?你有哪个安卓版本?我有一个类似的问题。在 Android 4.2 (Samsung Galaxy S3) 上运行良好,但在 4.4.4 (Nexus 4) 上运行良好

标签: android android-video-player video-player


【解决方案1】:

在向意图提供额外信息时,此代码对我有用。它不是视频数据,而是图像数据,但我认为概念应该是相同的(我可能错了,因为我没有在视频上尝试过)。

File file = new File(getExternalFilesDir(null), "image.png");
String uriPath = "file://"+file.getPath(); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uriPath));

请注意,我提供给意图的图像当然是在此之前下载的,然后保存在应用程序的目录中(这是我通过 getExternalFilesDir(null) 获得的)。然后图像作为流传递给意图。

如果这对您没有帮助,那么请检查是否安装了任何可以处理您的视频类型的应用程序(是 mp4 还是格式?)

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 2020-05-01
    • 2012-10-20
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多