From 54721ad7b17dfbb4e12fe50095b6cc966434eedc Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Wed, 29 Apr 2020 23:33:17 +1000 Subject: [PATCH] Workaround to supply file extension to ExoPlayer --- .../com/ryanheise/just_audio/AudioPlayer.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java index f5f4a2f..c7a9fa1 100644 --- a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java +++ b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java @@ -371,9 +371,10 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met ); DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, httpDataSourceFactory); Uri uri = Uri.parse(url); - if (uri.getPath().toLowerCase().endsWith(".mpd")) { + String extension = getLowerCaseExtension(uri); + if (extension.equals("mpd")) { mediaSource = new DashMediaSource.Factory(dataSourceFactory).createMediaSource(uri); - } else if (uri.getPath().toLowerCase().endsWith(".m3u8")) { + } else if (extension.equals("m3u8")) { mediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri); } else { mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri); @@ -381,6 +382,16 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met player.prepare(mediaSource); } + private String getLowerCaseExtension(Uri uri) { + // Until ExoPlayer provides automatic detection of media source types, we + // rely on the file extension. When this is absent, as a temporary + // workaround we allow the app to supply a fake extension in the URL + // fragment. e.g. https://somewhere.com/somestream?x=etc#.m3u8 + String fragment = uri.getFragment(); + String filename = fragment != null && fragment.contains(".") ? fragment : uri.getPath(); + return filename.replaceAll("^.*\\.", "").toLowerCase(); + } + public void setClip(final Long start, final Long end, final Result result) { if (state == PlaybackState.none) { throw new IllegalStateException("Cannot call setClip from none state");