playing sound with google script

前端 未结 3 1888
余生分开走
余生分开走 2020-12-21 11:23

Is there an easy way to incorporate a sound file into a Google Apps Script? I have seen a posting to try the following:

var audio = new Audio(\"alert.ogg\")         


        
相关标签:
3条回答
  • 2020-12-21 12:08

    To actually play an audio file with google script, and not just open a sidebar with a music player in it (which needs the user to activate the play button), you could add the following to Mogsdad's answer:

    • Make the audio play automatically when the sidebar opens
    • Make the sidebar close automatically when the audio finishes playing

    To make the audio play automatically, replace this in the html file:

    <audio id="player" controls>
     <source src="http://ukulelehunt.com/wp-content/uploads/2008/11/alohadechocobo.mp3" type="audio/mpeg">
     Your browser does not support the audio element.
    </audio>
    

    with this:

    <audio autoplay="true" src="http://yourdomain/youraudiofile.wav" />
    

    To make the sidebar close automatically when the audio finishes playing add this to the html file:

    <script>
     setTimeout(function(){ google.script.host.close(); }, 3000);
    </script>
    

    And replace the 3000 with the time in milliseconds your audio needs to play.

    It would be great to be able to automate the opening of the sidebar, be it with a time trigger or with a formula in a cell. If anybody knows how to achieve this, please share.

    0 讨论(0)
  • 2020-12-21 12:27

    Here's a simple example that embeds a music player into a document sidebar:

    screenshot

    Code.gs

    var SIDEBAR_TITLE = 'Sidebar Musicbox';
    
    /**
     * Adds a custom menu with items to show the sidebar and dialog.
     *
     * @param {Object} e The event parameter for a simple onOpen trigger.
     */
    function onOpen(e) {
      DocumentApp.getUi()
          .createAddonMenu()
          .addItem('Show sidebar', 'showSidebar')
          .addToUi();
    }
    
    /**
     * Runs when the add-on is installed; calls onOpen() to ensure menu creation and
     * any other initializion work is done immediately.
     *
     * @param {Object} e The event parameter for a simple onInstall trigger.
     */
    function onInstall(e) {
      onOpen(e);
    }
    
    /**
     * Opens a sidebar. The sidebar structure is described in the Sidebar.html
     * project file.
     */
    function showSidebar() {
      var ui = HtmlService.createTemplateFromFile('Sidebar')
          .evaluate()
          .setSandboxMode(HtmlService.SandboxMode.IFRAME)
          .setTitle(SIDEBAR_TITLE);
      DocumentApp.getUi().showSidebar(ui);
    }
    

    Sidebar.html

    <!-- Use a templated HTML printing scriptlet to import common stylesheet -->
    <?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
    
    <div class="sidebar branding-below">
        <p>
        A little music for your enjoyment!
        </p>
        <audio id="player" controls>
          <source src="http://ukulelehunt.com/wp-content/uploads/2008/11/alohadechocobo.mp3" type="audio/mpeg">
          Your browser does not support the audio element.
        </audio>
        <div id="sidebar-status"></div>
    </div>
    
    <div class="sidebar bottom">
      <span class="gray branding-text">Docs Add-on Sound Demo</span>
    </div>
    

    Stylesheet.html

    <!-- This CSS package applies Google styling; it should always be included. -->
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
    
    <style>
    #player {
     width: 95%;
    }
    </style>
    
    0 讨论(0)
  • 2020-12-21 12:29

    sure - this works

    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
      <audio id="audio_01" controls autoplay hidden="hidden">
      <source src="SHAKUHACHI_Masayuki_Koga_trim_mono-IMi00aV1tdA.mp3" type="audio/mpeg">
          Your browser does not support the audio element
        </audio>
    
        <script>
    
        var my_audio_01 = document.getElementById("audio_01");
    
        my_audio_01.play()
    
        </script>
      </body>
      </html>
    

    or this simpler version also works

    <body>
      <audio controls autoplay hidden="hidden">
        <source src="SHAKUHACHI_Masayuki_Koga_trim_mono-IMi00aV1tdA.mp3" type="audio/mpeg">
          Your browser does not support the audio element
      </audio>
    </body>
    
    0 讨论(0)
提交回复
热议问题