Flowplayer和ipad条件切换

Flowplayer and ipad conditional switching

本文关键字:条件 ipad Flowplayer      更新时间:2023-09-26

我正在使用这段代码将流播放器放在页面上,它几乎使流播放器具有通用性:它几乎适用于所有的PC浏览器以及iPhone、iPad、android设备。。。

<script>
  $(document).ready(function() {
    if (navigator.userAgent.match(/Android/i)
       || navigator.userAgent.match(/webOS/i)
       || navigator.userAgent.match(/iPhone/i)
       || navigator.userAgent.match(/iPad/i)
       || navigator.userAgent.match(/iPod/i)
       || navigator.userAgent.match(/BlackBerry/i)
      ) {
          $f("player", "/flowplayer-3.2.16.swf", {
            clip: {
              provider: "rtmp",
              url: "streamname",
              ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
              bufferLength: 2,
              live: true,
            },
            plugins: {
              rtmp: {
                url: "/flowplayer.rtmp-3.2.12.swf",
                netConnectionUrl: "rtmp://wowza.example:1935/live"
              }
            }
          }).ipad({ simulateiDevice: true, controls: false });
      } 
      else {
          $f("player", "/flowplayer-3.2.16.swf", {
            clip: {
              provider: "rtmp",
              url: "streamname",
              ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
              bufferLength: 2,
              live: true,
            },
            plugins: {
              rtmp: {
                url: "/flowplayer.rtmp-3.2.12.swf",
                netConnectionUrl: "rtmp://wowza.example:1935/live"
              }
            }
          }).ipad();
      }
  });
</script>

我的问题是关于干净的代码。现在代码是有效的,但在我看来,我正在复制整个东西,ifelse之间唯一不同的是最后一行:

}).ipad({ simulateiDevice: true, controls: false });

}).ipad();

有没有办法在ipad()函数中移动if子句?作为一名jQuery或javascript开发人员,我一直在尝试做这件事,但我一无所获。。。

最简单的方法是为选项值simulateiDevicecontrols创建变量。然后使用if语句设置它们的值。

示例

<script>
  $(document).ready(function() {
    var simulateiDevice,
        controls;
    if (navigator.userAgent.match(/Android/i) || 
        navigator.userAgent.match(/webOS/i) || 
        navigator.userAgent.match(/iPhone/i) || 
        navigator.userAgent.match(/iPad/i) || 
        navigator.userAgent.match(/iPod/i) || 
        navigator.userAgent.match(/BlackBerry/i)) {
      simulateiDevice = false;
      controls = true;
    } else {
      simulateiDevice = true;
      controls = false;
}
$f("player", "/flowplayer-3.2.16.swf", {
  clip: {
    provider: "rtmp",
    url: "streamname",
    ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
    bufferLength: 2,
    live: true,
  },
  plugins: {
    rtmp: {
      url: "/flowplayer.rtmp-3.2.12.swf",
      netConnectionUrl: "rtmp://wowza.example:1935/live"
    }
  }
  }).ipad({
    simulateiDevice: simulateiDevice,
    controls: controls
  });
}); 
< /script>