Search code examples
javascriptjqueryjplayer

Javascript syntax issue sessionStorage values to setting


First of all I should say that I am not very experienced with JavaScript and I would like some help on passing a sessionStorage value to a setting.

$(document).ready(function(){
    window.userSettings = null;

    $("#jquery_jplayer_1").jPlayer({
        ready: function () {
            $(this).jPlayer("setMedia", {
                title: "TestRadio",
                mp3: "http:/streamlink"
            });
        },
        swfPath: "jplayer/dist/jplayer",
        supplied: "mp3",
        wmode: "window",
        volume: "75",
        useStateClassSkin: true,
        loop: true,
        autoBlur: true,
        smoothPlayBar: true,
        keyEnabled: true,
        remainingDuration: false,
        toggleDuration: false
    });
});

function storeUserjPlayerSettings(){

var settings = new Object();

settings.volume = $("#jquery_jplayer_1").data().jPlayer.status.volume;
settings.paused = $("#jquery_jplayer_1").data().jPlayer.status.paused;
settings.src = $("#jquery_jplayer_1").data().jPlayer.status.src;

sessionStorage.setItem('userjPlayerSettings', JSON.stringify(settings));
window.userSettings = JSON.parse(sessionStorage.getItem('settings'));
}

What I would like to do is to pass the settings.volume web stored value to the volume parameter

$("#jquery_jplayer_1").jPlayer({
            ready: function () {
                $(this).jPlayer("setMedia", {
                    title: "TestRadio",
                    mp3: "http:/streamlink"
                });
            },
            swfPath: "jplayer/dist/jplayer",
            supplied: "mp3",
            wmode: "window",
            **volume**: "75",
            useStateClassSkin: true,
            loop: true,
            autoBlur: true,
            smoothPlayBar: true,
            keyEnabled: true,
            remainingDuration: false,
            toggleDuration: false
        });
    });

Solution

  • You are saving the volume earlier, so you just access it in the reverse process when you need it.

    For example, change the volume: 75 initialization to a call to a function that get the volume from your saved settings: volume: volumeSetting().

    Here's an example of how you might write that function itself:

    function volumeSetting() {
      var settings = sessionStorage.getItem("userjPlayerSettings");
      if (settings != null) {
         settings = JSON.parse(settings);
         if (typeof settings.volume == 'number')
           return settings.volume;
      }
      return 75;
    }