discord-jadx/app/src/main/java/com/discord/utilities/media/AppSoundManager.java
2021-06-27 22:44:35 +02:00

190 lines
7.2 KiB
Java

package com.discord.utilities.media;
import android.app.Application;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import com.discord.app.AppLog;
import com.discord.utilities.logging.Logger;
import d0.g;
import d0.k;
import d0.l;
import d0.z.d.m;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import kotlin.Lazy;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
/* compiled from: AppSoundManager.kt */
public final class AppSoundManager {
private final Context context;
private Map<Integer, SoundPlayer> soundPlayers = new LinkedHashMap();
/* compiled from: AppSoundManager.kt */
public static final class Provider {
public static final Provider INSTANCE = new Provider();
private static final Lazy INSTANCE$delegate = g.lazy(AppSoundManager$Provider$INSTANCE$2.INSTANCE);
private Provider() {
}
private final AppSoundManager getINSTANCE() {
return (AppSoundManager) INSTANCE$delegate.getValue();
}
public final AppSoundManager get() {
return getINSTANCE();
}
}
/* compiled from: AppSoundManager.kt */
public static final class SoundPlayer {
private MediaPlayer mediaPlayer;
public SoundPlayer(Context context, AppSound appSound, Function0<Unit> function0) {
m.checkNotNullParameter(context, "context");
m.checkNotNullParameter(appSound, "sound");
m.checkNotNullParameter(function0, "onCompletion");
MediaPlayer mediaPlayer = new MediaPlayer();
this.mediaPlayer = mediaPlayer;
if (mediaPlayer != null) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(appSound.getContentType()).setUsage(appSound.getUsage()).build());
AssetFileDescriptor openRawResourceFd = context.getResources().openRawResourceFd(appSound.getResId());
m.checkNotNullExpressionValue(openRawResourceFd, "assetFileDescriptor");
mediaPlayer.setDataSource(openRawResourceFd.getFileDescriptor(), openRawResourceFd.getStartOffset(), openRawResourceFd.getLength());
mediaPlayer.setLooping(appSound.getShouldLoop());
mediaPlayer.setOnCompletionListener(new AppSoundManager$SoundPlayer$$special$$inlined$apply$lambda$1(this, appSound, context, function0));
try {
mediaPlayer.prepare();
} catch (IOException unused) {
this.mediaPlayer = null;
}
}
}
public final boolean isPlaying() {
Boolean bool;
try {
k.a aVar = k.i;
MediaPlayer mediaPlayer = this.mediaPlayer;
boolean z2 = true;
if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
z2 = false;
}
bool = k.m78constructorimpl(Boolean.valueOf(z2));
} catch (Throwable th) {
k.a aVar2 = k.i;
bool = k.m78constructorimpl(l.createFailure(th));
}
Throwable r1 = k.m80exceptionOrNullimpl(bool);
if (r1 != null) {
AppLog.g.w("Error checking if MediaPlayer is playing", r1);
}
Boolean bool2 = Boolean.FALSE;
if (k.m82isFailureimpl(bool)) {
bool = bool2;
}
return ((Boolean) bool).booleanValue();
}
public final Unit release() {
MediaPlayer mediaPlayer = this.mediaPlayer;
if (mediaPlayer == null) {
return null;
}
mediaPlayer.release();
return Unit.a;
}
public final void start() {
Object obj;
Unit unit;
try {
k.a aVar = k.i;
MediaPlayer mediaPlayer = this.mediaPlayer;
if (mediaPlayer != null) {
mediaPlayer.start();
unit = Unit.a;
} else {
unit = null;
}
obj = k.m78constructorimpl(unit);
} catch (Throwable th) {
k.a aVar2 = k.i;
obj = k.m78constructorimpl(l.createFailure(th));
}
Throwable r3 = k.m80exceptionOrNullimpl(obj);
if (r3 != null) {
Logger.e$default(AppLog.g, "Error starting MediaPlayer in invalid state", r3, null, 4, null);
}
}
public final void stop() {
Object obj;
Unit unit;
try {
k.a aVar = k.i;
MediaPlayer mediaPlayer = this.mediaPlayer;
if (mediaPlayer != null) {
mediaPlayer.stop();
unit = Unit.a;
} else {
unit = null;
}
obj = k.m78constructorimpl(unit);
} catch (Throwable th) {
k.a aVar2 = k.i;
obj = k.m78constructorimpl(l.createFailure(th));
}
Throwable r0 = k.m80exceptionOrNullimpl(obj);
if (r0 != null) {
AppLog.g.w("Called stop on uninitialized MediaPlayer", r0);
}
}
}
public AppSoundManager(Application application) {
m.checkNotNullParameter(application, "application");
this.context = application;
}
public final boolean isPlaying(AppSound appSound) {
m.checkNotNullParameter(appSound, "sound");
return this.soundPlayers.containsKey(Integer.valueOf(appSound.getResId()));
}
public final void play(AppSound appSound) {
m.checkNotNullParameter(appSound, "sound");
if (isPlaying(appSound)) {
SoundPlayer soundPlayer = this.soundPlayers.get(Integer.valueOf(appSound.getResId()));
if (soundPlayer != null) {
soundPlayer.stop();
}
SoundPlayer soundPlayer2 = this.soundPlayers.get(Integer.valueOf(appSound.getResId()));
if (soundPlayer2 != null) {
soundPlayer2.release();
}
}
this.soundPlayers.put(Integer.valueOf(appSound.getResId()), new SoundPlayer(this.context, appSound, new AppSoundManager$play$1(this, appSound)));
SoundPlayer soundPlayer3 = this.soundPlayers.get(Integer.valueOf(appSound.getResId()));
if (soundPlayer3 != null) {
soundPlayer3.start();
}
}
public final void stop(AppSound appSound) {
SoundPlayer soundPlayer;
m.checkNotNullParameter(appSound, "sound");
SoundPlayer soundPlayer2 = this.soundPlayers.get(Integer.valueOf(appSound.getResId()));
if (!(soundPlayer2 == null || !soundPlayer2.isPlaying() || (soundPlayer = this.soundPlayers.get(Integer.valueOf(appSound.getResId()))) == null)) {
soundPlayer.stop();
}
SoundPlayer soundPlayer3 = this.soundPlayers.get(Integer.valueOf(appSound.getResId()));
if (soundPlayer3 != null) {
soundPlayer3.release();
}
this.soundPlayers.remove(Integer.valueOf(appSound.getResId()));
}
}