如何使用组合框更改Windows窗体应用程序的声音文件?

时间:2011-11-25 11:43:23

标签: winforms

我正在开发Windows窗体应用程序。目前我正在为我的Windows窗体应用程序的设置方面工作。在设置表单上,我可以切换应用程序的提示音。默认声音代码如下

public String defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\applause-2.wav"; 

至于设置,我已经包含2个默认音调供用户通过组合框进行选择。组合框的代码如下,

private void comboBoxSound_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBoxSound.SelectedIndex == 0)
        {
            ReportStatus("Alert tone changed to 'Beep(1)'!");
            backgroundFormObject.getSetting().defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-1.wav";
        }
        else
        {

            ReportStatus("Alert tone changed to 'Beep(2)'!");
            backgroundFormObject.getSetting().defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-2.wav";
        }

        string appPath = Path.GetDirectoryName(Application.ExecutablePath);

        Stream stream = File.Open(appPath + "\\setting.sd", FileMode.Create);
        BinaryFormatter bFormatter = new BinaryFormatter();


        bFormatter.Serialize(stream, backgroundFormObject.getSetting());
        stream.Close();
    }

为什么每当我选择另一种音调,并播放声音时,效果仍然与掌声的原始提示音相同。在播放之前,我是否必须等待文件加载完成?

1 个答案:

答案 0 :(得分:0)

设法使用以下代码自行解决

private void comboBoxSound_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBoxSound.SelectedIndex == 0)
        {
            ReportStatus("Alert tone changed to 'Beep(1)'!");
            settingObject.defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-1.wav";
        }
        else
        {

            ReportStatus("Alert tone changed to 'Beep(2)'!");
            settingObject.defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-2.wav";
        }

        string appPath = Path.GetDirectoryName(Application.ExecutablePath);

        Stream stream = File.Open(appPath + "\\setting.sd", FileMode.Create);
        BinaryFormatter bFormatter = new BinaryFormatter();


        bFormatter.Serialize(stream, settingObject);
        stream.Close();
    }