audio - 有没有一种方法可以使用Google Apps脚本播放背景音乐?

标签 audio google-apps-script

我正在创建一个插件,要求用户从列表中选择音乐,并将其作为背景音乐播放。但是以前的帖子显示了侧边栏,用户手动按下了播放按钮。我想知道是否有一种方法只能与Google Apps脚本一起播放。另外,如果有用于设置音量的音量属性,那会有所帮助吗?

我的代码:

function onOpen(){
     DocumentApp.getUi()
          .createMenu("Background Music Add-On")
          .addItem("Select Music","music")
          .addItem("Set Volume","musicVol")
          .addToUi();
}

//music selection
function music(){

var musicName = DocumentApp.getUi()
     .prompt("Please select one of the music names:\n\nElevator Music,\nLeaf Rag.\nso on...")

     switch(musicName){
          case "Elevator":

               //code to play music Elevator

          break;

          //So On
     }
}

最佳答案

我以这个问题的答案作为起点:playing sound with google script

您将需要打开html边栏并使用音频标签,为此,您可以使用HtmlService类[1]。

作为总体背景,侧栏必须始终打开才能播放音乐。但是您仍然可以在编辑文档时播放音频。

要添加音频设置,可以将controls属性添加到音频标签[2]。为了自动播放音频,您可以添加autoplay附件[3]。

这是我为实现您的目标而实现的代码。该代码获取选定的值,并使用它将autoplay值更改为true并显示音频。另外,当select元件上对焦,它获取先前所选的值,以便以后(当选择一个新的值),它可以被用来停止之前的音频选择和不再显示它。为此,我使用了onchange [4]和onfocus [5]事件。

代码。gs

var SIDEBAR_TITLE = 'Sidebar Musicbox';

function onOpen(e) {
  DocumentApp.getUi()
  .createMenu('Custom Menu')
  .addItem('Show sidebar', 'showSidebar')
  .addToUi();
}

function showSidebar() {
  var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle(SIDEBAR_TITLE);
  DocumentApp.getUi().showSidebar(ui);
}

Sidebar.html
<!DOCTYPE html>
<html>
   <head>
      <base target="_top">
   </head>
   <body>
      <div class="sidebar branding-below">
         <p>
            A little music for your enjoyment!
         </p>
         <form>
            <select id="music" onchange="playSelection();" onfocus="setOldValue(this.value);">
               <option value="0">0</option>
               <option value="1">1</option>
               <option value="2">2</option>
            </select>
         </form>
         <br> 
         <audio id="player0" controls style="display:none">
            <source src="[WEB-URL-FOR-MP3-FILE]" type="audio/mpeg">
            Your browser does not support the audio element.
         </audio>
         <audio id="player1" controls style="display:none">
            <source src="[WEB-URL-FOR-MP3-FILE]" type="audio/mpeg">
            Your browser does not support the audio element.
         </audio>
         <audio id="player2" controls style="display:none">
            <source src="[WEB-URL-FOR-MP3-FILE]" type="audio/mpeg">
            Your browser does not support the audio element.
         </audio>
         <br> 
         <div id="sidebar-status"></div>
      </div>
      <div class="sidebar bottom">
         <span class="gray branding-text">Docs Add-on Sound Demo</span>
      </div>
   </body>
   <script>
      var previousValue;

      //Function called when select onFocus     
      function setOldValue(e) {
        previousValue = e;
      }

      //Function called when selected value change     
      function playSelection() {
        //Get the value for the selected option
        var selectedValue = document.getElementById("music").value;

        //Latest and previous selection IDs 
        var player = "player" + selectedValue;
        var previousPlayer = "player" + previousValue;

        //Stop and don't display the previous selection of audio
        document.getElementById(previousPlayer).style.display = "none";
        document.getElementById(previousPlayer).autoplay = false;
        document.getElementById(previousPlayer).load();

        //Play and display the new selection and put the focus on it
        document.getElementById(player).style.display = "block";
        document.getElementById(player).autoplay = true;
        document.getElementById(player).load();
        document.getElementById(player).focus();
      }   
   </script>
</html>

[1] https://developers.google.com/apps-script/guides/html/

[2] https://www.w3schools.com/tags/att_audio_controls.asp

[3] https://www.w3schools.com/tags/att_audio_autoplay.asp

[4] https://www.w3schools.com/jsref/event_onchange.asp

[5] https://www.w3schools.com/jsref/event_onfocus.asp

关于audio - 有没有一种方法可以使用Google Apps脚本播放背景音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57914935/

相关文章:

android - Android 手机上的采样率

actionscript-3 - 如何在 ActionScript 中拆分音频文件

macos - 将2个声卡合并到一个虚拟驱动程序中

audio - 将音频提取到标准输出 [youtube-dl]

postgresql - 如何从 Google 电子表格连接到 Heroku Postgres

google-apps-script - Google Apps 脚本,选择一个工作表 - 在第一个工作表选项卡中设置值 - 不是特定的工作表选项卡

ios - 每隔一段时间播放一次UILocalNotification声音

google-apps-script - 新的 Google 表格 : set text wrapping to Clip programmatically

javascript - 使用 Google Apps 脚本循环遍历整个列

google-apps-script - Google 脚本 - getNumCells 返回值(获取每行中的列数)