Results 1 to 6 of 6

Thread: Playing audio files.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    174

    Playing audio files.

    Dear all,

    Hope all in fine tune.

    I am creating a media player project. I am taking the list of audio files (.mp3) in a list box. Now I want them to be played one by one starting from the top of the list box.

    I am writing the code in a loop where I am taking a loop which runs from the 0th Index to the index=Items.Count-1

    But while playing only the last song is played. How can I rectify this problem. Please help me.

    Code:
    Private Sub PlaySong()
            Dim i As Integer
            i = 0
            Do While i < Playlist.Items.Count
                Playlist.GetSelected(i)
                PlayerControl.URL = Playlist.Items.Item(i)
                If PlayerControl.URL <> "" Then
                    'PlayerControl.Ctlcontrols.play()
                    'DurationTimer.Enabled = True
                Else
                    DurationTimer.Enabled = False
                End If
                i = i + 1
            Loop
        End Sub

  2. #2
    New Member
    Join Date
    Feb 2009
    Posts
    1

    Re: Playing audio files.

    Depending on what version of visual basic you currently own, Rumor has it, version 6.0 and lower supports only ".wav" audio files, so you must convert your .mp3 perhaps?

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Playing audio files.

    It looks to me that the loop you show does play every song in the list but it does not appear to wait for a song to finish before playing the next one.

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Playing audio files.

    Quote Originally Posted by lotlon
    Depending on what version of visual basic you currently own, Rumor has it, version 6.0 and lower supports only ".wav" audio files, so you must convert your .mp3 perhaps?
    There are several audio controls compatible with Vb6 that support practically all audio formats. I recommend either .mp3 or .wma rather than .wav files that are much larger. Either one of these two audio file formats have no loss in sound quality while saving lots of disk space and memory.
    Doctor Ed

  5. #5
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: Playing audio files.

    VB6 as well as 5 can play mp3 because windows can play mp3. Add this code to a module and you can start Jammin':
    Code:
    Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    Public Function Play_MM(song As String)
        Dim shortpath As String, pathlength As Long
        
        shortpath = Space(250)
        pathlength = GetShortPathName(song, shortpath, Len(shortpath))
    
    
        If pathlength Then
            shortpath = Left$(shortpath, pathlength)
        End If
        
        mciSendString "Close MM", 0, 0, 0
        mciSendString "Open " & shortpath & " Alias MM", 0, 0, 0
        mciSendString "Play MM", 0, 0, 0
    End Function
    Public Function Pause_MM()
        If Paused = False Then
            mciSendString "Pause MM", 0, 0, 0
            Paused = True
        Else
            mciSendString "Play MM", 0, 0, 0
            Paused = False
        End If
    End Function
    Public Function Stop_MM()
        mciSendString "Stop MM", 0, 0, 0
        mciSendString "Close MM", 0, 0, 0
    End Function
    You can check if the songs still playing by :
    Code:
    s = Space(128)
    x = mciSendString("status MM mode", s, Len(s), 0)
    
    If Left(s, 7) = "stopped" Then
        'do stopped stuff
    ElseIf Left(s, 7) = "playing" Then
        'do playing stuff
    end if
    stick that part in a timer
    Last edited by technorobbo; Feb 11th, 2009 at 08:19 PM.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  6. #6
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: Playing audio files.

    Here let me put it together for you.
    This program will read the XP My Music directory and play it:

    1 form and 1 listbox
    Code:
    Option Explicit
    
    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    'Declaration for the winmm dll
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    Private Sub Form_Load()
    Dim env As String, x As String, test As String, i As Integer
    Me.Show
    env = Environ("USERPROFILE") & "\My Documents\My Music\"
    List1.Clear
    x = Dir(env & "*.mp3")
    Do While x <> ""
    List1.AddItem x
    x = Dir()
    Loop
    
    For i = 0 To List1.ListCount - 1
        Play_MM env & List1.List(i)
        DoEvents
        test = "playing"
        While Left(test, 7) = "playing"
            test = Space(128)
            x = mciSendString("status MM mode", test, Len(test), 0)
            DoEvents
        Wend
    Next
    End Sub
    Public Function Play_MM(song As String)
        Dim shortpath As String, pathlength As Long
        
        shortpath = Space(250)
        pathlength = GetShortPathName(song, shortpath, Len(shortpath))
    
    
        If pathlength Then
            shortpath = Left$(shortpath, pathlength)
        End If
        
        mciSendString "Close MM", 0, 0, 0
        mciSendString "Open " & shortpath & " Alias MM", 0, 0, 0
        mciSendString "Play MM", 0, 0, 0
    End Function
    Private Sub Form_Unload(Cancel As Integer)
    mciSendString "Close MM", 0, 0, 0
    End
    End Sub
    Post edited to add the end command
    Last edited by technorobbo; Feb 11th, 2009 at 08:52 PM.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width