用循环c#同时播放两个声音

时间:2016-02-13 19:54:06

标签: c# windows-media-player

我有一个背景音乐和一个播放1~2秒音效的按钮。 我想要的是使它即使播放按钮声后仍然可以继续播放背景音乐。

目前,它会永久停止播放背景音乐以播放声音按钮。

这是我现在的解决方案

private void LoginGUI_Load(object sender, EventArgs e)
{
    try
    {
        string filename = @"..\..\Soundtracks\The Boy Who Shattered Time.wav";
        Player = new System.Media.SoundPlayer(filename);
        Player.PlayLooping();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Unable to play soundtrack.");
    }
}

这是按钮

public virtual void getButtonSound()
{
    try
    {
        string filename = @"..\..\Soundtracks\demacia.wav";
        Player = new System.Media.SoundPlayer(filename);
        Player.PlaySync();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Unable to play soundtrack.");
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用线程

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"..\..\Soundtracks\The Boy Who Shattered Time.wav"));
        c.PlayLooping();
    }).Start();


new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"..\..\Soundtracks\demacia.wav"));
        c.Play();
    }).Start();

答案 1 :(得分:0)

我猜PlaySync()是你班级的一个字段,所以你用你的按钮声音覆盖包含循环wav文件的第一个实例。使用Play()的两个实例 - 每个声音一个 - 并且不需要线程。

此外,您应该将Play()更改为PlayLooping(),因为PlaySync()__int64 calcImageHash(IplImage* src, bool show_results) { if(!src){ return 0; } IplImage *res=0, *gray=0, *bin =0; res = cvCreateImage( cvSize(8, 8), src->depth, src->nChannels); gray = cvCreateImage( cvSize(8, 8), IPL_DEPTH_8U, 1); bin = cvCreateImage( cvSize(8, 8), IPL_DEPTH_8U, 1); cvResize(src, res); cvCvtColor(res, gray, CV_BGR2GRAY); CvScalar average = cvAvg(gray); printf("[i] average: %.2f \n", average.val[0]); cvThreshold(gray, bin, average.val[0], 255, CV_THRESH_BINARY); __int64 hash = 0; int i=0; for( int y=0; y<bin->height; y++ ) { uchar* ptr = (uchar*) (bin->imageData + y * bin->widthStep); for( int x=0; x<bin->width; x++ ) { if(ptr[x]){ hash |= (__int64)1<<i; } i++; } } printf("[i] hash: %I64X \n", hash); cvReleaseImage(&res); cvReleaseImage(&gray); cvReleaseImage(&bin); return hash; } 将在内部使用线程,而__int64 calcImageHash(IplImage* src, bool show_results) { if(!src) { return 0; } IplImage *res=0, *gray=0; res = cvCreateImage( cvSize(9,8), src->depth, src->nChannels); gray = cvCreateImage( cvSize(9,8), IPL_DEPTH_8U, 1); cvResize(src, res); cvCvtColor(res, gray, COLOR_BGR2GRAY); __int64 hash = 0; int i=0; cout<<gray->height; for( int y=0; y<gray->height; y++ ) { uchar* ptr = (uchar*) (gray->imageData + y* gray->widthStep); for( int x=0; x<gray->width-1; x++ ) { if( ptr[x+1] > ptr[x] ){ hash |= (__int64)1<<i; } i++; } } printf("[i] hash: %I64X \n", hash, "\n"); std::cout<<endl; cvReleaseImage(&res); cvReleaseImage(&gray); return hash; } 将阻止当前线程。