Android Question MediaPlayerStream

Discussion in 'Android Questions' started by walterf25, Jul 25, 2015.

  1. walterf25

    walterf25 Well-Known Member 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,
    however when i use the MediaPlayerStream.getDuration method i get the following:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

  3. walterf25

    walterf25 Well-Known Member Licensed User

  4. RandomCoder

    RandomCoder Well-Known Member Licensed User

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

    Code:
    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, 10) & ":" & NumberFormat(seconds, 20)
    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.
     
  5. walterf25

    walterf25 Well-Known Member Licensed User

    Hey RandomCoder, thanks a lot, i will give it a try later on today.

    Cheers,
    Walter
     
    RandomCoder likes this.
Loading...