B4A Library ExoPlayer - MediaPlayer / VideoView Alternative

Discussion in 'Additional libraries, classes and official updates' started by Erel, Nov 3, 2016.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    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):
    Code:
    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:
    Code:
    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:
    Code:
    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

    Version 1.10 - Based on ExoPlayer v2.5.3. Make sure to update the additional aar files as well.
     

    Attached Files:

    Last edited: Dec 3, 2018
  2. KZero

    KZero Active Member Licensed User

    amazing
    does it support RTSP ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. blong

    blong Active Member Licensed User

    Tried running example and had following message
    Could not find file 'C:\Program Files (x86)\Anywhere Software\Basic4android\libraries\exoplayer.aar'.

    Note extension is .aar ??
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. susu

    susu Well-Known Member Licensed User

    How can I add SimpleExoPlayerView1 to Activity by code? I tried the code below but my app crash

    Code:
    SimpleExoPlayerView1.Initialize("")
    player1.Initialize(
    "player")
    player1.Prepare(player1.CreateHLSSource(
    "http://video-site.com/file.mp4"))

    Activity.AddView(SimpleExoPlayerView1, 0050%x50%y)
    SimpleExoPlayerView1.Player = player1
     
    KZero likes this.
  7. Syd Wright

    Syd Wright Active Member Licensed User

    This looks like a very nice player. However, I get this error when running it:

    Generating R file. Error
    invalid resource directory name: D:\Android\ExoPlayerExample\Objects\bin\extra\res1\res/values-b+sr+Latn


    probably ...\res/values... should be ...\res\values... but where is this to be found?
     
    Last edited: Nov 4, 2016
  8. Erel

    Erel Administrator Staff Member Licensed User

    You cannot. However you can create a layout file with only the SimpleExoPlayerView and load this layout one or more times and do whatever you like with the view.

    It is not related to the slashes.

    Which version of Android SDK Build Tools (check it in Android SDK manager) are you using?
     
    Peter Simpson likes this.
  9. Syd Wright

    Syd Wright Active Member Licensed User

    I am using SDK Build Tools 19.1. Any higher version gives problems on my Windows-XP developer machine (see earlier thread). Is it possible to resolve this issue without the need for a higher version?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    The correct solution is to upgrade to Windows 7 and upgrade the build tools.

    You can try to:

    1. Open the aar file with 7zip.
    2. Delete the folder res\values-b+sr+Latn
     
  11. Syd Wright

    Syd Wright Active Member Licensed User

    Thank you. The problem is that I am very attached to my Windows-XP machine.

    I tried to delete res\values-b+sr+Latn using WinRar, but now get a list of errors when opening the Exoplayer project. I suppose I will have to install 7zip (or could I still use WinRar?).

    If that fails, what is the minimum SDK Build Tools version that I should use?
     
    Last edited: Nov 6, 2016
  12. Erel

    Erel Administrator Staff Member Licensed User

    You should use SDK build tools v23+.
     
  13. Edson Freire

    Edson Freire Member Licensed User

    com.google.android.exoplayer2.upstream.AssetDataSource$AssetDataSourceException: java.io.FileNotFoundException:

    I'm using correct path.

    Code:
    Main.Music.Prepare(Main.Music.CreateFileSource(File.DirAssets,"once_upon_a_time.mp3"))
    I already checked the file to see if exists, and yes, exist.

    Sorry for bad english..
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full error message?

    Does it happen in both debug mode and release mode?
     
  15. Edson Freire

    Edson Freire Member Licensed User

    I get this error in:
    Code:
    Music_Error (Message As String)
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    1. Can you upload the project?
    2. Did you test it in release mode?
     
  17. Edson Freire

    Edson Freire Member Licensed User

  18. Erel

    Erel Administrator Staff Member Licensed User

    Works fine here (note that exo should only be initialized when FirstTime is true).

    1. Are you testing it on a real device?
    2. Try to copy the file to File.DirInternal and then play it from there.
     
  19. DrAlex

    DrAlex Member Licensed User

    The example with the streaming video works fine, but when I try to play a video file from a web site, it hangs every few seconds for buffering. Is there a way to speed it up? It's not a problem of the connection speed, because in a browser the same file displays fast
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    No. The buffering is handled internally.
     
    wibbo likes this.
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice