please help me on my problem about visual basic 6.0
my problem is how to add music background ....
please give me some code or some idea thank you....

Dani AI

Generated

A few practical ways to get MP3 background music in VB6 (building on posts above): 's sndPlaySound is fine for short WAV effects but it won't handle MP3. 's MMC is aimed at MIDI/older formats. For MP3 the two simplest, reliable choices are (A) the Windows Media Player ActiveX control (easy, full-featured) or (B) the lightweight MCI API (no extra control). Short comment: third‑party engines (BASS, FMOD) give the best audio control if advanced features or gapless looping are required.

Windows Media Player control (recommended for most cases)

  • Add the Windows Media Player control to the toolbox (Project → Components → check Windows Media Player), place it on the form (default name WMP1).
  • Common useful settings: set WMP1.URL, WMP1.settings.autoStart = True, WMP1.settings.setMode "loop", True. Hide UI with WMP1.uiMode = "invisible" or WMP1.Visible = False. Stop and clear the URL on unload to free the file.
' Place a Windows Media Player control named WMP1 on the form
Private Sub Form_Load()
    WMP1.URL = App.Path & "\background.mp3"
    WMP1.settings.autoStart = True
    WMP1.settings.setMode "loop", True
    WMP1.uiMode = "invisible"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    WMP1.controls.stop
    WMP1.URL = ""
End Sub

MCI API (no ActiveX required)

  • Uses winmm.dll and works with system codecs; useful when avoiding extra controls.
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()
    mciSendString "open """ & App.Path & "\background.mp3"" alias bgmusic", vbNullString, 0, 0
    mciSendString "play bgmusic repeat", vbNullString, 0, 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    mciSendString "stop bgmusic", vbNullString, 0, 0
    mciSendString "close bgmusic", vbNullString, 0, 0
End Sub

Practical notes: test on the target machine (codecs/WMP presence), always stop/close on exit to release the file, and combine approaches if needed (WMP for MP3 background + sndPlaySound/PlaySound for short WAV effects).

Recommended Answers

All 4 Replies

You can use an API function to play your file....

try to use sndPlaySound function...The computer i'm using has no VB6 installed so I just search the internet...for more details: this link might be useful to you...

NOTE: files must in .WAV format....

Example: copy and paste this in a form with 1 command button named Command1

Option Explicit

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1
Private Const SND_LOOP = &H8
Private Const SND_NODEFAULT = &H2
Private Const SND_SYNC = &H0
Private Const SND_NOSTOP = &H10
Private Const SND_MEMORY = &H4

Private Sub Command1_Click()
    sndPlaySound App.Path & "\ButtonClick.wav", SND_ASYNC or SND_LOOP
End Sub

For background music, midi, rmi, and such, have a look at the MMC (Microsoft Multimedia Control) then you can still play sound effects in wav format using sndPlaySound API over the background music and yes, I do mean at the same time...

Good Luck

thank you for help me... but i want background music like mp3.....
pleasss help me again at second time and give me again codes or idea...

You can use your media player as well if you do not want to get involved in API's. Set a reference to the mediaplayer library files, select it from components and then load the mp3 files to be played. Set the mediaplayer's visibility to false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.