Javascript 使用JQuery自动录制并允许播放一次

Javascript 使用JQuery自动录制并允许播放一次,javascript,jquery,audio,record,Javascript,Jquery,Audio,Record,大家好 Below is the sample code for record. <html> <body> <audio controls autoplay></audio> <input onclick="startRecording()" type="button" value="start recording" /> <input onclick="stopRecordin

大家好

         Below is the sample code for record.

<html>
  <body>
    <audio controls autoplay></audio>

    <input onclick="startRecording()" type="button" value="start recording" />
    <input onclick="stopRecording()" type="button" value="stop recording and play" />

    <script>
      var onFail = function(e) {
        console.log('Rejected!', e);
      };

      var onSuccess = function(s) {
        stream = s;
      }

      window.URL = window.URL || window.webkitURL;
      navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

      var stream;
      var audio = document.querySelector('audio');

      function startRecording() {
        if (navigator.getUserMedia) {
          navigator.getUserMedia({audio: true}, onSuccess, onFail);
        } else {
          console.log('navigator.getUserMedia not present');
        }
      }

      function stopRecording() {
        audio.src = window.URL.createObjectURL(stream);
      }
    </script>
  </body>
</html>
下面是用于记录的示例代码。

进度条将显示录音。同样,在音频播放完成后,我需要开始录音。如果有人知道此问题的解决方案,请帮助我。感谢您的支持
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <style>
    .center_div {
      width: 500px;
      height: 100px;
      background-color: #f5f5f5;
      border: 1px solid #808080;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -250px;
      /* half width*/
      margin-top: -50px;
      /* half height*/
      padding: 25px;
    }

    .recording_label {
      display: block;
      text-align: center;
      padding: 10px;
      font-family: sans-serif;
      font-size: 1.1em;
      margin-bottom: 25px;
    }

    .loader_bg {
      min-width: 100%;
      background: #c5c5c5;
      min-height: 20px;
      display: block;
    }

    .loader_bg1 {
      min-width: 90%;
      background: grey;
      min-height: 20px;
      display: inline-block;
      position: relative;
      top: -20px;
    }
  </style>
</head>

<body>


  <audio controls autoplay></audio>

  <input onclick="startRecording();" type="button" value="start recording" />
  <input onclick="stopRecording();" type="button" value="stop recording and play" />


  <div class="center_div">
    <span class="recording_label">Please wait...</span>
    <span class="loader_bg"></span>
    <span class="loader_bg1"></span>



  </div>




  <script>
    var onFail = function(e) {
      console.log('Rejected!', e);
    };

    var onSuccess = function(s) {
      stream = s;
    }

    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

    var stream;
    var audio = document.querySelector('audio');

    function startRecording() {
      if (navigator.getUserMedia) {
        navigator.getUserMedia({
          audio: true
        }, onSuccess, onFail);
      } else {
        console.log('navigator.getUserMedia not present');
      }
    }

    function stopRecording() {
      audio.src = window.URL.createObjectURL(stream);
    }

    $(function() {
      var max = 40;
      var count = max + 1;
      var counter = setInterval(timer, 1000);

      function timer() {
        count = count - 1;
        if (count <= 0) {
          clearInterval(counter);
          $(".recording_label").html("Recording...");
          $('.loader_bg1').css({
            'min-width': '' + (100) + '%'
          })
          startRecording();
          recordingSec(40);
          return;
        }
        $(".recording_label").html("Recording will begin in " + count + " sec.");
        var percent = (count / max) * 100;
        $('.loader_bg1').css({
          'min-width': '' + (100 - percent) + '%'
        })
      }
    });



    function recordingSec(sec) {
      var count = sec + 1;
      var counter = setInterval(timer, 1000);

      function timer() {
        count = count - 1;
        if (count <= 0) {
          clearInterval(counter);
          $(".recording_label").html("Recording stopped...Playing");
          $('.loader_bg1').css({
            'min-width': '' + (100) + '%'
          })
          stopRecording();
          return;
        }
        $(".recording_label").html("Recording started [ " + (sec - count) + " / " + sec + " ] sec.");
        var percent = (count / sec) * 100;
        $('.loader_bg1').css({
          'min-width': '' + (100 - percent) + '%'
        })
      }
    }
  </script>
