如何播放声音时,应用程序开始的Windows Phone [英] How do I play sound when app begins windows phone

查看:106
本文介绍了如何播放声音时,应用程序开始的Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows手机应用程序,我想,当它启动时打短.wav音频,类似于windows。起初,我试图用我的时候一个控件加载事件处理程序(这个工作的时间,这是非常有趣的约60%,也许有人可以清除起来为好,我想它与为了做到事情发生加载。这就是为什么它的不同,每次运行它​​)。我使用视觉工作室2012极致,这是我的XAML code为.wav文件:

On my windows phone app, I want to play a short .wav audio, similar to windows when it boots up. At first I tried using an event handler when one of my controls was loaded (this worked about 60% of the time, which is very interesting, maybe someone can clear that up as well, I'm thinking it has to do with the order things happen to load. That's why its different each time running it). I'm using visual studios 2012 ultimate, this is my xaml code for the .wav file:

<MediaElement x:Name="MySound"
                          Source="/quantum_drive.wav"
                          Volume="1"
                          AutoPlay="false"
                          />

我也只是尝试调用此方法时,主要页面加载:

I've also just tried to call this method when the main page loads:

private void MainPage1_Loaded(object sender, RoutedEventArgs e)
    {
        MySound.Play();
    }

有关某种原因,这仅适用时间(貌似随机)约60%,这是为什么?这似乎是一个常见的​​事情要在手机应用程序做的,但我找不到计算器或谷歌搜索这方面的消息。

For some reason this only works about 60% of the time(seemingly randomly), why is this?! This seems like a common thing to want to do in a phone app, yet I cannot find any information on this on stackoverflow or google searches.

推荐答案

不要使用媒体元素做你正在尝试做的。它是有缺陷的声音的效果。这意味着更多的用户交互媒体。

Don't use the media element to do what you are trying to do. It is to buggy for sound effects. It is meant more for user interaction media.

相反,请执行下列操作(你可以在这里全文)

Instead, do the following (you can the full article here)

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Media;

using Microsoft.Xna.Framework;

static Stream stream1 = TitleContainer.OpenStream("soundeffect.wav");

static SoundEffect sfx = SoundEffect.FromStream(stream1);

static SoundEffectInstance soundEffect = sfx.CreateInstance();

现在只是从你的加载方法调用播放声音

Now just invoke play sound from your loaded method

public void playSound(){

    FrameworkDispatcher.Update();

    soundEffect.Play();

}

这篇关于如何播放声音时,应用程序开始的Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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