简体   繁体   中英

PhoneGap Build Android Audio Not Working

I'm using PhoneGap Build to launch an Android application. My html and everything works perfectly in the browser. However, it does not on my Android device. Does it have something to do with the permissions? I'm really not sure, and am completely confused as to where to go next. Below I have my html and javascript.

I do not have a context file, fyi. My folders are css, img, js, and sound.

Let me know if you can help!

html + javascript

<h1 id= "main">GO</h1>

var pp = 1;
            var audio = new Audio('sound/sound1.mp3');
$("#main").click(function(){
                console.log(pp);
                if (pp == 1){
                    audio.play();
                    pp++;
                    console.log(pp);
                }else if (pp == 2){
                    audio.pause();
                    pp--;
                    console.log("audio pause");
                }
            });

It's not a permission issue, rather, your HTML/Javascript code is run/loaded inside a customized Android Webview ( http://developer.android.com/reference/android/webkit/WebView.html ).

This Webview does have several quirks and does not quite behave, like a browser would in some cases.

One of those cases is playing audio or video. On Android, there is MediaPlayer to do that. If you were running this code in a 'normal' Android application you would have an Activity that contains an instance of WebView. To play media files you could for example inject an object http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object , java.lang.String) that plays the audio or video using Android's MediaPlayer.

So basically, it just doesn't work the way, you try, right now. However Phonegap provides an API to work around this issue. Possible that they are doing, what I just explained above. http://docs.phonegap.com/en/2.0.0/cordova_media_media.md.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM