Piasy / RxAndroidAudio

Maybe the most robust Android Audio encapsulation library, with partial Rx support.
http://blog.piasy.com/2016/02/24/Robust-Android-Audio-encapsulation/
MIT License
1.57k stars 235 forks source link

StreamAudioPlayer can't play file recorded by AudioRecorder #24

Closed jaychang0917 closed 7 years ago

jaychang0917 commented 7 years ago

The auidoFile is recorded by

Observable.fromCallable(() -> {
      audioFile = new File(getContext().getExternalCacheDir(), UUID.randomUUID().toString() + ".m4a");
      return audioRecorder.prepareRecord(
        MediaRecorder.AudioSource.MIC,
        MediaRecorder.OutputFormat.MPEG_4,
        MediaRecorder.AudioEncoder.AAC,
        audioFile);
    })
      .doOnCompleted(() -> audioRecorder.startRecord())
      .flatMap(ignore -> RxAmplitude.from(audioRecorder))
      .compose(bindToLifecycle())
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(amp -> {
        LogUtils.logDebug("amp:" + amp);
      }, Throwable::printStackTrace);

And then process and play the audio when i click a stop button.

Observable.just(audioFile)
      .subscribeOn(Schedulers.io())
      .subscribe(file -> {
        try {
          audioPlayer.init();
          FileInputStream inputStream = new FileInputStream(file);
          int read;
          while ((read = inputStream.read(audioBuffer)) > 0) {
            audioPlayer.play(audioProcessor.process(0.5f, audioBuffer, StreamAudioRecorder.DEFAULT_SAMPLE_RATE), read);
          }
          inputStream.close();
          audioPlayer.release();
        } catch (IOException e) {
          e.printStackTrace();
        }
      });

The result is not a processed audio but some noise, have any idea?

Piasy commented 7 years ago

Of course it can't, the StreamAudioPlayer is used to play raw audio data, eg. PCM8, PCM16, and your recorded file is AAC encoded.

AudioRecorder and RxAudioPlayer are used in pair. StreamAudioRecorder and StreamAudioPlayer are used in pair.