同时播放两个声音 c# [英] Playing two sounds simultaneously c#

查看:14
本文介绍了同时播放两个声音 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WP7 应用程序,它需要在循环背景音乐上播放(按下按钮时)各种音效.背景音乐通过按下按钮 1 启动并循环播放.当我按下 button3(触发音效)时,第一次按下时音效会很好地覆盖在背景音乐上.但是,当我再次按下 button3 时,背景音乐停止了.我无法弄清楚为什么会发生这种情况!?我在下面粘贴了代码的相关部分.将不胜感激任何帮助.

I am creating a WP7 application that requires various sound effects to be played (on button press) over looped background music. The background music is initiated by pressing Button 1 and loops fine. When I press button3 (triggers a sound effect), the sound effect overlays on the background music fine on first press. However, when I press button3 again, the background music stops. I cannot figure out why this might be happening!? I have pasted the relevant portions of code below. Would appreciate any help.

public partial class MainPage : PhoneApplicationPage
{
    SoundEffect soundEffect;
    Stream soundfile;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }



    static protected void LoopClip(SoundEffect soundEffect)
    {
        {
        SoundEffectInstance instance = soundEffect.CreateInstance();
        instance.IsLooped = true;
        FrameworkDispatcher.Update();
        instance.Play();
        }
    }

    public void PlaySound(string soundFile)
    {
        using (var stream = TitleContainer.OpenStream(soundFile))
        {

            var effect = SoundEffect.FromStream(stream);
            effect.Play();
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        soundfile = TitleContainer.OpenStream("BackgroundMusic.wav");
        soundEffect = SoundEffect.FromStream(soundfile);  
        LoopClip(soundEffect);
    }


    private void button3_Click(object sender, RoutedEventArgs e)
    {
        PlaySound("sound3.wav");
    }

}

}

推荐答案

如果您总是使用实例,这应该可以工作,因此将代码更改为此,它应该可以解决问题:

This should work if you are always working with Instances so change your code to this and it should clear up the problem:

public partial class MainPage : PhoneApplicationPage
{   

    SoundEffectInstance loopedSound = null;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    static protected void LoopClip(SoundEffect soundEffect)
    {
        loopedSound = soundEffect.CreateInstance();
        loopedSound.IsLooped = true;
        loopedSound.Play();
    }

    public void PlaySound(string soundFile)
    {
        SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(soundFile, UriKind.Relative)).Stream);
        SoundEffectInstance instance = sound.CreateInstance();
        instance.Play();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(@"BackgroundMusic.wav", UriKind.Relative)).Stream); 
        LoopClip(sound);
    }


    private void button3_Click(object sender, RoutedEventArgs e)
    {
        PlaySound("sound3.wav");
    }

}

以上示例假设您的声音文件设置为 Build Action = Content 并且位于顶级目录中.

The above example assumes your sound files are set with Build Action = Content and are in the top level directory.

这篇关于同时播放两个声音 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