admin管理员组文章数量:1279113
I'm trying to play sound from JavaScript code loaded to WebView from assets:
WebView web_view = (WebView) findViewById(R.id.web_view);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebChromeClient(new WebChromeClient());
web_view.loadUrl("file:///assets/www/index.html");
I tried all binations of following ways how to play audio with OGG, MP3 and WAV files, the JavaSrcipt code is in assets/www/js/play.js:
audio = new Audio("../audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("./audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("file:///android_asset/www/audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio();
audio.src = document.getElementById("audio_tag").src;
audio.load();
audio.play();
Where audio_tag is <audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>
, also tried with all binations of paths and formats.
But the sound is not playing and only clue what may be wrong is following general error:
E/MediaPlayer(1530): error (1, -2147483648)
Guessing from this info message, the path to audio file should be correct for second trough fifth case:
I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');
Although this question may be considered as duplicate of Android WebView playing audio with javascript and the answer is not helping anyhow since shouldOverrideUrlLoading is never called, I'm trying to specify this problem more precisely here.
Do you have any clue what may be wrong or how to find out what is it?
Thank you.
Edit: Testing in Galaxy Gio with Android 2.3 gives me different set of errors though:
02-06 17:15:13.829: V/PlayerDriver(95): AddToScheduler 02-06 17:15:13.829: V/PlayerDriver(95): PendForExec 02-06 17:15:13.829: V/PlayerDriver(95): OsclActiveScheduler::Current 02-06 17:15:13.829: V/PlayerDriver(95): StartScheduler 02-06 17:15:13.829: V/PVPlayer(95): send PLAYER_SETUP 02-06 17:15:13.829: V/PlayerDriver(95): Send player code: 2 02-06 17:15:13.829: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.829: V/PlayerDriver(95): Completed mand PLAYER_SETUP status=PVMFSuccess 02-06 17:15:13.829: V/PVPlayer(95): setDataSource(file:///android_asset/www/audio/sound.mp3) 02-06 17:15:13.839: V/PVPlayer(95): prepareAsync 02-06 17:15:13.839: V/PVPlayer(95): data source = file:///android_asset/www/audio/sound.mp3 02-06 17:15:13.849: V/PlayerDriver(95): Send player code: 3 02-06 17:15:13.849: V/PlayerDriver(95): handleSetDataSource 02-06 17:15:13.849: V/PlayerDriver(95): handleSetDataSource- scanning for extension
02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingStart 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: type=26 UNHANDLED 02-06 17:15:13.849: W/MediaPlayer(4361): info/warning (1, 26) 02-06 17:15:13.849: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.849: V/PlayerDriver(95): Completed mand PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported 02-06 17:15:13.849: E/PlayerDriver(95): Command PLAYER_SET_DATA_SOURCE pleted with an error or info
PVMFErrNotSupported 02-06 17:15:13.849: E/MediaPlayer(4361): error (1, -4) 02-06 17:15:13.849: V/PVPlayer(95): run_init s=-2147483648, cancelled=0 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingComplete 02-06 17:15:13.849: W/PlayerDriver(95): PVMFInfoErrorHandlingComplete
02-06 17:15:13.939: I/MediaPlayer(4361): Info (1,26) 02-06 17:15:13.939: E/MediaPlayer(4361): Error (1,-4) 02-06 17:15:13.939: V/PVPlayer(95): reset 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 18 02-06 17:15:13.939: V/PlayerDriver(95): handleCancelAllCommands 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_CANCEL_ALL_COMMANDS status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 11 02-06 17:15:13.939: V/PlayerDriver(95): handleReset 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_RESET status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 17 02-06 17:15:13.939: V/PlayerDriver(95): handleRemoveDataSource 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_REMOVE_DATA_SOURCE status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): remove datasource plete 02-06 17:15:13.939: V/PVPlayer(95): unmap file
I'm trying to play sound from JavaScript code loaded to WebView from assets:
WebView web_view = (WebView) findViewById(R.id.web_view);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebChromeClient(new WebChromeClient());
web_view.loadUrl("file:///assets/www/index.html");
I tried all binations of following ways how to play audio with OGG, MP3 and WAV files, the JavaSrcipt code is in assets/www/js/play.js:
audio = new Audio("../audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("./audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("file:///android_asset/www/audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio();
audio.src = document.getElementById("audio_tag").src;
audio.load();
audio.play();
Where audio_tag is <audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>
, also tried with all binations of paths and formats.
But the sound is not playing and only clue what may be wrong is following general error:
E/MediaPlayer(1530): error (1, -2147483648)
Guessing from this info message, the path to audio file should be correct for second trough fifth case:
I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');
Although this question may be considered as duplicate of Android WebView playing audio with javascript and the answer is not helping anyhow since shouldOverrideUrlLoading is never called, I'm trying to specify this problem more precisely here.
Do you have any clue what may be wrong or how to find out what is it?
Thank you.
Edit: Testing in Galaxy Gio with Android 2.3 gives me different set of errors though:
Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Feb 6, 2012 at 14:56 BlackhexBlackhex 1,7543 gold badges22 silver badges46 bronze badges 102-06 17:15:13.829: V/PlayerDriver(95): AddToScheduler 02-06 17:15:13.829: V/PlayerDriver(95): PendForExec 02-06 17:15:13.829: V/PlayerDriver(95): OsclActiveScheduler::Current 02-06 17:15:13.829: V/PlayerDriver(95): StartScheduler 02-06 17:15:13.829: V/PVPlayer(95): send PLAYER_SETUP 02-06 17:15:13.829: V/PlayerDriver(95): Send player code: 2 02-06 17:15:13.829: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.829: V/PlayerDriver(95): Completed mand PLAYER_SETUP status=PVMFSuccess 02-06 17:15:13.829: V/PVPlayer(95): setDataSource(file:///android_asset/www/audio/sound.mp3) 02-06 17:15:13.839: V/PVPlayer(95): prepareAsync 02-06 17:15:13.839: V/PVPlayer(95): data source = file:///android_asset/www/audio/sound.mp3 02-06 17:15:13.849: V/PlayerDriver(95): Send player code: 3 02-06 17:15:13.849: V/PlayerDriver(95): handleSetDataSource 02-06 17:15:13.849: V/PlayerDriver(95): handleSetDataSource- scanning for extension
02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingStart 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: type=26 UNHANDLED 02-06 17:15:13.849: W/MediaPlayer(4361): info/warning (1, 26) 02-06 17:15:13.849: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.849: V/PlayerDriver(95): Completed mand PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported 02-06 17:15:13.849: E/PlayerDriver(95): Command PLAYER_SET_DATA_SOURCE pleted with an error or info
PVMFErrNotSupported 02-06 17:15:13.849: E/MediaPlayer(4361): error (1, -4) 02-06 17:15:13.849: V/PVPlayer(95): run_init s=-2147483648, cancelled=0 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingComplete 02-06 17:15:13.849: W/PlayerDriver(95): PVMFInfoErrorHandlingComplete
02-06 17:15:13.939: I/MediaPlayer(4361): Info (1,26) 02-06 17:15:13.939: E/MediaPlayer(4361): Error (1,-4) 02-06 17:15:13.939: V/PVPlayer(95): reset 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 18 02-06 17:15:13.939: V/PlayerDriver(95): handleCancelAllCommands 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_CANCEL_ALL_COMMANDS status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 11 02-06 17:15:13.939: V/PlayerDriver(95): handleReset 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_RESET status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 17 02-06 17:15:13.939: V/PlayerDriver(95): handleRemoveDataSource 02-06 17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed mand PLAYER_REMOVE_DATA_SOURCE status=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): remove datasource plete 02-06 17:15:13.939: V/PVPlayer(95): unmap file
- think I found an wonderful workaround for this. Please, check my answer here stackoverflow./a/40634355/3866399 – Rodrigo Borba Commented Nov 16, 2016 at 14:16
3 Answers
Reset to default 6Re: MediaPlayer( ): Error (1,-2147483648)
The Android MediaPlayer needs the media files to be world-readable so they can’t reside in the “assets” folder inside the Eclipse project. Push the audio/video files onto the device external storage. To do this, with an emulator, use the DDMS Perspective in Eclipse (while your emulator is running go to Window->Open Prospective->Other->DDMS) to create a folder and push files onto SD card image or internal (non-removable) storage.
Reference on DDMS: http://developer.android./guide/developing/debugging/ddms.html
In DDMs, select your emulator in the Devices panel on the left, and then choose the FileExplorer tab on the right, look for a folder named /mnt/sdcard/ which contains the SDCard contents, or, alternatively /Android/data/package_name/files/ for the standardized application’s storage area. So, in the above example, if you create a folder myaudio on the SDcard the filename path would be:
audio = new Audio("/mnt/sdcard/myaudio/sound.mp3");
Reference: http://developer.android./guide/topics/data/data-storage.html#filesExternal
And don't give up on HTML5!
I had the same problem and finally i'm using phonegap and cordova where you can play and record audios easily: http://docs.phonegap./en/2.9.0/cordova_media_media.md.html
It depends on your Android device and the available codecs... But probably the path is not correct. Are you using Phonegap? Does your code play on desktop browsers?
I'm using a similar code:
var myAudio = document.getElementsByTagName('audio')[0];
myAudio.pause();
myAudio.src = file;
myAudio.play();
And my HTML audio tag is this:
<audio id="audio" src="" type="audio/mpeg" preload="metadata" ontimeupdate="timeUpdate()"
ondurationchange="durationChange()" onerror="musicError()" onended="musicEnded()">
<embed src="" height=50 width=100></embed>
I'm using mp3 audio file format because it works for iOS and some Android devices.
Also, notice that Audio tag element is not available on all android versions. As you can see here, it works for Android 2.3 and up.
本文标签: androidPlaying sound in WebView from JavaScriptStack Overflow
版权声明:本文标题:android - Playing sound in WebView from JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741224436a2361588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论