javascript Google Apps脚本:如何播放谷歌驱动器上的音频文件与按钮点击

knsnq2tg  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(115)

我已经使用了一些现有的代码来创建一个网络应用程序,记录音频文件,并将其保存到我的谷歌驱动器。我想包括一个“消息”按钮,点击时播放音频文件,虽然这似乎不适合我......其他一切都按预期工作。真的很感激任何意见和建议。

<html>
<head>
    <title>Lame.js Upload Example</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
</head>

<body style="padding-top: 60px">

<div class="container">
    <div class="row">
        <div class="col-sm-8 col-sm-offset-2 text-center">
            <div>
                <h3 class="text-center" id="timer"></h3>
            </div>
            <img src="https://img.freepik.com/premium-photo/rotary-phone-dial-white-background_476612-10274.jpg?w=1380" width="400" height="300">
            <br><br>
            <button class="btn btn-primary" id="messageBtn">
                <i class="glyphicon glyphicon-envelope"></i> Message
            </button>

            <button class="btn btn-primary" id="startBtn">
                <i class="glyphicon glyphicon-record"></i> Start
            </button>

            <button class="btn btn-primary" id="stopBtn" disabled="true">
                <i class="glyphicon glyphicon-stop"></i> Stop
            </button>

            <ol class="convertedList" style="display:none"></ol>

        </div>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<?!= Include.js(['worker-realtime','mic']); ?>
<script>
    //on document ready
    $(function () {
        var recorder = new MP3Recorder({
            bitRate: 128
        }), timer;

        $('#startBtn').on('click', function (e) {
            e.preventDefault();
            var btn = $(this);
            recorder.start(function () {
                //start timer,
                var seconds = 0, updateTimer = function(){
                    $('#timer').text(seconds < 10 ? '0' + seconds : seconds);
                };
                timer = setInterval(function () {
                    seconds++;
                    updateTimer();
                }, 1000);
                updateTimer();
                //disable start button
                btn.attr('disabled', true);
                $('#stopBtn').removeAttr('disabled');
            }, function () {
                alert('We could not make use of your microphone at the moment');
            });
        });

        $('#stopBtn').on('click', function (e) {
            e.preventDefault();
            recorder.stop();
            $(this).attr('disabled', true);
            $('#startBtn').removeAttr('disabled');
            //get MP3
            clearInterval(timer);
            recorder.getMp3Blob(function (blob) {
                //var blobUrl = window.URL.createObjectURL(blob);
                blobToDataURL(blob, function(url){
                   var filename = 'recording_' +
                            (new Date()) +
                            '_.mp3';
                   google.script.run.withSuccessHandler(function(driveUrl){
                     //do nothing
                   }).invokeSave(url, filename);
                });
            }, function (e) {
                alert('We could not retrieve your message');
                console.log(e);
            });
        });

        function getAudioUrl(callback) {
             var fileId = "file ID here";
             var path = $("https://drive.google.com/uc?export=download&id=" + fileId).val();
              callback(path);
        }

       $('#messageBtn').on('click', function (e) {
             e.preventDefault();
             getAudioUrl(function(url) {
             var audio = new Audio(url);
             audio.preload = 'auto';
             audio.src = url;
             audio.play();
           });
        });
        
        function blobToDataURL(blob, callback) {
            var a = new FileReader();
            a.onload = function (e) {
                callback(e.target.result);
            }
            a.readAsDataURL(blob);
        }

    });
</script>
</body>

这段代码是我创建一个“消息按钮”并试图从我的驱动器播放音频文件的地方:

function getAudioUrl(callback) {
             var fileId = "file ID here";
             var path = $("https://drive.google.com/uc?export=download&id=" + fileId).val();
              callback(path);
        }

       $('#messageBtn').on('click', function (e) {
             e.preventDefault();
             getAudioUrl(function(url) {
             var audio = new Audio(url);
             audio.preload = 'auto';
             audio.src = url;
             audio.play();
           });
        });
sqxo8psd

sqxo8psd1#

在你的脚本中,下面的修改怎么样?

发件人:

var path = $("https://drive.google.com/uc?export=download&id=" + fileId).val();

收件人:

var path = "https://drive.google.com/uc?export=download&id=" + fileId;
  • 我担心在这种情况下,fileId的文件可能会被要求公开共享。如果这个修改不起作用,请公开共享fileId的文件并重新测试。

相关问题