首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要有关Windows Phone 7的小帮助

需要有关Windows Phone 7的小帮助
EN

Stack Overflow用户
提问于 2012-05-09 23:45:54
回答 1查看 97关注 0票数 0

我正在创建一个应用程序..我正面临一个小问题。我想把这个东西..。我有一个单选按钮。现在,每当单击此单选按钮时,我都想循环播放一小段音乐。我使用的是visual studio..不能对其使用表达式。我正在使用以下代码..

代码语言:javascript
复制
public partial class Page3 : PhoneApplicationPage
    {

        private SoundEffect s1;


        public Page3()
        {
            InitializeComponent();
            GameTimer gameTimer = new GameTimer();
            gameTimer.UpdateInterval = TimeSpan.FromMilliseconds(10000);

            // Call FrameworkDispatcher.Update to update the XNA Framework internals.
            gameTimer.Update += delegate { try { FrameworkDispatcher.Update(); } catch { } };

            // Start the GameTimer running.
            gameTimer.Start();

            // Prime the pump or we'll get an exception.
            FrameworkDispatcher.Update();

            LoadSound("Views/1.wav", out s1);


        }

        private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        {
            // For error checking, assume we'll fail to load the file.
            Sound = null;

            try
            {
                // Holds informations about a file stream.
                StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

                // Create the SoundEffect from the Stream
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
            }
            catch (NullReferenceException)
            {
                // Display an error message
                MessageBox.Show("Couldn't load sound " + SoundFilePath);
            }
        }


        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {

                try
                {
                    s1.Play();
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("Can't play, s1 is null.");
                }
            }
EN

回答 1

Stack Overflow用户

发布于 2012-05-09 23:50:54

这是我用来播放声音的代码,它是有效的:

代码语言:javascript
复制
private void PlaySound()
{
    Stream stream = TitleContainer.OpenStream("Media/Sounds/SomeFile.wav");
    SoundEffect effect = SoundEffect.FromStream(stream);
    FrameworkDispatcher.Update();
    effect.Play();
}

此外,请确保您的声音文件被标记为内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10519499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档