B4A Library PndAudioExoPlayer - For online radio stations apps

Original library: https://github.com/google/ExoPlayer
Wrapper is based on ExoPlayer version v2.17.1 (10 Mar 2022)

This wrapper is no substitute for Erel's ExoPlayer, it was created with one goal in mind, to make it easier to create applications that use the online radio streams.

A few things have been added or made easier in relation to Erel's:
  • Handle audio focus - temporary mute player on call, SMS, etc.
  • Player's user agent
  • Radio station metadata
  • Stream metadata
  • Show song name, artist and album art (image) on remote Bluetooth device
  • Control player from remote device (smart watch, car's steering wheel, headphones, etc.)
  • Audio session ID for future improvements.

Some of the data you can extract from metadata: radio station name, song name, bitrate, radio station genre, radio station's url, etc.
Library can be used in Activity/B4XPage/Service.
Requires: B4A v11.5+, minSdkVersion: 21+

Screenshot.png

This is how example app looks on my Samsung (Tizen) watch, similar look and same functionality on Android and Huawei watches:
img.jpg

If you use not secure HTTP protocol for streams, you need to add this code in manifest:
B4X:
CreateResourceFromFile(Macro, Core.NetworkClearText)

PndAudioExoPlayer

Author:
Author: Google - B4a Wrapper: Pendrush
Version: 1.21
  • PndAudioExoPlayer
    • Events:
      • MediaSessionOnButton (KeyCode As Int)
      • MediaSessionOnPause
      • MediaSessionOnPlay
      • MediaSessionOnStop
      • OnAudioSessionIdChanged (AudioSessionId As Int)
      • OnIsPlayingChanged (IsPlaying As Boolean)
      • OnMetadata (Metadata As String)
      • OnPlaybackStateChanged (PlaybackState As Int)
      • OnPlayerError (Error As String)
      • OnTracksInfoChanged (Metadata As String)
    • Functions:
      • AudioSessionId As Int
        Returns the audio session identifier.
      • HlsMediaSource (Url As String)
        For: HLS streams (stream Url ends with .m3u8)
        Url - Radio station stream Url
      • Initialize (EventName As String, UserAgent As String, HandleAudioFocus As Boolean, AllowCrossProtocolRedirects As Boolean)
        Initialize player.
        EventName - Event name.
        UserAgent - User agent of player.
        HandleAudioFocus - Whether the player should handle audio focus, for example temporary mute player on call, SMS, etc.
        AllowCrossProtocolRedirects - Allow redirect from HTTPS to HTTP and vice versa.
        PndAudioExoPlayer1.Initialize("PndAudioExoPlayer1", "MyAppUserAgent/1.0", True, True)
      • IsInitialized As Boolean
      • IsPlaying As Boolean
        Returns whether the player is playing.
      • Play
        Resumes playback.
      • ProgressiveMediaSource (Url As String)
        For: MPEG (MP3), AAC, OGG streams
        Url - Radio station stream Url
      • Release
        Releases the player.
        This method must be called when the player is no longer required.
        The player must not be used after calling this method.
      • Stop
        Stops playback.
      • UpdateMediaSession (KeyTitle As String, KeyArtist As String, KeyAlbumArt As android.graphics.Bitmap)
        This items will display on remote (bluetooth) device like: smart watch, car display, etc.
        KeyTitle - Title name or song name
        KeyArtist - Artist name or for example radio station name
        KeyAlbumArt- Image of album or for example radio station logo.
    • Properties:
      • Volume As Float
        Get or Sets the audio volume.
        Range(from = 0.0, to = 1.0)

What's new:

v1.21
  • Resolved conflict with RuntimePermissions library.
  • Example app updated, removed AppCompat library.
v1.20
  • Previous, Next and all other media buttons will rise new event MediaSessionOnButton (KeyCode As Int)
  • Example app updated
v1.10
  • UpdateMediaSession - Title, song name and album art for remote devices like: smart watch, car display, etc.
  • 3 new events for remote devices (Bluetooth): play, pause, stop, this also should work from headphone devices. You can use it FROM remote device to control player on your phone, volume also work from remote device.

Download library from: https://www.dropbox.com/s/8954fff9pewc0rt/PndAudioExoPlayerLibrary.zip?dl=0
 

Attachments

  • PndAudioExoPlayerExample.zip
    91.8 KB · Views: 51
Last edited:
I have good news for The author of this library. I can 100 % confirm, that even BLuetooth headset HBFS900 is fully compatible with The library and with The sample app. I can use next / Previous Hbs900 Bluetooth headset to switch between The stations. Also Play / Pause button work smoothly. I have also done my best to solve issue, that if user turn off The device screen, playback could stop on some aggressive Android setups, which have been focused on energy saving.
I have used starter.bas to add The simple code. Sure, it may have disadvantage. That background service will be started immediately after app execution even before user will play AN station. Some advanced developers here would know how to fix it. Some programmers simply solve such situations by enforcing ignore battery optimisations permission and to call special intent to recall battery optimisation enforcing dialog. But The service based approach is more ellegant so I rather want to use service based approach. I Am sending servicestarter.bas as attachment.
 

Attachments

  • Starter.bas
    1,007 bytes · Views: 19
Dear Pendrush,
I have A big plea to you.
Is it possible to extend your library, so audio meta data could be obtained even if live Internet radio stream is not being actually plaied by Exoplayer? Or unfortunately, Exoplayer from Google company have been coded by The way, which prevent from doing so?
My aim of this plea is to allow Internet radio player programmer to allow programmer to play The specified song?
GUI could contain Editable field with The hint enter song name. And if user fill out The song name, app could monitor meta data of The specified live stream. If The specified song will be plaied, app could start play.
I Am aware, that I want too advanced and complex features.
I also know, that there are professional companies, who are using their own servers for such thinks to monitor many live Internet streams for user given song. So I AM aware, that I could not monitor too much streams at The same time for meta data on my own device.
I would want to only monitor one live stream.
But if it is not possible also because os Internet technology used in live streams, I will understand that.

Your library has brought much joy to Me as a visually impaired advanced user with no sight at all.
I can make several little Internet radio players for myselves thanks to your code and library.
I do not have to cope with object position values, I can even used my wired or bluetooth headsets to switch between live streams. I Am really very very happy thanks to your programmers work dear Pendrush. And I Am really appreciating your time and your development Java and Android API deep knowledge very much. I Am meaning it from The bottom of my heart.

So thank you very very much for your very useful and stable library.
 
Dear elite developers,
Who of us could try to add some miracle code to The ServicePlayer service so it will allow even users or Android 12 to be able to listen live stream with The screen turned off.
Why I Am asking for such miracle code?
Because Even ignore battery optimisations are not The guarantee, that you will be able to listen live streams with screen turned off on Android 9 and newer.
So who of us could modify sample code from The creator of this forum thread, so radio player will not be turned off even on Android 12 with The screen turned off and if AC adapter is not connected to The device.


It is really very painful Android world today.
Why Google apps such as Youtube Music premium or Youtube Premium are able to overcome power management?
Does Google use some their undocumented secret APi functions for such purposes?

If yes, may be, that The founder of B4A could try to negotiate with engineers from Google so their secret APIS could be The part of some B4A library such as Phone library.
 
Top