Play sound on Phonegap app for Android

后端 未结 6 1169
时光说笑
时光说笑 2020-11-29 05:47

I try to play an mp3 file. This works if I change the path to the file on my local webserver, but if I run this on an Android device the sound is not played and no error is

6条回答
  •  鱼传尺愫
    2020-11-29 06:02

    The play method which the HTML5 API provides is useful for playing media files available on the web. Phonegap provides the media plugin to play local media files. I checked out your code and played the sound successfully by making the following changes. Please check at your end.

    1.Add the following line to config.xml

    
    

    2.Replace the lines in index.html

    
    
    

    with these lines

    
    
    

    3.Add the following function to js/index.js

    function playAudio(id) {
        var audioElement = document.getElementById(id);
        var url = audioElement.getAttribute('src');
        var my_media = new Media(url,
                // success callback
                 function () { console.log("playAudio():Audio Success"); },
                // error callback
                 function (err) { console.log("playAudio():Audio Error: " + err); }
        );
               // Play audio
        my_media.play();
    }
    

提交回复
热议问题