Is it possible to check if it's actually a URL that can be loaded with the player before attempting to load it?
String mUrl = " --- ";
player = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(player);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "yourApplicationName"));
Uri uri = Uri.parse(mUrl);
MediaSource videoSource =
new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
player.prepare(videoSource);
Or if theres a library that does this?
One way to approach this would be to check the extension of the URL you are trying to load. For instance, the URL: http://thisisavideo/randompath/random_id.mp4
is most likely pointing to an mp4 resource. However, there are two main problems with this approach:
http://simpleurlhaha.com
could also be pointing to an mp4 resource.
One approach that's guaranteed to work however, is to do an HTTP Head request on the URL and check the Content-Type
. You can achieve this with Retrofit or any other Network Library you are using. If the URL is valid, you'll get a response similar to this:
HTTP/1.1 200 OK
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Accept-Ranges: bytes
Content-Length: 1034745
Content-Type: video/mp4
Date: Wed, 17 Sat 2020 13:01:19 GMT
Last-Modified: Fri, 8 Aug 2019 21:12:31 GMT
Server: Apache
As you can see, you can detect whether it's an MP4 or WebM URL by just doing an if-check on the Content-Type
field.
Personally, I think the suggestion above is expensive because it involves making an additional network call and that adds to the overall processing time. If this still fits your use-case. Please, go ahead.