Player
Play music with the MediaPlayer SDK.Developed by Joe using Fast.
- Get all the music from your device or from Assets.
- Play audio from URLs, local files, and project assets.
Functions
Play
Plays an audio file from a URL or absolutePath.
Example Usages:
- Playing a local file from external storage:
Play("/storage/emulated/0/Music/song.mp3");
- Playing a file from the web:
Play("https://www.example.com/path/to/song.mp3");
PlayFromAssets
Plays an audio file from the project's assets.
-
For the Assets option, you need to create a .txt document with the same name as the song that is in assets with this modified information inside:
- Artist: Jet
Album: Jet
Duration: 00:00
- Artist: Jet
Pause
Pauses the current audio playback.
Stop
Stops the current audio playback and resets the pause position.
Resume
Resumes playback from the last pause position or from the beginning if itâs an asset file.
GetAllAssetMusicFiles
Retrieves all MP3 music files from the project's assets with details such as path, name, artist, album, and duration.
ConvertToMilliseconds
Converts a time in minutes and seconds to milliseconds.
ConvertToMinutesAndSeconds
Converts milliseconds to minutes and seconds.
SeekTo
Seeks to a specific position in the audio in milliseconds.
GetCurrentPosition
Retrieves the current playback position of the audio in milliseconds.
StartService
Starts the foreground player service.
StopService
Stops the foreground player service.
- You can turn off the screen without entering Doze mode.
A foregroundService is being used.
SetVolume
Sets the volume of the left and right channels of the media player.
Each channel can be configured separately.
Volume value:
0.0
Mute volume
1.0
Maximum volume
Default value: 0.5
SetNotificationContent
Sets the title and content for the notification that is displayed while the service is running.
-
The
SetNotificationContent
block is optional, but defaults to these values:title:
Playing
content:
Audio playback in progress
GetAlbumArt
Get the cover art for the songs on your device.
songPath
: The path of the song on the device.
imageIfThereIsntOne
: An image from assets that will appear if the song has no cover art.
ă
€ Added Helper-Block to GetMusicFiles.ă
€ V1.0.6
Jan. 05, 2025.
GetMusicFiles
Set the type of audio file you want to get.
-
audioType
: "MP3"- Input types: MP3, AAC, WAV, OGG, AMR, FLAC, MIDI, MP4, M4A, MKA.
Setting All
will get all audio types.
SetPlaylist
Set a list of songs.
songs
: List of song paths.
GetPlaylist
Get the list of songs set in SetPlaylist.
PlaybackSpeed
Set the playback speed (0.5 to 2.0 recommended).
speed
: The number must be greater than 0.0.
Repeat
Set the repeat mode.
repeatMode
: RepeatMode helper block with these options:- repeatAll
- repeatOne
- repeatOff
Loop:
Calling the function causes playback to loop for the time set in the start and end parameters.
start
: Start time in milliseconds.end
: End time in milliseconds.
The set song segment will not stop playing until you call the DisableLoop
function.
Minutes to milliseconds example:
DisableLoop
Disable loop playback, the song will continue to play normally.
GetSongsCount
Get the number of songs in the playlist in the app.
Shuffle
Set songs to shuffle mode.
Next
Go to the next song.
Shuffle mode applies to Next if enabled.
Previous
Go back to the previous song.
Shuffle mode does not apply here, the song will be returned to the index before the current one.
isPlaying
Returns true
if the media player is currently playing.
CurrentSongIndex
Get the index of the currently playing song.
Events
OnMusicFilesRetrieved
It's activated when all music files have been retrieved from the device and provides lists of:
- paths, titles, artists, albums, durations, tracks, years, genres, composers, sizes, displayNames and bitRates.
OnAssetMusicFilesRetrieved
Triggered when all music files from the project's assets have been retrieved, providing lists of paths, names, artists, albums, and durations.
OnPlay
Triggered when audio playback starts.
OnPause
Triggered when audio playback is paused.
OnStop
Triggered when audio playback is stopped.
EVENT
OnSongIndexChanged
Event is fired when a new song starts by getting its index.
OnPlaybackComplete
Event is fired when a song ends, except in RepeatOne mode.
OnSetPlaylist
Event triggered when playlist of songs is complete.
OnNext
The event is fired when using the Next function.
OnPrevious
The event is fired when using the Previous function.
Update, v1.0.8
Jan. 19, 2025.
In Android prior to 13 the notification could not be dismissed, but that changed in Android 14, now the user can dismiss the notification, Google put it that way.
So I decided to make it so that when the notification is dismissed the music and the service stops.
Note:
- I had to remove the play and pause feature when clicking on the notification, as it interfered with stopping the music and service when swiping.
Adjust the MediaPlayer playback pitch.
- 1.0 is normal pitch, recommended: 0.5 to 2.0.
Update, v1.0.9
Jan. 23, 2025.
Audio focus was updated.
- The music kept playing even when there was another player active or you played this one and the other's audio didn't stop.
- That's now fixed.
OnAudioFocusLost
Fired when audio focus is lost.
Update to floating notification:
Tested on Android 14 and 11.
-
Added Album Art display.
-
Established that if it's the last song in the playlist, when it's finished, the service stops and consequently the notification closes, since it didn't.
-
Added display of the elapsed time of the song, when pausing it will only show Now, but when calling Resume it will continue moving forward from where it was paused.
- This only appears on Android 14 and not on Android 11.
Android 11
Android 14
SetNotificationAlbumArt
Get the song's cover art to be displayed in the notification.
- songPath: Path to the song.
- imageIfTherIsntOne: If there is no cover art embedded in the song, an image is set from the assets.
Update, v1.1.0
Feb 01, 2025.
AudioSessionId
Returns the audio session ID.
A small update, needed to be able to use this Player with an Equalizer I created separately.
- When playing audio, you are given a session ID, when you stop the audio, that ID is lost.
Pausing does not lose the ID.
ă
€Extension: Version: 1.1.0ă
€ Feb 01, 2025.
joejsanz.player.joedevplayer.aix (942.1 KB)
minSdk: 26
maxSdk: 34
JDK: 11
Thanks.