在Android webview应用程序中播放本地音频文件

发布于 2025-01-03 09:58:32 字数 418 浏览 1 评论 0原文

我有一个简单的 Android webview 应用程序,使用 Phonegap Build 构建。尝试播放 APK 中包含的本地 mp3 文件时,没有任何反应。但是,如果我从网上提取 mp3,它就可以工作。

例如,

<audio src="www.example.com/01.mp3" > WORKS JUST FINE

<audio src="01.mp3" > DOES NOT WORK

我是 Android 开发新手,所以我只能猜测 Android 的媒体播放器无法访问该文件。这是我的日志文件中的错误/警告的链接,如果这有助于阐明问题。

http://pastebin.com/isS542RE

I have a simple Android webview app, built with Phonegap Build. When trying to play a local mp3 file that's included in the APK, nothing happens. However, if I pull an mp3 from the web it works.

e.g.

<audio src="www.example.com/01.mp3" > WORKS JUST FINE

<audio src="01.mp3" > DOES NOT WORK

I'm new to Android development, so I can only guess that the file is somehow inaccessible by Android's media player. Here's a link to the errors/warnings from my log file if that helps shed any light on the problem.

http://pastebin.com/isS542RE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月隐月明月朦胧 2025-01-10 09:58:32

我使用 Phonegap 的 Media 类让它工作。我一开始肯定做错了什么,但我不确定是什么。通过媒体对象访问它的正确方法如西蒙之前所述:
myMedia = new Media("/android_asset/www/test.mp3");

您必须将完整路径放在相对路径上,因为媒体类默认为 /SDCARD 目录。

Simon 还写了一篇关于使用媒体类的精彩文章:
http://simonmacdonald.blogspot.com /2011/05/using-media-class-in-phonegap.html?m=1

至于 HTML5 方法和使用 AUDIO 标签,我仍然不确定为什么会失败在本地文件上。 Phonegap 的媒体类工作得很好,而且无论如何可能是一个更可靠的解决方案。电话间隙 FTW!

I have it working using Phonegap's Media class. I must have been doing something wrong initially, but I'm not sure what. The proper way to access it through the media object is as Simon stated previously:
myMedia = new Media("/android_asset/www/test.mp3");

You have to put the full path as the media class defaults to the /SDCARD directory on relative paths.

Simon also has a great write up about using the media class:
http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html?m=1

As for the HTML5 method and using the AUDIO tag, I'm still not certain why it fails on local files. Phonegap's media class works just fine though and is probably a more reliable solution anyhow. Phonegap FTW!

软的没边 2025-01-10 09:58:32

您是否尝试过使用“file:///android_assets/01.mp3”将文件指向本地文件系统路径(假设您的 .mp3 位于应用程序资产目录中)?

Have you tried pointing the file to the local file system path using "file:///android_assets/01.mp3" assuming your .mp3 is within the applications assets directory?

稀香 2025-01-10 09:58:32

我们遇到了同样的问题,为了播放媒体文件,需要位于外部存储目录中。尝试将 HTML 和相关文件移动到 /mnt/sdcard/

We had the same problem, the media files, in order to be played, needs to be in the external storage dir. Try to move your HTML and related files to /mnt/sdcard/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文