Skip to content

Instantly share code, notes, and snippets.

@MrVilkaman
Created November 12, 2016 18:40
Show Gist options
  • Save MrVilkaman/2d174b773283550cc9dcc4dae2af6744 to your computer and use it in GitHub Desktop.
Save MrVilkaman/2d174b773283550cc9dcc4dae2af6744 to your computer and use it in GitHub Desktop.
абстракция над AudioRecorder
// в domainlayer
public interface IAudioRecorder {
Observable<Void> start();
Observable<Void> stop();
}
//// в datalayer
public class AudioRecorder implements IAudioRecorder {
private AudioRecordDP recordDP;
private MediaRecorder recorder;
private String nextPathForAudio;
public AudioRecorder(AudioRecordDP recordDP) {
this.recordDP = recordDP;
nextPathForAudio = recordDP.getNextPathForAudio();
}
@Override
public Observable<Void> start() {
return Observable.create(subscriber -> {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(nextPathForAudio);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
recorder.setAudioEncodingBitRate(64);
recorder.setAudioSamplingRate(44100);
try {
recorder.prepare();
recorder.start();
if (!subscriber.isUnsubscribed())
subscriber.onCompleted();
} catch (IOException e) {
if (!subscriber.isUnsubscribed())
subscriber.onError(e);
}
});
}
public Observable<Void> stop() {
return Observable.create(subscriber -> {
if (recorder != null) {
try {
recorder.stop();
recorder.release();
if (!subscriber.isUnsubscribed())
subscriber.onCompleted();
recordDP.audioWasRecorder();
} catch (RuntimeException e) {
if (!subscriber.isUnsubscribed())
subscriber.onError(e);
} finally {
recorder = null;
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment