是否可以使用 content://作为 <audio> 的源?WebView 中的元素 [英] Is it possible to use content:// as a source for an <audio> element in a WebView

查看:26
本文介绍了是否可以使用 content://作为 <audio> 的源?WebView 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 ContentProvider.它导出我的 assets/目录中的文件.我正在使用 content://url 来访问 WebView 中导出的内容.

以下 HTML 按预期工作:

/test.jpg">

我正在尝试将内容提供程序用于 mp3 音频文件:

<脚本>url = "content:///test.mp3";var audio = document.createElement('audio');audio.src = url;audio.controls = "控制";document.getElementById("player").appendChild(audio);

我收到以下错误消息.

E/MediaPlayer(24120):无法创建媒体播放器E/HTML5Audio(24120): 无法加载资源: content://.../test.mp3 exc: java.io.IOException: setDataSource failed.: status=0x80000000

我尝试了 Android SDK 8 和 10,但没有成功.我在日志中没有看到对我的 ContentProvider 的任何访问.看起来在 WebView 中无法从音频标签访问内容.看起来很奇怪,因为以下代码按预期工作:

MediaPlayer player = MediaPlayer.create(this,uri.parse("content:///test.mp3"));播放器开始();

如何在 WebView 中播放来自 ContentProvider 的音频文件?

解决方案

简短的回答是否定的,您不能使用 content:// 作为音频元素的来源.

您得到的错误来自 HTML5Audio setDataSource 方法,如果您查看 源代码,调用MediaPlayer setDataSource 与音频 url.如果您查看 文档 对于 MediaPlayer 类,它只支持文件路径和 http/rtsp url.

我认为最好的办法是将音频内容保存到文件路径并使用 file: url 来访问它.

但是,如果我没记错的话,您还需要使文件世界可读,以便 MediaPlayer 代码访问它.这需要使用私有 android.os.FileUtils 类和如下代码:

void makeFileReadable(String filename) {尝试 {Class fileUtils = Class.forName("android.os.FileUtils");方法 setPermissions = fileUtils.getMethod("setPermissions", String.class,int.class, int.class, int.class);setPermissions.invoke(fileUtils, 文件名, 0775, -1, -1);}捕获(异常前){}}

775rwxrwxr-x 等价,它可能比必要的更宽松,因此您可能想要尝试使用更宽松的值.另请注意,您可能需要为包含该文件的目录以及该目录的父目录等设置权限 - 而不仅仅是文件本身.

I created a ContentProvider. It exports files within my assets/ directory. I'm using content:// urls to access the exported content in a WebView.

The following HTML works as expected:

<img src="content://<provider-name>/test.jpg">

I'm trying to use the content provider for mp3 audio files:

<div id='player'></div>
<script>
  url = "content://<provider-name>/test.mp3";
  var audio = document.createElement('audio');
  audio.src = url;
  audio.controls = "controls";
  document.getElementById("player").appendChild(audio);
</script>

I'm getting the following error message.

E/MediaPlayer(24120): Unable to to create media player
E/HTML5Audio(24120): couldn't load the resource: content://.../test.mp3 exc: java.io.IOException: setDataSource failed.: status=0x80000000

I tried the Android SDK 8 and 10 without success. I'm not seeing any access to my ContentProvider in the log. It looks like accessing content from audio-tags is not possible within a WebView. It seems strange, since the following code works as expected:

MediaPlayer player = MediaPlayer.create(this, 
    Uri.parse("content://<provider-name>/test.mp3")
);
player.start();

How can I play audio files from a ContentProvider within a WebView?

解决方案

The short answer is no, you can't use content:// as a source for an audio element.

The error you are getting comes from the HTML5Audio setDataSource method, which if you look at the source code, calls MediaPlayer setDataSource with the audio url. And if you look at the documentation for the MediaPlayer class, it only supports file paths and http/rtsp urls.

I think your best bet is to save your audio content to a file path and use a file: url to access it.

However, if I remember correctly, you will also need to make the file world readable in order for the MediaPlayer code to access it. This requires using the private android.os.FileUtils class with code like this:

void makeFileReadable(String filename) {
  try {
    Class fileUtils = Class.forName("android.os.FileUtils");
    Method setPermissions = fileUtils.getMethod("setPermissions", String.class,
     int.class, int.class, int.class);
    setPermissions.invoke(fileUtils, filename, 0775, -1, -1);
  }
  catch (Exception ex) {
  }
}

775 is the equivalent of rwxrwxr-x which may be more permissive than necessary, so you may want to try experimenting with less permissive values. Also note that you'll likely need to set the permissions on the directory containing the file, and that directory's parent, etc. - not just the file itself.

这篇关于是否可以使用 content://作为 &lt;audio&gt; 的源?WebView 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