Created
November 12, 2016 18:43
-
-
Save MrVilkaman/492ade3a4bb059a3b9ca5e432c81b732 to your computer and use it in GitHub Desktop.
Абстракция над AudioPlayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// domainlayer | |
public interface AudioPlayer { | |
Observable<Integer> play(String pathToFile); | |
void stop(); | |
} | |
// datalayer | |
public class AudioPlayerImpl implements AudioPlayer { | |
private Context context; | |
private MediaPlayer mp; | |
public AudioPlayerImpl(Context context) { | |
this.context = context; | |
} | |
@Override | |
public Observable<Integer> play(String pathToFile) { | |
return Observable.create(subscriber -> { | |
stop(); | |
mp = MediaPlayer.create(context, Uri.parse(pathToFile)); | |
if (mp != null) { | |
subscriber.onNext(mp.getAudioSessionId()); | |
mp.start(); | |
mp.setOnCompletionListener(mp1 -> { | |
if (!subscriber.isUnsubscribed()) | |
subscriber.onCompleted(); | |
}); | |
mp.setOnErrorListener((mp1, what, extra) -> { | |
if (!subscriber.isUnsubscribed()) | |
subscriber.onError(new MediaPlayerError(what, extra)); | |
return false; | |
}); | |
subscriber.add(Subscriptions.create(this::stop)); | |
} else { | |
//?? | |
subscriber.onCompleted(); | |
} | |
}); | |
} | |
@Override | |
public void stop() { | |
if (mp != null) { | |
if (mp.isPlaying()) { | |
mp.stop(); | |
} | |
mp.release(); | |
mp = null; | |
} | |
} | |
} | |
// использование | |
public class PlayLastAudioUseCase extends GetCurrentOrLastAudioPath<Integer> { | |
private AudioPlayer audioPlayer; | |
@Inject | |
public PlayLastAudioUseCase(SchedulersProvider provider, AudioRecordDP audioRecordDP, | |
AudioPlayer audioPlayer) { | |
super(provider,audioRecordDP); | |
this.audioPlayer = audioPlayer; | |
} | |
@Override | |
protected Observable<Integer> buildUseCaseObservable() { | |
return getRecordPath() | |
.concatMap(s -> audioPlayer.play(s)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment