Results 1 to 3 of 3

Thread: Classic VB - How can I play MP3/WAV songs?

Hybrid View

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Classic VB - How can I play MP3/WAV songs?

    The simplest method to play MP3 or WAV songs through a VB project is to use the Windows Media Player Control. In fact, you can use this control to play all file formats supported by the Windows Media Player, the default player in your Windows OS. But let's now focus on MP3 and WAV !

    (See below for alternative methods using DirectX, or sndPlaySound, or MMControl)


    You can access this control through Project->Components (or press CTRL+T to open the Components Dialog box). Under the Controls tab, tick the box next to "Windows Media Player".

    Place one instance of this control on your form of the project (I renamed it to mp3 in the attached project). The 4 basic controls you need to play a song is PLAY it, STOP it, PAUSE it, and of course to start with you must OPEN it in the control.

    The Methods used by this control for the above-mentioned operations are:
    1 -> .Play to PLAY a song;
    2 -> .Stop to STOP a song;
    3 -> .Pause to PAUSE a song; and
    4 -> .FileName to OPEN a song in the Media Player control.
    Various other useful Methods supported by this control are as follows:
    (a) .Cancel -> Cancels the Open method before the file completes opening.

    (b) .FastForward -> Scans rapidly forward through the current clip.

    (c) .FastReverse -> Scans rapidly backward through the current clip.

    (d) .GetCodecDescription -> Retrieves the descriptive name of the given codec.

    (e) .GetCurrentEntry -> Retrieves the current clip being played.

    (f) .GetMediaInfoString -> Retrieves show or clip information like that of ID3 tags.

    (g) .Next -> Jumps to the next clip in a playlist.

    (h) .Previous -> Jumps to the previous clip in a playlist.
    Please note that this is not the full list, these are just the ones which are most useful in this situation.


    There are several Properties available to you, which you can use to determine (or set) features of the player or sound file. These include:
    (a) .AllowScan -> Read/Write Sets or retrieves a value specifying whether scanning is enabled for files that support scanning (fast-forwarding and rewinding).

    (b) .AutoRewind -> Read/Write Sets or retrieves a value specifying whether the Windows Media Player control automatically returns to the clip's starting point after the clip finishes playing or has otherwise stopped.

    (c) .AutoStart -> Read/Write Sets or retrieves a value specifying whether to start playing the clip automatically.

    (d) .Balance -> Read/Write Sets or retrieves a value indicating the stereo balance.

    (e) .Bandwidth -> Read-only Retrieves the bandwidth of the current clip in bits per second.

    (f) .CanSeek -> Read-only Retrieves a value specifying whether the current file has the ability to seek to a specific time.

    (g) .CreationDate -> Read-only Retrieves a value specifying the date and time when the clip was created.

    (h) .CurrentPosition -> Read/Write Sets or retrieves a value representing the clip's current position, in seconds.

    (i) .CurrentState -> Read-only Specifies the playback file's current state: stopped, paused, or running.

    (j) .Duration -> Read-only Retrieves a value indicating the clip's playing time in seconds.

    (k) .Enabled -> Read/Write Sets or retrieves a value specifying whether the control is enabled.

    (l) .FileName -> Read/Write Sets or retrieves a value specifying the name of the clip to play.

    (m) .HasError -> Read-only Retrieves a value specifying whether the control currently has an error.

    (n) .Mute -> Read/Write Sets or retrieves a value indicating the current mute state of the Windows Media Player control.

    (o) .PlayCount -> Read/Write Sets or retrieves a value indicating the number of times a clip plays.

    (p) .PlayState -> Read-only Retrieves a value indicating the state of the Windows Media Player operation.

    (q) .Volume -> Read/Write Sets or retrieves a value specifying the volume, in hundredths of decibels.
    If you still have any queries, please use the following link for more documentation on the Windows Media Player Control: MSDN



    The project attached is an example of how you can use the control. Hope it helps you !!!!
    NB: to select the file to play, this example uses the Common Dialog control.
    Attached Files Attached Files
    Last edited by si_the_geek; Apr 8th, 2007 at 02:41 PM.

  2. #2
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    ..and another way

    Two other ways that you might find helpful are the sndPlaySound API and the MMControl that can be added in VB.

    sndPlaySound

    This function plays the sound specified by 'lpszPlaySound' parameter. This function is limited to .wav files. An example of use is shown below.

    VB Code:
    1. Option Explicit
    2.  
    3. ' Plays the sound specified by
    4. ' lpszPlaySound. This function is
    5. ' limited to .wav files.
    6. Private Declare Function sndPlaySound _
    7.     Lib "winmm.dll" _
    8.     Alias "sndPlaySoundA" ( _
    9.         ByVal lpszSoundName As String, _
    10.         ByVal uFlags As Long) _
    11.         As Long
    12.        
    13. Private Const SND_NOWAIT    As Long = &H2000                    ' Don't wait if the driver is busy
    14. Private Const SND_SYNC      As Long = &H0                       ' Play synchronously (default)
    15. Private Const SND_FLAGS     As Long = SND_SYNC Or SND_NOWAIT    ' Combination of two constants above
    16.  
    17. Private Sub Command1_Click()
    18.     ' Call sndPlaySound to play our file
    19.     Call sndPlaySound(App.Path & "\Chimes.wav", SND_FLAGS)
    20. End Sub

    MMControl

    This is a control that can be added through Visual Basic 6.0 by going to Project > Components and selecting 'Microsoft Multimedia Control 6.0'. This control can play a mixture of sound types and is of more use than sndPlaySound if you are playing music through VB. An example on using this is below:

    VB Code:
    1. Private Sub Form_Load()
    2.     With MMControl1
    3.         .FileName = App.Path & "\Sound.wma"     ' Set the file to be played
    4.         .Command = "Open"                       ' Open the file
    5.         .Command = "Play"                       ' Play the file
    6.     End With
    7. End Sub

    Hope this is of some help
    Last edited by si_the_geek; Apr 8th, 2007 at 02:36 PM.

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