PhoneGap - 无法播放本地声音文件

时间:2012-09-29 23:15:53

标签: android cordova android-mediaplayer

我正在使用Android设备使用PhoneGap。

我正在尝试播放本地.wav文件,但我遇到了一些错误。

我正在使用

中的示例代码
http://docs.phonegap.com/en/1.0.0/phonegap_media_media.md.html

document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        playAudio("sounds/menu-change.wav");
    }

    // Audio player
    var my_media = null;

    // Play audio
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);

        // Play audio
        my_media.play();
    }


    function onSuccess() {
        console.log("playAudio():Audio Success");
    }

    function onError(error) {
        alert('code: '    + error.code    + '\n' + 
              'message: ' + error.message + '\n');
    }

我收到以下错误

09-29 16:06:49.934: D/CordovaLog(14944): JSCallback Error: Request failed.
09-29 16:06:49.934: D/CordovaLog(14944): file:///android_asset/www/js/cordova-2.0.0.js: Line 3698 : JSCallback Error: Request failed.
09-29 16:06:49.934: I/Web Console(14944): JSCallback Error: Request failed. at file:///android_asset/www/js/cordova-2.0.0.js:3698
09-29 16:06:49.944: D/DroidGap(14944): onMessage(onPageStarted,file:///android_asset/www/settings.html)
09-29 16:06:49.964: I/AudioSystem(14944): getting audio flinger
09-29 16:06:49.974: I/AudioSystem(14944): returning new audio session id
09-29 16:06:50.014: E/MediaPlayer(14944): error (1, -2147483648)
09-29 16:06:50.014: W/System.err(14944): java.io.IOException: Prepare failed.: status=0x1
09-29 16:06:50.014: W/System.err(14944):    at android.media.MediaPlayer.prepare(Native Method)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.AudioPlayer.loadAudioFile(AudioPlayer.java:544)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.AudioPlayer.readyPlayer(AudioPlayer.java:468)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.AudioPlayer.startPlaying(AudioPlayer.java:212)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.AudioHandler.startPlayingAudio(AudioHandler.java:232)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.AudioHandler.execute(AudioHandler.java:75)
09-29 16:06:50.014: W/System.err(14944):    at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:192)
09-29 16:06:50.014: W/System.err(14944):    at java.lang.Thread.run(Thread.java:1027)

09-29 16:06:50.494: D/CordovaLog(14944): JSCallback: Message from Server: cordova.require('cordova/plugin/Media').onStatus('abf853b7-3e45-f906-fc59-92d9f11e9a14', 1, 0);
09-29 16:06:50.494: D/CordovaLog(14944): file:///android_asset/www/js/cordova-2.0.0.js: Line 3669 : JSCallback: Message from Server: cordova.require('cordova/plugin/Media').onStatus('abf853b7-3e45-f906-fc59-92d9f11e9a14', 1, 0);
09-29 16:06:50.504: I/Web Console(14944): JSCallback: Message from Server: cordova.require('cordova/plugin/Media').onStatus('abf853b7-3e45-f906-fc59-92d9f11e9a14', 1, 0); at file:///android_asset/www/js/cordova-2.0.0.js:3669
09-29 16:06:50.504: D/CordovaLog(14944): JSCallback Error: TypeError: Cannot read property 'statusCallback' of undefined
09-29 16:06:50.504: D/CordovaLog(14944): file:///android_asset/www/js/cordova-2.0.0.js: Line 3670 : JSCallback Error: TypeError: Cannot read property 'statusCallback' of undefined
09-29 16:06:50.504: I/Web Console(14944): JSCallback Error: TypeError: Cannot read property 'statusCallback' of undefined at file:///android_asset/www/js/cordova-2.0.0.js:3670

它说它需要媒体插件,但我在config.xml文件中启用了它

<plugin name="Media" value="org.apache.cordova.AudioHandler"/>

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

使用位于www文件夹中的声音文件时,您需要在Android上使用以下文件路径格式:

new Media("/android_asset/www/{directory}/{file-name}.mp3");