B4A Library ExoPlayer - MediaPlayer / VideoView Alternative

Status
Not open for further replies.
Edit: new version: https://www.b4x.com/android/forum/threads/exoplayer-media3-video-player.158204/

ExoPlayer is an open source project that replaces the native MediaPlayer and VideoView types.
https://google.github.io/ExoPlayer/
License: Apache 2.0

It supports more media formats, it is more powerful and more customizable.
It is supported by Android 4.1+ (API 16+).

upload_2016-11-3_17-10-9.png


There are two types: SimpleExoPlayer and SimpleExoPlayerView.

SimpleExoPlayer is the internal engine, it is similar to MediaPlayer. SimpleExoPlayerView provides the interface.
SimpleExoPlayer can be used without the interface.
SimpleExoPlayer should be a process global variable.

Example of video playback from an asset file (same exact code will work with audio files as well):
B4X:
Sub Process_Globals
   Private player1 As SimpleExoPlayer
End Sub

Sub Globals
   Private SimpleExoPlayerView1 As SimpleExoPlayerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     player1.Initialize("player")
     player1.Prepare(player1.CreateFileSource(File.DirAssets, "BLE_Chat.mp4"))
   End If
   Activity.LoadLayout("1")
   SimpleExoPlayerView1.Player = player1 'Connect the interface to the engine
End Sub
You can use player.CreateUriSource to load a remote resource:
B4X:
player1.Prepare(player1.CreateUriSource("https://..."))
Or you can use one of the following methods for streaming resources:

CreateHLSSource - Http Live Streaming
CreateDashSource - Dynamic Adaptive Streaming over Http
CreateSmoothStreamingSource - Smooth Streaming


You can also create a playlist with multiple sources:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     player1.Initialize("player")
     Dim sources As List
     sources.Initialize
     sources.Add(player1.CreateDashSource("http://www.youtube.com/api/manifest/dash/id/bf5bb2419360daf1/source/youtube?as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,source,id,as&ip=0.0.0.0&ipbits=0&expire=19000000000&signature=51AF5F39AB0CEC3E5497CD9C900EBFEAECCCB5C7.8506521BFC350652163895D4C26DEE124209AA9E&key=ik0"))
     sources.Add(player1.CreateHLSSource("https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"))
     sources.Add(player1.CreateUriSource("http://html5demos.com/assets/dizzy.mp4"))
     player1.Prepare(player1.CreateListSource(sources))

   End If
   Activity.LoadLayout("1")
   SimpleExoPlayerView1.Player = player1
   player1.Play
End Sub

The library is attached.


It depends on several additional aar files (should be copied to the additional libraries folder):
www.b4x.com/android/files/exoplayer-additional_libs.zip

Updates:

