How to play background sound?

How can I play a sound in background using Command1?

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 13293 Views - Last Post: 07 March 2010 - 10:05 AM Rate Topic: -----

#1 Guest_Tomáš Čimma*


Reputation:

How to play background sound?

Posted 04 March 2010 - 07:37 AM

Hello. I have a Command1 and when I click it, I want a sound to be played.

I tried this:
Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", _
        AudioPlayMode.Background)
End Sub

...but it doesn't work, 'cause it's for VB 2005.

Then I tried this one:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Private Sub Command1_Click()
Dim returnval As Long
Dim soundfile As String

soundfile = "C:\Documents and Settings\Tomáš Čimma\Plocha\Hudba a Zvuky\Test sound.mp3"
returnval = PlaySound("SystemStart", 0, &H0)
End Sub

...but that didn't work too, 'cause it didn't play the correct sound and when it was playing the other one, the whole Form was stuck and I went active when the sound was played.

PLEASE, HELP ME! How can I play a sound on background with path...

"C:\Documents and Settings\Tomáš Čimma\Plocha\Hudba a Zvuky\Test sound.mp3"

...and the project won't stuck while the sound is playing. PLEASE HELP!!!

Thank you and another 1,000 thanks to you.


Is This A Good Question/Topic? 0

Replies To: How to play background sound?

#2 Guest_evry1falls*


Reputation:

Re: How to play background sound?

Posted 04 March 2010 - 07:47 AM

You Should Transfer Your Code To The VB .Net Section ... VB 6.0 Help only Is Here
Was This Post Helpful? 0

#3 Guest_Guest*


Reputation:

Re: How to play background sound?

Posted 04 March 2010 - 07:54 AM

And how can I transfer it?
Was This Post Helpful? 0

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to play background sound?

Posted 04 March 2010 - 08:27 AM

VB 2008 is VB.NET, not VB6 :)

Moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to play background sound?

Posted 04 March 2010 - 08:28 AM

http://lmgtfy.com/?q...sound+in+vb.net
and wow that was fast :o

This post has been edited by NoBrain: 04 March 2010 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 tom1597530  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-October 09

Re: How to play background sound?

Posted 04 March 2010 - 08:57 AM

BUT IM TALKING ABOUT VB6!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to play background sound?

Posted 04 March 2010 - 09:06 AM

ok lets try this then:
http://lmgtfy.com/?q...y+sounds+in+vb6
pay attention on this link:
http://www.vbforums....ad.php?t=375314

This post has been edited by NoBrain: 04 March 2010 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How to play background sound?

Posted 04 March 2010 - 09:09 AM

View Posttom1597530, on 04 March 2010 - 07:57 AM, said:

BUT IM TALKING ABOUT VB6!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Hi,

This question should be in the VB6 section.
However I tryed to write VB code.
Here's what I think you can do:

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1         '  play asynchronously

Private Sub Form_Load()
  Call sndPlaySound("your file path and name of your wave should be in here", SND_ASYNC)
End Sub



I hope it works :walkman:

This post has been edited by Luc001: 04 March 2010 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

#9 tom1597530  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-October 09

Re: How to play background sound?

Posted 04 March 2010 - 09:35 AM

Doesn't works
Was This Post Helpful? 0
  • +
  • -

#10 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How to play background sound?

Posted 04 March 2010 - 09:40 AM

Hi,

I can't test it, but do you have some errors?
Can you post us the whole codes that we can try to help you.
Was This Post Helpful? 0
  • +
  • -

#11 tom1597530  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-October 09

Re: How to play background sound?

Posted 04 March 2010 - 09:50 AM

There are no errors at all, but when I test the project, it only plays the "classic msgbox" sound and nothing else. I have no idea, how to do that
Was This Post Helpful? 0
  • +
  • -

#12 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How to play background sound?

Posted 04 March 2010 - 10:00 AM

Hi,

Try here to find a solution for your problem.
Was This Post Helpful? -1
  • +
  • -

#13 tom1597530  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-October 09

Re: How to play background sound?

Posted 04 March 2010 - 01:36 PM

No idea.

I have no idea how to work with this. It's something new for me. Could you please help me more.
Was This Post Helpful? 0
  • +
  • -

#14 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to play background sound?

Posted 04 March 2010 - 04:29 PM

hello :)

well i try this
'These Constants and API call was copied from the API-Guide program.

   

Private Const SND_APPLICATION = &H80 ' look for application specific association

   

Private Const SND_ALIAS = &H10000 ' name is a WIN.INI [sounds] entry

   

Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI [sounds] entry identifier

   

Private Const SND_ASYNC = &H1 ' play asynchronously

   

Private Const SND_FILENAME = &H20000 ' name is a file name

   

Private Const SND_LOOP = &H8 ' loop the sound until next PlaySound

   

Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file

  

Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found

   

Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound

Private Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

   

Private Const SND_PURGE = &H40 ' purge non-static events for task

    

Private Const SND_RESOURCE = &H40004 ' name is a resource name or atom

   

Private Const SND_SYNC = &H0 ' play synchronously (default)

    

    

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    



    
Private Sub Command1_Click()


'

'This will simply play a wave sound from a file location and aSync so it uses a thread other than  your applications thread and will not keep your application from responding till its done, like'playing Sync would do.

'

'The flag: SND_FILENAME I believe is the default for the PlaySound function. You can execute 'that flag if you want to make sure it plays the sound file fine.

'
Dim lngResult As Long
    

lngResult = PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC)
Sleep 2000
    

'

'IF you want to use more than 1x flag with the api call, then just call it like below....

   

'

'This will play a wave file async and loop it over and over till you call the PlaySound Function again.

lngResult = PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_LOOP)
Sleep 5000
    '


    

'

'This will call the Playsound function to execute and play nothing which will interupt and stop the previous wave file from playing.

lngResult = PlaySound(vbNullString, 0, 0)
End Sub



from the link Luc001 send you i just add sleep so you can hear it with runtime. if you debug it without the sleep you will hear it.
good luck :)
Was This Post Helpful? -1
  • +
  • -

#15 tom1597530  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-October 09

Re: How to play background sound?

Posted 05 March 2010 - 10:05 AM

Ok thanks, it works now and I have only one question left: Ehm... what code can I use, so that when it is playing I can continue working in the Form1 for example, that it won't stuck, while the sound is beeing played???
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2