</body>

</html>
中环分区{ 宽度:500px; 高度:100px; 背景色:#F5; 边框:1px实心#808080; 位置:绝对位置; 最高:50%; 左:50%; 左边距:-250px; /*半宽度*/ 利润上限:-50px; /*半高*/ 填充:25px; } .录音标签{ 显示:块; 文本对齐:居中; 填充:10px; 字体系列:无衬线; 字体大小:1.1米; 边缘底部:25px; } .loader_bg{ 最小宽度:100%; 背景:#C5C5; 最小高度:20px; 显示:块; } .loader_bg1{ 最小宽度:90%; 背景:灰色; 最小高度:20px; 显示:内联块; 位置:相对位置; 顶部:-20px; } 请稍候。。。 var onFail=函数(e){ console.log('Rejected!',e); }; var onSuccess=函数{ stream=s; } window.URL=window.URL | | window.webkitURL; navigator.getUserMedia=navigator.getUserMedia | | navigator.webkitGetUserMedia | | navigator.mozGetUserMedia | | navigator.msGetUserMedia; var流;
var audio=document.querySelector('audio'); 函数startRecording(){ if(navigator.getUserMedia){ navigator.getUserMedia({ 音频:正确 },onSuccess,onFail); }否则{ log('navigator.getUserMedia不存在'); } } 函数stopRecording(){ audio.src=window.URL.createObjectURL(流); } $(函数(){ var max=40; 变量计数=最大值+1; var计数器=设置间隔(计时器,1000); 函数计时器(){ 计数=计数-1; 如果(计数<代码> 中环分区{ 宽度:500px; 高度:100px; 背景色:#F5; 边框:1px实心#808080; 位置:绝对位置; 最高:50%; 左:50%; 左边距:-250px; /*半宽度*/ 利润上限:-50px; /*半高*/ 填充:25px; } .录音标签{ 显示:块; 文本对齐:居中; 填充:10px; 字体系列:无衬线; 字体大小:1.1米; 边缘底部:25px; } .loader_bg{ 最小宽度:100%; 背景:#C5C5; 最小高度:20px; 显示:块; } .loader_bg1{ 最小宽度:90%; 背景:灰色; 最小高度:20px; 显示:内联块; 位置:相对位置; 顶部:-20px; } 请稍候。。。 var onFail=函数(e){ console.log('Rejected!',e); }; var onSuccess=函数{ stream=s; } window.URL=window.URL | | window.webkitURL; navigator.getUserMedia=navigator.getUserMedia | | navigator.webkitGetUserMedia | | navigator.mozGetUserMedia | | navigator.msGetUserMedia; var流;
var audio=document.querySelector('audio'); 函数startRecording(){ if(navigator.getUserMedia){ navigator.getUserMedia({ 音频:正确 },onSuccess,onFail); }否则{ log('navigator.getUserMedia不存在'); } } 函数stopRecording(){ audio.src=window.URL.createObjectURL(流); } $(函数(){ var max=40; 变量计数=最大值+1; var计数器=设置间隔(计时器,1000); 函数计时器(){ 计数=计数-1; 如果(计数尝试使用:

使用Html代码:

<div style="text-align: left; padding: 10px;">
    <label style="margin-right: 10px; font-size: 14px !important;">Dictate Status</label>
    <a class="btn btn-primary-success pauseAudioDocWS pause" title="Pause" style="display: none; margin-right: 5px;"
        data-value="">
        <i class="imgpauseWS fa fa-lg fa-pause" style="cursor: pointer; margin: 2px 5px;"></i>
        <b class="ppauseWS btn-primary-success">Pause</b></a>
    <a class="btn btn-primary-success recordAudioDocWS inActiveWS" title="Start" style="margin-right: 20px;"
        data-value="">
        <b class="pplayWS">Click here to Start</b>
        <i class="imgplayWS fa fa-lg fa-microphone" style="cursor: pointer; margin: 2px 5px;"></i>
    </a>
    <span class="stopwatchWS" style="display: none; margin-right: 10px;">00:00:00</span>
</div>

支配地位

使用Jquery代码:

<script src="../js/StopTimer.js"></script>
<script src="../js/jquery.timer.js"></script>
<script src="../js/AudioRecorder.js"></script>
<script src="https://cdn.webrtc-experiment.com/RecordRTC.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/gif-recorder.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/gumadapter.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/DetectRTC.js" type="text/javascript"> </script>

<script type="text/javascript">
 $(document).ready(function () {
     var oldRecorderWS;
     $('.recordAudioDocWS').click(function () {
         var $this = $(this);
         if (oldRecorderWS != undefined) {
             if (oldRecorderWS != document.tmId) {
                 alert('Dictating Already In Progress, Please Stop Previous Dictating To Continue');
                 return;
             }
         }

         var checkin_id = document.tmId;
         localStorage.setItem('checkin_id', checkin_id);
         localStorage.setItem('Tran_Type', 'W');

         oldRecorderWS = document.tmId;
         var roomId = document.tmId;
         localStorage.setItem('roomId', roomId);
         var isRecording = false;

         $(".isActiveWS").each(function () {
             if ($(this).hasClass('isActiveWS')) {
                 isRecording = true;
             }
         });


         if (isRecording == true) {
             document.isRecordActivityPerforms = true;
             if (confirm('Dictating in progress do you want to stop and save?')) {
                 oldRecorderWS = undefined;
                 $this.next('span').css('display', 'none');
                 Example1.resetStopwatch();
                 $('.stoprecmessage').css('display', 'block');
                 $(".isActiveWS").each(function () {
                     $(this).addClass('inActiveWS');
                     $(this).removeClass('isActiveWS');
                     $(this).find('.pplayWS').text('Click here to Start');
                     //$(this).find('.imgplay').attr('src', 'img/play.png');
                     $(this).find('.imgplayWS').addClass('fa-stop');
                     $(this).find('.imgplayWS').addClass('fa-microphone');
                     $(this).attr('title', 'Start');

                 });
                 //$('.tbothers').css('pointer-events', 'auto');
                 $('.btn-ws-next').removeAttr('disabled', '');
                 $('.btn-ws-prv').removeAttr('disabled', '');
                 $this.prev('a').css('display', 'none');
                 $this.prev('a').addClass('pause');
                 StartRecording();
                 document.isRecordActivityPerform = false;

                 //$this.next().next('img').removeClass('hidden');

                 if ($(CurruntAudioRecRow).parent().parent().find('.hdtmvid').val() == document.tmId) {
                     $(CurruntAudioRecRow).parent().parent().find('td .audioList').removeClass();
                     $(CurruntAudioRecRow).parent().parent().find('td .REC').removeClass('hidden');
                     $(CurruntAudioRecRow).parent().parent().find('td .PEN').addClass('hidden');
                 }

             }
         }
         else {
             $('.btn-ws-next').attr('disabled', 'disabled');
             $('.btn-ws-prv').attr('disabled', 'disabled');
             document.isRecordActivityPerform = true;
             $this.next('span').css('display', 'inline');
             $this.next().next('img').addClass('hidden');
             Example1.init($this.next('span'));
             Example1.resetStopwatch();
             Example1.Timer.toggle();
             $this.removeClass('inActiveWS');
             $this.addClass('isActiveWS');
             $this.find('.pplayWS').text('Stop');
             //$this.find('.imgplay').attr('src', 'img/stop.png');
             $this.find('.imgplayWS').removeClass('fa-microphone');
             $this.find('.imgplayWS').addClass('fa-stop');
             $this.attr('title', 'Stop');
             $this.prev('a').css('display', 'inline-table');
             StartRecording();
         }

     });

     $('.pauseAudioDocWS').click(function () {
         document.isRecordActivityPerform = true;
         var $this = $(this);
         Example1.Timer.toggle();
         var btnStartRecording = document.querySelector('#btn-start-recording');
         if ($(this).hasClass('pause')) {
             btnStartRecording.recordRTC.pauseRecording();
             recordingPlayer.pause();
             $(this).addClass('resume');
             $(this).removeClass('pause');
             $(this).find('.ppauseWS').text('Resume');
             //$(this).find('.imgpause').attr('src', 'img/play.png');
             $(this).find('.imgpauseWS').removeClass('fa-pause');
             $(this).find('.imgpauseWS').addClass('fa-microphone');
             $(this).attr('title', 'Resume');
             $(this).next('a').css('pointer-events', 'none');
             $(this).next('a').attr('disabled', 'disabled');
         }
         else if ($(this).hasClass('resume')) {
             btnStartRecording.recordRTC.resumeRecording();
             recordingPlayer.play();
             $(this).addClass('pause');
             $(this).removeClass('resume');
             $(this).find('.ppauseWS').text('Pause');
             //$(this).find('.imgpause').attr('src', 'img/pause.png');
             $(this).find('.imgpauseWS').removeClass('fa-microphone');
             $(this).find('.imgpauseWS').addClass('fa-pause');
             $(this).attr('title', 'Pause');
             $(this).next('a').css('pointer-events', 'auto');
             $(this).next('a').removeAttr('disabled');
         }
     });
 });
</script>

$(文档).ready(函数(){
var旧记录器;
$('.recordAudioDocWS')。单击(函数(){
var$this=$(this);
if(oldRecorderWS!=未定义){
if(oldRecorderWS!=document.tmId){
警报(“口述已在进行中,请停止先前的口述以继续”);
返回;
}
}
var checkin_id=document.tmId;
setItem('checkin_id',checkin_id');
setItem('Tran_Type','W');
oldRecorderWS=document.tmId;
var roomId=document.tmId;
setItem('roomId',roomId);
var isRecording=false;
$(“.isActiveWS”)。每个(函数(){
if($(this).hasClass('isActiveWS')){
isRecording=true;
}
});
如果(isRecording==true){
document.isRecordActivityPerforms=true;
if(确认(‘正在听写您想停止并保存吗?’)){
oldRecorderWS=未定义;
$this.next('span').css('display','none');
示例1.重置秒表();
$('.stoprecmessage').css('display','block');
$(“.isActiveWS”)。每个(函数(){
$(this.addClass('inActiveWS');
$(this.removeClass('isActiveWS');
$(this).find('.pplayWS').text('单击此处开始');
//$(this.find('.imgplay').attr('src','img/play.png');
$(this.find('.imgplayWS').addClass('fa-stop');
$(this.find('.imgplayWS').addClass('fa-micromic');
$(this.attr('title','Start');
});
//$('.tbothers').css('pointer-events','auto');
$('.btn ws next').removeAttr('disabled','');
$('.btn ws prv').removeAttr('disabled','');
$this.prev('a').css('display','none');
$
<script src="../js/StopTimer.js"></script>
<script src="../js/jquery.timer.js"></script>
<script src="../js/AudioRecorder.js"></script>
<script src="https://cdn.webrtc-experiment.com/RecordRTC.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/gif-recorder.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/gumadapter.js" type="text/javascript"></script>
<script src="https://cdn.webrtc-experiment.com/DetectRTC.js" type="text/javascript"> </script>

<script type="text/javascript">
 $(document).ready(function () {
     var oldRecorderWS;
     $('.recordAudioDocWS').click(function () {
         var $this = $(this);
         if (oldRecorderWS != undefined) {
             if (oldRecorderWS != document.tmId) {
                 alert('Dictating Already In Progress, Please Stop Previous Dictating To Continue');
                 return;
             }
         }

         var checkin_id = document.tmId;
         localStorage.setItem('checkin_id', checkin_id);
         localStorage.setItem('Tran_Type', 'W');

         oldRecorderWS = document.tmId;
         var roomId = document.tmId;
         localStorage.setItem('roomId', roomId);
         var isRecording = false;

         $(".isActiveWS").each(function () {
             if ($(this).hasClass('isActiveWS')) {
                 isRecording = true;
             }
         });


         if (isRecording == true) {
             document.isRecordActivityPerforms = true;
             if (confirm('Dictating in progress do you want to stop and save?')) {
                 oldRecorderWS = undefined;
                 $this.next('span').css('display', 'none');
                 Example1.resetStopwatch();
                 $('.stoprecmessage').css('display', 'block');
                 $(".isActiveWS").each(function () {
                     $(this).addClass('inActiveWS');
                     $(this).removeClass('isActiveWS');
                     $(this).find('.pplayWS').text('Click here to Start');
                     //$(this).find('.imgplay').attr('src', 'img/play.png');
                     $(this).find('.imgplayWS').addClass('fa-stop');
                     $(this).find('.imgplayWS').addClass('fa-microphone');
                     $(this).attr('title', 'Start');

                 });
                 //$('.tbothers').css('pointer-events', 'auto');
                 $('.btn-ws-next').removeAttr('disabled', '');
                 $('.btn-ws-prv').removeAttr('disabled', '');
                 $this.prev('a').css('display', 'none');
                 $this.prev('a').addClass('pause');
                 StartRecording();
                 document.isRecordActivityPerform = false;

                 //$this.next().next('img').removeClass('hidden');

                 if ($(CurruntAudioRecRow).parent().parent().find('.hdtmvid').val() == document.tmId) {
                     $(CurruntAudioRecRow).parent().parent().find('td .audioList').removeClass();
                     $(CurruntAudioRecRow).parent().parent().find('td .REC').removeClass('hidden');
                     $(CurruntAudioRecRow).parent().parent().find('td .PEN').addClass('hidden');
                 }

             }
         }
         else {
             $('.btn-ws-next').attr('disabled', 'disabled');
             $('.btn-ws-prv').attr('disabled', 'disabled');
             document.isRecordActivityPerform = true;
             $this.next('span').css('display', 'inline');
             $this.next().next('img').addClass('hidden');
             Example1.init($this.next('span'));
             Example1.resetStopwatch();
             Example1.Timer.toggle();
             $this.removeClass('inActiveWS');
             $this.addClass('isActiveWS');
             $this.find('.pplayWS').text('Stop');
             //$this.find('.imgplay').attr('src', 'img/stop.png');
             $this.find('.imgplayWS').removeClass('fa-microphone');
             $this.find('.imgplayWS').addClass('fa-stop');
             $this.attr('title', 'Stop');
             $this.prev('a').css('display', 'inline-table');
             StartRecording();
         }

     });

     $('.pauseAudioDocWS').click(function () {
         document.isRecordActivityPerform = true;
         var $this = $(this);
         Example1.Timer.toggle();
         var btnStartRecording = document.querySelector('#btn-start-recording');
         if ($(this).hasClass('pause')) {
             btnStartRecording.recordRTC.pauseRecording();
             recordingPlayer.pause();
             $(this).addClass('resume');
             $(this).removeClass('pause');
             $(this).find('.ppauseWS').text('Resume');
             //$(this).find('.imgpause').attr('src', 'img/play.png');
             $(this).find('.imgpauseWS').removeClass('fa-pause');
             $(this).find('.imgpauseWS').addClass('fa-microphone');
             $(this).attr('title', 'Resume');
             $(this).next('a').css('pointer-events', 'none');
             $(this).next('a').attr('disabled', 'disabled');
         }
         else if ($(this).hasClass('resume')) {
             btnStartRecording.recordRTC.resumeRecording();
             recordingPlayer.play();
             $(this).addClass('pause');
             $(this).removeClass('resume');
             $(this).find('.ppauseWS').text('Pause');
             //$(this).find('.imgpause').attr('src', 'img/pause.png');
             $(this).find('.imgpauseWS').removeClass('fa-microphone');
             $(this).find('.imgpauseWS').addClass('fa-pause');
             $(this).attr('title', 'Pause');
             $(this).next('a').css('pointer-events', 'auto');
             $(this).next('a').removeAttr('disabled');
         }
     });
 });
</script>