V1.52 - Previous fix was incomplete. New version solves the issue.
V1.51 - Fixes an incompatibility between ExoPlayer and Firebase. Make sure to update the additional libs package.
V1.50 - New version based on ExoPlayer 2.13.3 (https://github.com/google/ExoPlayer/blob/release-v2/RELEASENOTES.md). The additional libs package was also updated.
V1.41 - Fixes a bug where the designer properties where missing.
V1.40 - Based on ExoPlayer v2.11.3. Make sure to update the additional libs as well.

V1.30 - New InitializeCustom method that allows creating the native player with JavaObject.

V1.20 - Based on ExoPlayer v2.10.2. This is a large update. Not all existing customizations (based on JavaObject) will work without modifications.

V1.10 - Based on ExoPlayer v2.5.3.

Make sure to update the additional aar files as well.
 

Attachments

  • ExoPlayerExample.zip
    14 KB · Views: 3,204
  • ExoPlayer.zip
    10.1 KB · Views: 2,692
Last edited:

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Forum,

i want to show 3 or 4 videos from youtube one after the other with the ExoPlayer.
Unfortunately, this does not work at the moment.

If I copy the string from the video into the PC browser, then the video is displayed on the PC.
But if I add the string with ExoPlayer to the list (in the code) and start the app, it does NOT work.

This example here works well:
B4X:
sources.Add (player1.CreateHLSSource ( "https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"))

... but this code does NOT work (it is a Tutorial for B4A Beginners on YouTube):
B4X:
Sources.Add (player1.CreateHLSSource ("https://www.youtube.com/watch?v=DmBXrwEYUMQ"))

The error message from the Logfile:
com.google.android.exoplayer2.ParserException: Failed to parse the playlist, could not identify any tags.

The question is: Which tags should be identified?
There are no tags defined in the working example code above.

The app should show 3-4 videos, which are stored on YouTube and it should be possible to choose a video by clicking on a Spinner.
... but in the moment I do not get even one single video to run!

Could someone please post an example code, wich can display for example the "Tutorial for B4A-Beginners" or another video on YouTube?

Thank you in advance ... i'm grateful for every tip.
 

Ehsan Tamjidi

Member
Licensed User
Longtime User
hi there
I know this is too late
Generating R File Error on release but I have no issues with debug mode, thanks for your answers

cheers
 

Kwame Twum

Active Member
Licensed User
Longtime User
Hello, everyone.
I used an api [saveitoffline - https://www.saveitoffline.com/api/] to get video url from a source which exoplayer couldn't play directly.
The resulting url from one of them is: https://www.saveitoffline.com/get/?i=dxRJIKd8u6krPBi4Rx2AcJ0ide6inyJp

The above url when directly accessed attempts to download the video, indicating it indeed has been saved.
However, I get errors when I try to create a source out of the url. Below are the errors.
B4X:
'vi is a SimpleExoPlayer instance
'act is the url derived from the api

vi.Prepare(vi.CreateHLSSource(act)) 'Error: com.google.android.exoplayer2.ParserException: Failed to parse the playlist, could not identify any tags.

vi.Prepare(vi.CreateUriSource(act)) 'Error: com.google.android.exoplayer2.source.ExtractorMediaSource$UnrecognizedInputFormatException: None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor) could read the stream.

vi.Prepare(vi.CreateDashSource(act)) 'Error: com.google.android.exoplayer2.ParserException: inputStream does not contain a valid media presentation description

vi.Prepare(vi.CreateSmoothStreamingSource(act)) 'Error: com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 404

Any idea what could be done about this?
 

analizer3816

Member
Licensed User
Longtime User
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>27BB93CADACCC7FA</RequestId><HostId>6Ghq8BqFyzfeZQEj6NQQqfZsz07jNrIvZHUpF6tqewslnri9nS1ezC/8/0gpTt4zw5jBLD3oDsE=</HostId></Error>
Hello, everyone.
I used an api [saveitoffline - https://www.saveitoffline.com/api/] to get video url from a source which exoplayer couldn't play directly.
The resulting url from one of them is: https://www.saveitoffline.com/get/?i=dxRJIKd8u6krPBi4Rx2AcJ0ide6inyJp

The above url when directly accessed attempts to download the video, indicating it indeed has been saved.
However, I get errors when I try to create a source out of the url. Below are the errors.
B4X:
'vi is a SimpleExoPlayer instance
'act is the url derived from the api

vi.Prepare(vi.CreateHLSSource(act)) 'Error: com.google.android.exoplayer2.ParserException: Failed to parse the playlist, could not identify any tags.

vi.Prepare(vi.CreateUriSource(act)) 'Error: com.google.android.exoplayer2.source.ExtractorMediaSource$UnrecognizedInputFormatException: None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor) could read the stream.

vi.Prepare(vi.CreateDashSource(act)) 'Error: com.google.android.exoplayer2.ParserException: inputStream does not contain a valid media presentation description

vi.Prepare(vi.CreateSmoothStreamingSource(act)) 'Error: com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 404

Any idea what could be done about this?

I open this file and got this, can you give me source url?
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>27BB93CADACCC7FA</RequestId><HostId>6Ghq8BqFyzfeZQEj6NQQqfZsz07jNrIvZHUpF6tqewslnri9nS1ezC/8/0gpTt4zw5jBLD3oDsE=</HostId></Error>
 

Kwame Twum

Active Member
Licensed User
Longtime User
Thanks for the assistance, will try it out.
 

cruzeiro991

Member
Licensed User
Longtime User
Hello.
I'm trying to make a app to play streamings by uri but the exoplayer only plays the first url, after release, initialize and create new resource it's play the same video. What's happening?
 

Lucas Eduardo

Active Member
Licensed User
How can i play https video in exoplayer? Because when i try to play in VLC, for example, It ask me to accept the certificate. Is there a way to do this in exoplayer?



I get the following error Error: com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to https://.....rest of the link
 

Shivito1

Active Member
Licensed User
How can i play https video in exoplayer? Because when i try to play in VLC, for example, It ask me to accept the certificate. Is there a way to do this in exoplayer?



I get the following error Error: com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to https://.....rest of the link
There are examples here : https://www.b4x.com/android/forum/threads/exoplayer-mediaplayer-videoview-alternative.72652/#content

You may need to get the direct link for the video you wish to stream. In the past I have used YouTube-dl Which is not a library but an executable I ran on my computer. Sometimes you can find a direct link in the source code of the webpage. Not simplistic nore automated approach though.
 
Status
Not open for further replies.
Top