ilia khosravi
ilia khosravi

Reputation: 1

how to reinitial media stream that stopped before?

I wanted to disappear the red recorder indicator so I used service.$html5AudioProps.mediaStream.getTracks().forEach(track => track.stop()); In my stop recording method but then when I start record again it won't record and also the red recording indicator won't appear so I realised that I should to re initial the media stream and other things. with this change the red recording indicator appear when I start again but it didn't record Here is the link of angular recorder directive that I used https://github.com/Telmediq/angular-audio-recorder

control.stopRecord = function () {
    var id = control.id;
    if (!service.isAvailable() || !status.isRecording) {
      return false;
    }

    var recordHandler = service.getHandler();
    var completed = function (blob) {
      $interval.cancel(timing);
      status.isRecording = false;
      var finalize = function (inputBlob) {
        control.audioModel = inputBlob;
        embedPlayer(inputBlob);
      };

      if (shouldConvertToMp3) {
        doMp3Conversion(blob, finalize);
      } else {
        finalize(blob)
      }

      embedPlayer(null);
      control.onRecordComplete();
    };

    //To stop recording
    if (service.isCordova) {
      cordovaMedia.recorder.stopRecord();
      window.resolveLocalFileSystemURL(cordovaMedia.url, function (entry) {
        entry.file(function (blob) {
          completed(blob);
        });
      }, function (err) {
        console.log('Could not retrieve file, error code:', err.code);
      });
    } else if (service.isHtml5) {
      // // ilia

      // console.log(service.$html5AudioProps.mediaStream)

      // service.$html5AudioProps.mediaStream.stop();
      recordHandler.stop();
      recordHandler.getBuffer(function () {
        recordHandler.exportWAV(function (blob) {
          completed(blob);
          scopeApply();
        });
      });

      service.$html5AudioProps.mediaStream.getTracks().forEach(track => track.stop());
      recordingIndicator = true;

    } else {
      recordHandler.stopRecording(id);
      completed(recordHandler.getBlob(id));
    }
  };

this is my stop recording code

control.startRecord = function () {
    if (!service.isAvailable()) {
      return;
    }

    if (status.isPlaying) {
      control.playbackPause();
      //indicate that this is not paused.
      status.playback = PLAYBACK.STOPPED;
    }

    //clear audio previously recorded
    // control.audioModel = null;

    var id = control.id, recordHandler = service.getHandler();
    //Record initiation based on browser type
    var start = function () {
      if (service.isCordova) {
        cordovaMedia.url = recorderUtils.cordovaAudioUrl(control.id);
        //mobile app needs wav extension to save recording
        cordovaMedia.recorder = new Media(cordovaMedia.url, function () {
          console.log('Media successfully played');
        }, function (err) {
          console.log('Media could not be launched' + err.code, err);
        });
        console.log('CordovaRecording');
        cordovaMedia.recorder.startRecord();
      }
      else if (service.isHtml5) {
        //HTML5 recording
        if (!recordHandler) {
          return;
        }
        console.log('HTML5Recording');
        // recordHandler.clear();


        

        

        if (recordingIndicator) {
          console.log(service.$html5AudioProps)
          navigator.mediaDevices.getUserMedia({"audio": true})
              .then(stream => {
                
                service.$html5AudioProps.mediaStream = stream;
                var audioContext = service.$html5AudioProps.audioContext;
                if (!audioContext || audioContext.state === 'closed') {
                  audioContext = new (window.AudioContext || window.webkitAudioContext)();
                  service.$html5AudioProps.audioContext = audioContext;
                }

                // Create an AudioNode from the stream.

                service.$html5AudioProps.audioInput = audioContext.createMediaStreamSource(stream);
                // service.$html5AudioProps.audioInput = audioContext.createMediaStreamSource(service.$html5AudioProps.audioInput.mediaStream);
                service.$html5AudioProps.inputPoint = audioContext.createGain();
                service.$html5AudioProps.audioInput.connect((service.$html5AudioProps.inputPoint = audioContext.createGain()));

                //analyser
                service.$html5AudioProps.analyserNode = audioContext.createAnalyser();
                service.$html5AudioProps.analyserNode.fftSize = 2048;
                service.$html5AudioProps.inputPoint.connect(service.$html5AudioProps.analyserNode);


                service.isReady = true;
                // service.$html5AudioProps.audioRecorder = new Recorder(service.$html5AudioProps.audioInput);
                // recordHandler = service.getHandler();
                service.$html5AudioProps.audioRecorder = service.getHandler();


                recordHandler = service.$html5AudioProps.audioRecorder
                recordHandler.record();
                recordingIndicator = false;



              })
              .catch(error => {
                // Handle errors if any
                console.error('Error accessing user media:', error);
              });
        }
        else{
          recordHandler.record();
        }




      }
      else {
        //Flash recording
        if (!service.isReady) {
          //Stop recording if the flash object is not ready
          return;
        }
        console.log('FlashRecording');
        recordHandler.record(id, 'audio.wav');
      }

      status.isRecording = true;
      control.onRecordStart();
      // control.elapsedTime = 0;


      control.elapsedTime = control.getDuration();
      timing = $interval(function () {
        ++control.elapsedTime;
        if (control.timeLimit && control.timeLimit > 0 && control.elapsedTime >= control.timeLimit) {
          control.stopRecord();
        }
      }, 1000);
    };


    if (service.isCordova || recordHandler) {
      start();
    } else if (!status.isDenied) {
      //probably permission was never asked
      service.showPermission({
        onDenied: function () {
          status.isDenied = true;
          $scope.$apply();
        },
        onAllowed: function () {
          status.isDenied = false;
          recordHandler = service.getHandler();
          start();
          scopeApply();

        }
      });
    }
  };

this is my start recording that i think should change

Upvotes: 0

Views: 32

Answers (0)

Related Questions