Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Media Plugin setVolume() not working

Here is my play function: As you can see I setVolume() in multiple locations to 0. This has absolutely no effect. I've tried to also set to 0.8, 0.2, doesn't matter it wont work. I've also tried non string value, which doesn't really matter as the value is converted to a float val inside of the Obj-C module. I also NSLogged to ensure the value was being passed correctly and it is.

Testing with iPad iOS 9.2 | Cordova 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);
}

Any help or direction would be awesome.

like image 897
anthony.c Avatar asked Jun 27 '16 16:06

anthony.c


1 Answers

The following sample code works fine and it is tested in Android & iOS devices:

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));
}

The sample app can be downloaded from the github page. Ensure to add media and device plugin to test the same. More info can be found on the README file of the sample app. Hope it helps.

like image 108
Gandhi Avatar answered Nov 04 '22 20:11

Gandhi