Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# Stop Audio sound

Posted on 2011-02-20
13
Medium Priority
?
1,310 Views
Last Modified: 2013-12-17
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
Comment
Question by:jtran007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 

Author Comment

by:jtran007
ID: 34940430
Hi,

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

Thanks,
JT
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34940441
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
 

Author Comment

by:jtran007
ID: 34940443
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
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34940477
Hi,

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

Asim
0
 

Author Comment

by:jtran007
ID: 34940485
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
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34940505
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
 

Author Comment

by:jtran007
ID: 34940559
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
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34940614
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
 

Author Comment

by:jtran007
ID: 34940649
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
 

Author Comment

by:jtran007
ID: 34940656
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
 

Author Comment

by:jtran007
ID: 34940676
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
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 1000 total points
ID: 34940679
It's working on my machine and also on your desktop machine. So, this has to be configuration issue - as I wrote previously!
0
 

Author Closing Comment

by:jtran007
ID: 34942952
Thaks,
JT
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question