在Android webview应用程序中播放本地音频文件
我有一个简单的 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 的媒体播放器无法访问该文件。这是我的日志文件中的错误/警告的链接,如果这有助于阐明问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用 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!
您是否尝试过使用“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?
我们遇到了同样的问题,为了播放媒体文件,需要位于外部存储目录中。尝试将 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/
.我就此提交了一份官方错误报告:
http://code.google.com/p/android/issues/detail?id=41995&thanks=41995&ts=1356643666
I filed an official bug report on this:
http://code.google.com/p/android/issues/detail?id=41995&thanks=41995&ts=1356643666