Add setAndroidAudioAttributes and androidAudioSessionIdStream.

This commit is contained in:
Ryan Heise 2020-08-14 14:38:55 +10:00
parent ed9a5ffdce
commit 64d55345f8
4 changed files with 111 additions and 1 deletions

View file

@ -9,6 +9,8 @@ import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.audio.AudioAttributes;
import com.google.android.exoplayer2.audio.AudioListener;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataOutput;
import com.google.android.exoplayer2.metadata.icy.IcyHeaders;
@ -46,7 +48,7 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
public class AudioPlayer implements MethodCallHandler, Player.EventListener, MetadataOutput {
public class AudioPlayer implements MethodCallHandler, Player.EventListener, AudioListener, MetadataOutput {
static final String TAG = "AudioPlayer";
@ -76,6 +78,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
private int errorCount;
private SimpleExoPlayer player;
private Integer audioSessionId;
private MediaSource mediaSource;
private Integer currentIndex;
private Map<LoopingMediaSource, MediaSource> loopingChildren = new HashMap<>();
@ -136,6 +139,15 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
handler.post(bufferWatcher);
}
@Override
public void onAudioSessionId(int audioSessionId) {
if (audioSessionId == C.AUDIO_SESSION_ID_UNSET) {
this.audioSessionId = null;
} else {
this.audioSessionId = audioSessionId;
}
}
@Override
public void onMetadata(Metadata metadata) {
for (int i = 0; i < metadata.length(); i++) {
@ -347,6 +359,10 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
case "concatenating.clear":
concatenating(args.get(0)).clear(handler, () -> result.success(null));
break;
case "setAndroidAudioAttributes":
setAudioAttributes((Map<?, ?>)args.get(0));
result.success(null);
break;
default:
result.notImplemented();
break;
@ -532,9 +548,19 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
player = new SimpleExoPlayer.Builder(context).build();
player.addMetadataOutput(this);
player.addListener(this);
player.addAudioListener(this);
}
}
private void setAudioAttributes(Map<?, ?> json) {
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setContentType((Integer)json.get("contentType"));
builder.setFlags((Integer)json.get("flags"));
builder.setUsage((Integer)json.get("usage"));
builder.setAllowedCapturePolicy((Integer)json.get("allowedCapturePolicy"));
player.setAudioAttributes(builder.build());
}
private void broadcastPlaybackEvent() {
final Map<String, Object> event = new HashMap<String, Object>();
event.put("processingState", processingState.ordinal());
@ -544,6 +570,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
event.put("icyMetadata", collectIcyMetadata());
event.put("duration", duration = getDuration());
event.put("currentIndex", currentIndex);
event.put("androidAudioSessionId", audioSessionId);
if (eventSink != null) {
eventSink.success(event);