Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1378
  • Last Modified:

c# Stop Audio sound

Hi,

In my background task I play sound continuosly:

      private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {

            myaudio = new Audio();
            while (true)
            {
                myaudio.Play(@"C:\windows\media\ding.wav");
            }
        }

When I click cancel button, in the completed task:

       private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            myaudio.Stop();          
        }

But the sound is not stopped. do you know the reason?

Thanks,
JT
0
jtran007
Asked:
jtran007
  • 8
  • 5
1 Solution
 
jtran007Author Commented:
Hi,

Also I copy this wave sound to my project and under resources, how do I refer it ?

Thanks,
JT
0
 
Asim NazirCommented:
Hi,

Here is working code for your reference:

 
private void btnAudio_Click(object sender, EventArgs e)
        {
            SoundPlayer wavPlayer = new SoundPlayer();
            wavPlayer.SoundLocation = @"C:\WINDOWS\system32\LoopyMusic.wav";
            wavPlayer.Play();

            wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted);
            wavPlayer.LoadAsync();

        }

        private void wavPlayer_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            ((System.Media.SoundPlayer)sender).Stop();
        }

Open in new window


I hope this helps. Let me know if you need more help.
0
 
jtran007Author Commented:
Hi,

I copied this program to my notebook. It runs but does not produce sound whereas on my desktop
it produces souns. Could you give me a hand?

Thanks,
JT
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Asim NazirCommented:
Hi,

Make sure sound file exists on this path i.e. "C:\WINDOWS\system32\LoopyMusic.wav"

Asim
0
 
jtran007Author Commented:
Hi,

I checked c:\windows\media folder in my notebook, I found. ding.wav file. And go this folder (c:\windows\media),
and select ding.wav, and click play. It plays fine.

Do you know why it does not produce sound on the notebook?

Thanks,
JT
0
 
Asim NazirCommented:
Hi,

If you play ding.wav, it's simply a beep. Try referencing some other file.
Also, this way of playing sound is standard and is not specific to any machine. May be you can check some machine configurations...

Thanks.
Asim
0
 
jtran007Author Commented:
Hi asim,

Thansk.Your solution does not play sound continuously. Since during uploading file, I'd like
sound playing on until the end of file upload.
Do I miss something here?

JT
0
 
Asim NazirCommented:
Hi,

To play sound continuously, you need to use PlayLooping() method instead of Play();
So code will become:

private void btnAudio_Click(object sender, EventArgs e)
        {
            SoundPlayer wavPlayer = new SoundPlayer();
            wavPlayer.SoundLocation = @"C:\WINDOWS\system32\LoopyMusic.wav";
            //wavPlayer.Play();
            wavPlayer.PlayLooping();

            wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted);
            wavPlayer.LoadAsync();

        }

        private void wavPlayer_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            ((System.Media.SoundPlayer)sender).Stop();
        }

Open in new window


Asim
0
 
jtran007Author Commented:
hI ASIM..,

Thanks now it plays continuously. But as I said, it is not working on my notebook.
Do you have any idea?

Thanks,
JT
0
 
jtran007Author Commented:
Hi asim,

Yes. I can play any media sound on my note book: mp3, wav,...
But my program is not producing sound on this notebook.

Regards,
JT
0
 
jtran007Author Commented:
Hi asim,

if i have a different wave file, and load it at run time, suppose I save it into my resources folder,
what api call to load this file from my project (subfolder resources)

Thanks,
JT
0
 
Asim NazirCommented:
It's working on my machine and also on your desktop machine. So, this has to be configuration issue - as I wrote previously!
0
 
jtran007Author Commented:
Thaks,
JT
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now