Play Sound in WebView Using JavaScript

I am trying to play sound from JavaScript code loaded in a 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 combinations of the following ways, how to play audio with OGG, MP3 and WAV files, 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 <audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>is also used with all combinations of paths and formats.

But the sound does not play and only tells you what might be wrong, this is the following general error:

E / MediaPlayer (1530): error (1, -2147483648)

Guessing from this informational message, the path to the sound file should be correct for the second fifth case:

I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');

Android WebView, javascript, , shouldOverrideUrlLoading , .

- , , ?

.

: Galaxy Gio Android 2.3 :

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:: 02-06 17: 15: 13.829: V/PlayerDriver (95): StartScheduler 02-06 17: 15: 13.829: V/PVPlayer (95): PLAYER_SETUP 02-06 17: 15: 13.829: V/PlayerDriver (95): : 2 02-06 17: 15: 13.829: V/PlayerDriver (95): CommandCompleted 02-06 17: 15: 13.829: V/PlayerDriver (95): PLAYER_SETUP status = PVMFSuccess 02-06 17: 15: 13.829: V/PVPlayer (95): setDataSource (:///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): = :///android_asset/www/audio/sound.mp3 02-06 17: 15: 13.849: V/PlayerDriver (95): : 3 02-06 17: 15: 13.849: V/PlayerDriver (95): handleSetDataSource 02-06 17: 15: 13.849: V/PlayerDriver (95): handleSetDataSource -
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): / (1, 26) 02-06 17: 15: 13.849: V/PlayerDriver (95): CommandCompleted 02-06 17: 15: 13.849: V/PlayerDriver (95): PLAYER_SET_DATA_SOURCE status = PVMFErrNotSupported 02-06 17: 15: 13.849: E/PlayerDriver (95): PLAYER_SET_DATA_SOURCE
PVMFErrNotSupported 02-06 17: 15: 13.849: E/MediaPlayer (4361): (1, -4) 02-06 17: 15: 13.849: V/PVPlayer (95): run_init s = -2147483648, = 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): (1,26) 02-06 17: 15: 13.939: E/MediaPlayer (4361): (1, -4) 02-06 17: 15: 13.939: V/PVPlayer (95): reset 02-06 17: 15: 13.939: V/PlayerDriver (95): : 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): PLAYER_CANCEL_ALL_COMMANDS status = PVMFSuccess 02-06 17: 15: 13.939: V/PlayerDriver (95): : 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): PLAYER_RESET status = PVMFSuccess 02-06 17: 15: 13.939: V/PlayerDriver (95): : 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): PLAYER_REMOVE_DATA_SOURCE status = PVMFSuccess 02-06 17: 15: 13.939: V/PlayerDriver (95): 02-06 17: 15: 13.939: V/PVPlayer (95): unmap

+2
3

Re: MediaPlayer(): (1, -2147483648)

Android MediaPlayer , , "" Eclipse. / . DDMS Eclipse ( , Window- > Open Prospective- > Other- > DDMS), SD- (- ) .

DDMS: http://developer.android.com/guide/developing/debugging/ddms.html

DDM "" , FileExplorer , /mnt/sdcard/, SDCard , ,/Android/data/_// . , , myaudio SD-, :

audio = ( "/mnt/sdcard/myaudio/sound.mp3" );

: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

HTML5!

+7

It depends on your Android device and the available codecs ... But probably the path is wrong. Do you use Phonegap? Does your code play in desktop browsers?

I use similar code:

var myAudio = document.getElementsByTagName('audio')[0];
myAudio.pause();
myAudio.src = file;
myAudio.play();

And my HTML sound tag:

<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 use the mp3 audio file format because it works for iOS and some Android devices.

Also note that the Audio tag element is not available for all versions of Android. As you can see here , it works for Android 2.3 and above.

0
source

All Articles