Android Question MediaPlayerStream

walterf25

Expert
Licensed User
Hello All, i have a quick question, i'm using the MediaPlayerStream library to play mp3 files from a URL, so far everything works as expected, the problem i'm having is that i'm trying to figure out how to display the track duration time in minutes and seconds (mm:ss) as well as the elapsed time in minutes and seconds (mm:ss/mm:ss).
the information i get from the URL gives me the duration time in seconds, for example a specific track gives the duration time of 209 seconds,
Response from server: {"title":"Banda el recodo-Y Llegaste tu","length":"209","link":"http:\/\/youtubeinmp3.com\/download\/get\/?i=ex3SLjhOvot0Qa2cU3A3wJ5kXa%2B8ckfINrmZXMXJSgKeOtLM5TLEYdh9QO56IGWp%2FhdTmt4j7%2FcjHvxGIzE1vA%3D%3D"}
however when i use the MediaPlayerStream.getDuration method i get the following:
mp3 duration: 320933392

I know for a fact that the value returned in the Json response is the correct value as i've played the mp3 file in my laptop's windows media player and i can see that the total duration of the song is exactly 3 minutes and 28 seconds (03:28).

What is that value returned from the MediaStreamPlayer.getDuration method.
Does anyone have any suggestions on how i might be able to achieve what i'm trying to do here?

Thanks,
Walter
 

DonManfred

Expert
Licensed User
Upvote 0

walterf25

Expert
Licensed User
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Hi @walterf25 I think that the following code sample might help...

B4X:
Private Sub statusTimer_Tick()
    ' Track info for notification text
    getPosition
End Sub

Public Sub getPosition()
    ' Use java object to enquire about the current position of playing track
    Dim jo As JavaObject = musicStream
    Dim position As Int
    position = jo.RunMethod("getPosition", Null)
    ' Get track position and duration, format as "m:ss / m:ss"
    Dim duration As String = ConvertToTimeFormat(position) & " / " & ConvertToTimeFormat(musicStream.duration)
    ' Update log and app notification with track title and runtime info
    Debug.debugLog(duration) 'DEBUG
    n.SetInfo(currentlyPlaying, duration, "")
    n.Notify(1)
End Sub

Sub ConvertToTimeFormat(ms As Int) As String
    ' Format ms into minutes and seconds
    Dim seconds, minutes As Int
    seconds = Round(ms / 1000)
    minutes = Floor(seconds / 60)
    seconds = seconds Mod 60
    ' Return in time format "m:ss"
    Return NumberFormat(minutes, 1, 0) & ":" & NumberFormat(seconds, 2, 0)
End Sub

Sample usage can be found here... UPNP Browser - Source Files Included Just have a look in the Music_Service Module. For it to list devices you will need to have either a UPNP enabled device with music stored on it or set Windows to share its files.
 
Upvote 0

walterf25

Expert
Licensed User
Hi @walterf25 I think that the following code sample might help...

B4X:
Private Sub statusTimer_Tick()
    ' Track info for notification text
    getPosition
End Sub

Public Sub getPosition()
    ' Use java object to enquire about the current position of playing track
    Dim jo As JavaObject = musicStream
    Dim position As Int
    position = jo.RunMethod("getPosition", Null)
    ' Get track position and duration, format as "m:ss / m:ss"
    Dim duration As String = ConvertToTimeFormat(position) & " / " & ConvertToTimeFormat(musicStream.duration)
    ' Update log and app notification with track title and runtime info
    Debug.debugLog(duration) 'DEBUG
    n.SetInfo(currentlyPlaying, duration, "")
    n.Notify(1)
End Sub

Sub ConvertToTimeFormat(ms As Int) As String
    ' Format ms into minutes and seconds
    Dim seconds, minutes As Int
    seconds = Round(ms / 1000)
    minutes = Floor(seconds / 60)
    seconds = seconds Mod 60
    ' Return in time format "m:ss"
    Return NumberFormat(minutes, 1, 0) & ":" & NumberFormat(seconds, 2, 0)
End Sub

Sample usage can be found here... UPNP Browser - Source Files Included Just have a look in the Music_Service Module. For it to list devices you will need to have either a UPNP enabled device with music stored on it or set Windows to share its files.
Hey RandomCoder, thanks a lot, i will give it a try later on today.

Cheers,
Walter
 
Upvote 0
Top