Workaround to supply file extension to ExoPlayer

This commit is contained in:
Ryan Heise 2020-04-29 23:33:17 +10:00
parent a5f422942b
commit 54721ad7b1
1 changed files with 13 additions and 2 deletions

View File

@ -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");