简体   繁体   English

Windows Phone MediaElement

[英]Windows Phone MediaElement

I am creating an app using c# and xaml where i have 20 pages each page has some character images and when i tap on that image some dialogues should pop up.so i have taken a MediaElement and made it global like this: 我正在使用c#和xaml创建一个应用程序,其中我有20页,每页都有一些字符图像,当我点击该图像时,应该会弹出一些对话框。所以我采用了MediaElement并将其设置为全局,如下所示:

App.xaml: App.xaml:

<MediaElement x:Key="StorySound" 
                  Volume="1"
                  AutoPlay="True"/>

App.xaml.cs: App.xaml.cs:

public static MediaElement StorySound
    {
        get { return Current.Resources["StorySound"] as MediaElement; }
    }

And on every page on tap event i have written this code: 在点击事件的每一页上,我都编写了以下代码:

App.StorySound.Source = new Uri("/Sounds/Dialogues/" + textblock.Text + ".mp3", UriKind.Relative);
App.StorySound.MediaOpened+=StorySound_MediaOpened;

void StorySound_MediaOpened(object sender, RoutedEventArgs e)
    {         
            App.StorySound.Play();            
    } 

This plays the sound on not more than 2 pages when i navigate to the 3rd page using the next button which i have created no sound plays but when i close the app and directly open the 3rd page the sound plays.I have even tested this by using MessageBox to show the current state of the mediaelement i have found that on the first two pages the current state is "Opening" and the sound plays but on the third page the current state is "Closed" so the sound does not Play.is there any memory issue when playing sounds using MediaElement.I cannot use SoundEffect Because all my sound files are .mp3 and if i use soundeffect i will have to convert these sounds in .wav which will increase the size of my app because i have more than 50 sound files. 当我使用未创建声音播放的下一个按钮导航到第三页时,此声音会在不超过2页的页面上播放,但是当我关闭应用程序并直接打开第三页时,声音会播放。我什至对此进行了测试使用MessageBox显示中介元素的当前状态,我发现在前两页上当前状态为“正在打开”,并且播放声音,但是在第三页上,当前状态为“关闭”,因此声音未播放。使用MediaElement播放声音时没有任何内存问题。我无法使用SoundEffect,因为我所有的声音文件都是.mp3,如果我使用soundeffect,我将不得不将这些声音转换为.wav,这将增加我的应用程序的大小,因为我的声音超过了50个声音文件。

If I understand you correctly you are hooking the MediaOpened -event on the global StorySound -object on each page but you never seem to unhook it. 如果我理解你正确地你是挂钩MediaOpened对全球-event StorySound -object每一页上,但你似乎从来没有解开它。 I would suggest you to either: 我建议您:

  • use a local MediaElement on each page that you start (and perhaps stop), or 在您开始(甚至停止)的每个页面上使用本地MediaElement ,或者
  • make sure you unhook all events on the StorySound when you navigate (so that each page is the only "user" of this global resource). 确保在导航时取消对StorySound上的所有事件的StorySound (以便每个页面都是此全局资源的唯一“用户”)。

I am sure it will be fine to have may mp3's in your app; 我敢肯定,在您的应用中可以包含mp3。 you should not need to convert them to wav. 您无需将它们转换为wav。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM