문제

Well I can't debug if I'm using phonegap build because developer tools has no object called Media. But I have build the app and tested in the device (Android 4.1.2) but the audio does not play.

After I added the Media feature it has stopped working.

Here is the code sample:

config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.phonegap.audiotest"
    versionCode = "10" 
    version     = "1.0.0" >
<!-- versionCode is optional and Android only -->

<name>Audio Test</name>

<description>
    An example for phonegap Audio. 
</description>

<author href="https://www.whatwgisnottelling.com" email="anyname@gmail.com">
    justTest
</author>

<gap:plugin name="org.apache.cordova.media" version="0.2.8" />

</widget>

Index.html

<script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.4.2.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    <script type="text/javascript">
        var media = null;   
        $(document).one('deviceready', App.initialize());   
</script>

main.js

var App = {
    initialize: function () {
        this.touchEvents();
    },
    touchEvents: function () {
        $(document).bind('touchmove', function(e) {
            e.preventDefault();
        });
        var media = new Media();
        var box = document.getElementById('box');
        box.addEventListener('touchstart', function () {
            box.style.backgroundColor = 'black';
            media.play("song.wav");
        });
        box.addEventListener('touchend', function () {
            box.style.backgroundColor = 'red';
            media.stop("song.wav");
        });
       }
};
도움이 되었습니까?

해결책

Path of media file incorrect. You must get path correctly:

  var getPathMedia = function () {
    var path = window.location.pathname;
    var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
    // Return path at www folder
    return 'file://' + phoneGapPath;
  };
  var media = new Media(getPathMedia() + 'media/message.mp3');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top