Remove Java compiler warnings.

This commit is contained in:
Ryan Heise 2020-12-23 17:55:01 +11:00
parent 845a1e4606
commit 2aa9342442
1 changed files with 63 additions and 55 deletions

View File

@ -5,6 +5,7 @@ import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
@ -31,6 +32,7 @@ import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import io.flutter.Log; import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.BinaryMessenger;
@ -69,7 +71,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
private Result prepareResult; private Result prepareResult;
private Result playResult; private Result playResult;
private Result seekResult; private Result seekResult;
private boolean seekProcessed;
private boolean playing; private boolean playing;
private Map<String, MediaSource> mediaSources = new HashMap<String, MediaSource>(); private Map<String, MediaSource> mediaSources = new HashMap<String, MediaSource>();
private IcyInfo icyInfo; private IcyInfo icyInfo;
@ -203,7 +204,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlaybackStateChanged(int playbackState) {
switch (playbackState) { switch (playbackState) {
case Player.STATE_READY: case Player.STATE_READY:
if (prepareResult != null) { if (prepareResult != null) {
@ -219,7 +220,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
} else { } else {
transition(ProcessingState.ready); transition(ProcessingState.ready);
} }
if (seekProcessed) { if (seekResult != null) {
completeSeek(); completeSeek();
} }
break; break;
@ -263,23 +264,14 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
errorCount++; errorCount++;
if (player.hasNext() && currentIndex != null && errorCount <= 5) { if (player.hasNext() && currentIndex != null && errorCount <= 5) {
int nextIndex = currentIndex + 1; int nextIndex = currentIndex + 1;
player.prepare(mediaSource); // TODO: pass in initial position here.
player.setMediaSource(mediaSource);
player.prepare();
player.seekTo(nextIndex, 0); player.seekTo(nextIndex, 0);
} }
} }
@Override
public void onSeekProcessed() {
if (seekResult != null) {
seekProcessed = true;
if (player.getPlaybackState() == Player.STATE_READY) {
completeSeek();
}
}
}
private void completeSeek() { private void completeSeek() {
seekProcessed = false;
seekPos = null; seekPos = null;
seekResult.success(new HashMap<String, Object>()); seekResult.success(new HashMap<String, Object>());
seekResult = null; seekResult = null;
@ -289,13 +281,12 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
public void onMethodCall(final MethodCall call, final Result result) { public void onMethodCall(final MethodCall call, final Result result) {
ensurePlayerInitialized(); ensurePlayerInitialized();
final Map<?, ?> request = (Map<?, ?>) call.arguments;
try { try {
switch (call.method) { switch (call.method) {
case "load": case "load":
Long initialPosition = getLong(request.get("initialPosition")); Long initialPosition = getLong(call.argument("initialPosition"));
Integer initialIndex = (Integer)request.get("initialIndex"); Integer initialIndex = call.argument("initialIndex");
load(getAudioSource(request.get("audioSource")), load(getAudioSource(call.argument("audioSource")),
initialPosition == null ? C.TIME_UNSET : initialPosition / 1000, initialPosition == null ? C.TIME_UNSET : initialPosition / 1000,
initialIndex, result); initialIndex, result);
break; break;
@ -307,53 +298,53 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setVolume": case "setVolume":
setVolume((float) ((double) ((Double) request.get("volume")))); setVolume((float) ((double) ((Double) call.argument("volume"))));
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setSpeed": case "setSpeed":
setSpeed((float) ((double) ((Double) request.get("speed")))); setSpeed((float) ((double) ((Double) call.argument("speed"))));
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setLoopMode": case "setLoopMode":
setLoopMode((Integer) request.get("loopMode")); setLoopMode((Integer) call.argument("loopMode"));
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setShuffleMode": case "setShuffleMode":
setShuffleModeEnabled((Integer) request.get("shuffleMode") == 1); setShuffleModeEnabled((Integer) call.argument("shuffleMode") == 1);
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setShuffleOrder": case "setShuffleOrder":
setShuffleOrder(request.get("audioSource")); setShuffleOrder(call.argument("audioSource"));
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "setAutomaticallyWaitsToMinimizeStalling": case "setAutomaticallyWaitsToMinimizeStalling":
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
case "seek": case "seek":
Long position = getLong(request.get("position")); Long position = getLong(call.argument("position"));
Integer index = (Integer)request.get("index"); Integer index = call.argument("index");
seek(position == null ? C.TIME_UNSET : position / 1000, index, result); seek(position == null ? C.TIME_UNSET : position / 1000, index, result);
break; break;
case "concatenatingInsertAll": case "concatenatingInsertAll":
concatenating(request.get("id")) concatenating(call.argument("id"))
.addMediaSources((Integer)request.get("index"), getAudioSources(request.get("children")), handler, () -> result.success(new HashMap<String, Object>())); .addMediaSources(call.argument("index"), getAudioSources(call.argument("children")), handler, () -> result.success(new HashMap<String, Object>()));
concatenating(request.get("id")) concatenating(call.argument("id"))
.setShuffleOrder(decodeShuffleOrder((List<Integer>)request.get("shuffleOrder"))); .setShuffleOrder(decodeShuffleOrder(call.argument("shuffleOrder")));
break; break;
case "concatenatingRemoveRange": case "concatenatingRemoveRange":
concatenating(request.get("id")) concatenating(call.argument("id"))
.removeMediaSourceRange((Integer)request.get("startIndex"), (Integer)request.get("endIndex"), handler, () -> result.success(new HashMap<String, Object>())); .removeMediaSourceRange(call.argument("startIndex"), call.argument("endIndex"), handler, () -> result.success(new HashMap<String, Object>()));
concatenating(request.get("id")) concatenating(call.argument("id"))
.setShuffleOrder(decodeShuffleOrder((List<Integer>)request.get("shuffleOrder"))); .setShuffleOrder(decodeShuffleOrder(call.argument("shuffleOrder")));
break; break;
case "concatenatingMove": case "concatenatingMove":
concatenating(request.get("id")) concatenating(call.argument("id"))
.moveMediaSource((Integer)request.get("currentIndex"), (Integer)request.get("newIndex"), handler, () -> result.success(new HashMap<String, Object>())); .moveMediaSource(call.argument("currentIndex"), call.argument("newIndex"), handler, () -> result.success(new HashMap<String, Object>()));
concatenating(request.get("id")) concatenating(call.argument("id"))
.setShuffleOrder(decodeShuffleOrder((List<Integer>)request.get("shuffleOrder"))); .setShuffleOrder(decodeShuffleOrder(call.argument("shuffleOrder")));
break; break;
case "setAndroidAudioAttributes": case "setAndroidAudioAttributes":
setAudioAttributes((Integer)request.get("contentType"), (Integer)request.get("flags"), (Integer)request.get("usage")); setAudioAttributes(call.argument("contentType"), call.argument("flags"), call.argument("usage"));
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
break; break;
default: default:
@ -409,20 +400,20 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
private void setShuffleOrder(final Object json) { private void setShuffleOrder(final Object json) {
Map<?, ?> map = (Map<?, ?>)json; Map<?, ?> map = (Map<?, ?>)json;
String id = (String)map.get("id"); String id = mapGet(map, "id");
MediaSource mediaSource = mediaSources.get(id); MediaSource mediaSource = mediaSources.get(id);
if (mediaSource == null) return; if (mediaSource == null) return;
switch ((String)map.get("type")) { switch ((String)mapGet(map, "type")) {
case "concatenating": case "concatenating":
ConcatenatingMediaSource concatenatingMediaSource = (ConcatenatingMediaSource)mediaSource; ConcatenatingMediaSource concatenatingMediaSource = (ConcatenatingMediaSource)mediaSource;
concatenatingMediaSource.setShuffleOrder(decodeShuffleOrder((List<Integer>)map.get("shuffleOrder"))); concatenatingMediaSource.setShuffleOrder(decodeShuffleOrder(mapGet(map, "shuffleOrder")));
List<Object> children = (List<Object>)map.get("children"); List<Object> children = mapGet(map, "children");
for (Object child : children) { for (Object child : children) {
setShuffleOrder(child); setShuffleOrder(child);
} }
break; break;
case "looping": case "looping":
setShuffleOrder(map.get("child")); setShuffleOrder(mapGet(map, "child"));
break; break;
} }
} }
@ -444,22 +435,29 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
switch ((String)map.get("type")) { switch ((String)map.get("type")) {
case "progressive": case "progressive":
return new ProgressiveMediaSource.Factory(buildDataSourceFactory()) return new ProgressiveMediaSource.Factory(buildDataSourceFactory())
.createMediaSource(new MediaItem.Builder()
.setUri(Uri.parse((String)map.get("uri")))
.setTag(id) .setTag(id)
.createMediaSource(Uri.parse((String)map.get("uri"))); .build());
case "dash": case "dash":
return new DashMediaSource.Factory(buildDataSourceFactory()) return new DashMediaSource.Factory(buildDataSourceFactory())
.createMediaSource(new MediaItem.Builder()
.setUri(Uri.parse((String)map.get("uri")))
.setMimeType(MimeTypes.APPLICATION_MPD)
.setTag(id) .setTag(id)
.createMediaSource(Uri.parse((String)map.get("uri"))); .build());
case "hls": case "hls":
return new HlsMediaSource.Factory(buildDataSourceFactory()) return new HlsMediaSource.Factory(buildDataSourceFactory())
.setTag(id) .createMediaSource(new MediaItem.Builder()
.createMediaSource(Uri.parse((String)map.get("uri"))); .setUri(Uri.parse((String)map.get("uri")))
.setMimeType(MimeTypes.APPLICATION_M3U8)
.build());
case "concatenating": case "concatenating":
MediaSource[] mediaSources = getAudioSourcesArray(map.get("children")); MediaSource[] mediaSources = getAudioSourcesArray(map.get("children"));
return new ConcatenatingMediaSource( return new ConcatenatingMediaSource(
false, // isAtomic false, // isAtomic
(Boolean)map.get("useLazyPreparation"), (Boolean)map.get("useLazyPreparation"),
decodeShuffleOrder((List<Integer>)map.get("shuffleOrder")), decodeShuffleOrder(mapGet(map, "shuffleOrder")),
mediaSources); mediaSources);
case "clipping": case "clipping":
Long start = getLong(map.get("start")); Long start = getLong(map.get("start"));
@ -484,7 +482,8 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
} }
private List<MediaSource> getAudioSources(final Object json) { private List<MediaSource> getAudioSources(final Object json) {
List<Object> audioSources = (List<Object>)json; if (!(json instanceof List)) throw new RuntimeException("List expected: " + json);
List<?> audioSources = (List<?>)json;
List<MediaSource> mediaSources = new ArrayList<MediaSource>(); List<MediaSource> mediaSources = new ArrayList<MediaSource>();
for (int i = 0 ; i < audioSources.size(); i++) { for (int i = 0 ; i < audioSources.size(); i++) {
mediaSources.add(getAudioSource(audioSources.get(i))); mediaSources.add(getAudioSource(audioSources.get(i)));
@ -522,7 +521,9 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
prepareResult = result; prepareResult = result;
transition(ProcessingState.loading); transition(ProcessingState.loading);
this.mediaSource = mediaSource; this.mediaSource = mediaSource;
player.prepare(mediaSource); // TODO: pass in initial position here.
player.setMediaSource(mediaSource);
player.prepare();
} }
private void ensurePlayerInitialized() { private void ensurePlayerInitialized() {
@ -686,7 +687,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
abortSeek(); abortSeek();
seekPos = position; seekPos = position;
seekResult = result; seekResult = result;
seekProcessed = false;
int windowIndex = index != null ? index : player.getCurrentWindowIndex(); int windowIndex = index != null ? index : player.getCurrentWindowIndex();
player.seekTo(windowIndex, position); player.seekTo(windowIndex, position);
} }
@ -716,7 +716,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
seekResult.success(new HashMap<String, Object>()); seekResult.success(new HashMap<String, Object>());
seekResult = null; seekResult = null;
seekPos = null; seekPos = null;
seekProcessed = false;
} }
} }
@ -728,6 +727,15 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
return (o == null || o instanceof Long) ? (Long)o : new Long(((Integer)o).intValue()); return (o == null || o instanceof Long) ? (Long)o : new Long(((Integer)o).intValue());
} }
@SuppressWarnings("unchecked")
static <T> T mapGet(Object o, String key) {
if (o instanceof Map) {
return (T) ((Map<?, ?>)o).get(key);
} else {
return null;
}
}
enum ProcessingState { enum ProcessingState {
none, none,
loading, loading,