Cordova Media Plugin setVolume()无效

时间:2016-06-27 16:36:56

标签: ios cordova cordova-plugins

这是我的播放功能:你可以看到我在多个位置的setVolume()为0.这绝对没有效果。我试图也设置为0.8,0.2,无所谓它不会工作。我也尝试了非字符串值,这并不重要,因为值被转换为Obj-C模块内的浮点值。我还NSLogged以确保正确传递值,它是。

使用iPad iOS 9.2进行测试|科尔多瓦6.2 | Cordova Media Plugin 2.3.1。

play: function(src, seekTime)
        {
            App.Log.info('Playing audio source', src);
            seekTime = seekTime ? seekTime : 0;
            var obj = {
                source: src,
                startTime: new Date().getTime(),
                seekTime: seekTime,
                duration: 0,
                preloadedNext: false,
                audioSource: false
            };
            obj.audioSource = new Media(src, function(event){
                $this.player().onAudioEnd(event, obj);
            }, function(){
                //on error
            }, function(status)
            {
                obj.audioSource.setVolume("0.0");
                if(status == 2){
                    obj.audioSource.setVolume("0.0");
                    obj.audioSource.seekTo(obj.seekTime * 1000);
                    obj.timer = setInterval(function(){
                        obj.seekTime++;
                        obj.duration = obj.audioSource._duration;
                        if(obj.duration < 0){
                            return;
                        }
                        if(obj.seekTime >= (obj.duration - $this.default.fadeTime))
                        {
                            if(obj.preloadedNext){
                                return;
                            }
                            obj.preloadedNext = true;
                            $this.player().preloadNext(obj);
                        }
                    }, 1000);
                }

            });
            obj.audioSource.setVolume("0.0");
            obj.audioSource.play();
            obj.audioSource.setVolume("0.0");
            obj.audioSource.seekTo(obj.seekTime * 1000);
            console.log('Audio Source', JSON.stringify(obj));
            $this.playing.push(obj);
}

任何帮助或方向都会很棒。

3 个答案:

答案 0 :(得分:1)

以下示例代码运行正常,并在Android&amp; iOS设备:

<强>的index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <meta name="msapplication-tap-highlight" content="no" />
        <title>Hello World</title>
    </head>
    <body>
        <button id="playMp3">Play MP3</button><br><br>
        <button id="playMp3Mild">Play MP3 Mild</button><br><br>
        <button id="playRemoteFile">Play Remote File</button>

        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

<强> index.js:

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

function onDeviceReady() {
    document.querySelector("#playMp3").addEventListener("touchend", playMP3, false);
    document.querySelector("#playMp3Mild").addEventListener("touchend", playMp3Mild, false);
    document.querySelector("#playRemoteFile").addEventListener("touchend", playRemoteFile, false);

};

function playMP3() {
    var mp3URL = getMediaURL("sounds/button-1.mp3");
    var media = new Media(mp3URL, null, mediaError);
    media.setVolume(1.0);
    media.play();
}

function playMp3Mild() {
    var mp3URL = getMediaURL("sounds/button-1.mp3");
    var media = new Media(mp3URL, null, mediaError);
    media.setVolume(0.1);
    media.play();
}

function playRemoteFile() {
    var media = new Media("http://SERVER_IP:PORT/media/test.mp3");
    media.setVolume(0.1);
    media.play();
}

function getMediaURL(s) {
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s;
    return s;
}

function mediaError(e) {
    alert('Media Error');
    alert(JSON.stringify(e));
}

可以从github page下载示例应用。确保添加媒体和设备插件以进行相同的测试。可以在示例应用程序的README文件中找到更多信息。希望它有所帮助。

答案 1 :(得分:0)

我不确定这会解决您的问题,但可能音量未设置为零,因为setVolume也被调用&#34;早期&#34;。您能否尝试使用此代码并最终报告一些日志以优化我的解决方案?

play: function(src, seekTime)
        {
            App.Log.info('Playing audio source', src);
            seekTime = seekTime ? seekTime : 0;
            var obj = {
                source: src,
                startTime: new Date().getTime(),
                seekTime: seekTime,
                duration: 0,
                preloadedNext: false,
                audioSource: false
            };
            obj.audioSource = new Media(src, function(event){
                $this.player().onAudioEnd(event, obj);
            }, function(){
                //on error
            }, function(status)
            {
                obj.audioSource.setVolume("0.0");
                if(status == 2){
                    obj.audioSource.setVolume("0.0");
                    obj.audioSource.seekTo(obj.seekTime * 1000);
                    obj.timer = setInterval(function(){
                        obj.seekTime++;
                        obj.duration = obj.audioSource._duration;
                        if(obj.duration < 0){
                            return;
                        }
                        if(obj.seekTime >= (obj.duration - $this.default.fadeTime))
                        {
                            if(obj.preloadedNext){
                                return;
                            }
                            obj.preloadedNext = true;
                            $this.player().preloadNext(obj);
                        }
                    }, 1000);
                }

            });
            //obj.audioSource.setVolume("0.0");
            obj.audioSource.play();
            setTimeout(function() { //try to set volume to 0 after 10 milliseconds
               obj.audioSource.setVolume("0.0");
            }, 10);
            setTimeout(function() { //try to seekTo after 1 second
                obj.audioSource.seekTo(obj.seekTime * 1000);
            }, 1000);
            console.log('Audio Source', JSON.stringify(obj));
            $this.playing.push(obj);
}

答案 2 :(得分:0)

您可以设置一秒钟的静音延迟,看它是否至少产生了正确的延迟行为吗? (消除过程)

setTimeout(function() {
obj.audioSource.setVolume("0.0");
}, 1000);