【问题标题】:Audio doesn't play on Monaca音频无法在 Monaca 上播放
【发布时间】:2016-03-09 00:17:30
【问题描述】:

音频无法在我的 Hybrid 应用中播放。
它在调试模式下工作,但在构建和安装后,apk 文件不再工作。

这是播放音频的完整 JavaScript 代码。
谁能帮帮我?

var media = null;
var mediaTimer = null;

    function getPath() {
        var str = location.pathname;
        var i = str.lastIndexOf('/');
        return str.substring(0,i+1);
    }

    function playAudio() {
        var src = 'images/ring.mp3';

        media = new Media (getPath() + src , onSuccess, onError);

        //Play the audio. You can set number of the replaying time here.
        media.play({numberOfLoops:"0"});

        if (mediaTimer == null) {

            mediaTimer = setInterval(function() {

                // Return a current playback position
                media.getCurrentPosition(

                    //A Callback function if it's success
                    function(position) {
                        if (position > -1) {
                            setAudioPosition((position) + " sec");

                                    //If the playback stops at "-0.001" position, replay the audio.
                                    if(position == -0.001){

                                        media.play({numberOfLoops:"infinite"});

                                    }
                        }
                    },
                    //A callback function in case of failure
                    function(e) {

                        console.log("Error getting pos=" + e);
                        setAudioPosition("Error: " + e);

                    }
                );
            }, 1000);
        }
    }

    function pauseAudio() {

        if (media) {
            media.pause();
        }
    }

    function stopAudio() {

        if (media) {
            media.stop();
        }

        clearInterval(mediaTimer);
        mediaTimer = null;
    }

      //A success callback function displaying a success message.
      function onSuccess() {
        stopAudio();

      }

【问题讨论】:

    标签: android audio hybrid-mobile-app onsen-ui monaca


    【解决方案1】:

    这是 Phonegap 的一个已知问题。它已多次解决,并在此处发布了一些最佳信息:HTML5 audio not playing in PhoneGap App (Possible to use Media?)

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2021-05-07
      • 2014-09-19
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多