是否当它被嵌入在XAML code一个MediaElement的只能玩? [英] Does a MediaElement only play when it is embedded in XAML code?

查看:141
本文介绍了是否当它被嵌入在XAML code一个MediaElement的只能玩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有任何的视觉效果在所有的声音播放器类,我试图使用的MediaElement 来发挥我的声音。在所有测试项目中,其中的MediaElement 嵌入在XAML code,它工作正常。然而,在我的code-唯一版本,是不玩任何东西,即使该文件已加载完美(我可以在调试器中看到的)。我做以下内容:

I have a sound player class that doesn't have any visuals at all, and I am trying to use a MediaElement to play my sounds. In all the test projects, in which the MediaElement is embedded in the XAML code, it works fine. However, in my code-only version, is doesn't play anything at all, even though the file has been loaded perfectly (I could see in the Debugger). I am doing the following:

public class MySoundPlayer
{
    private MediaElement player = new MediaElement();

    public MySoundPlayer()
    {
        player.LoadedBehavior = MediaState.Manual;
        player.UnloadedBehavior = MediaState.Stop;
        player.Volume = 1.0;
        player.MediaEnded  += player_MediaEnded;
        player.MediaOpened += playerr_MediaOpened;
        player.MediaFailed += player_MediaFailed;
    }

    private void player_MediaEnded(object sender, EventArgs e)
    {
        player.Stop();
        Debug.WriteLine("Stopped");
     }

    private void player_MediaOpened(object sender, EventArgs e)
    {
        Debug.WriteLine("Opened");
    }

    private void player_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        Debug.WriteLine("Failed");
    }

    public void PlayFile(string fileName, bool loop)
    {
        player.Source = new Uri(fileName, UriKind.RelativeOrAbsolute);
        player.Play();
        player.Volume = 1.0;
    }
}

我双重检查,如果文件存在,这确实(它甚至装入正确),并且我的声音被接通。 :-)此外,当我修改的MediaElement 声音播放,它完美的罚款。我能找到的唯一区别是,我没有它嵌入在XAML code。这是一个要求?

I double-checked if the file exist, which it does (and it is even loaded correctly), and that my sound is turned on. :-) Also, when I change the MediaElement by SoundPlayer, it works perfectly fine. The only difference I can find is that I do not have it embedded in the XAML code. Is this a requirement?

推荐答案

在为了工​​作的MediaElement必须是你的应用程序的逻辑树的一部分,因此必须在应用程序被添加到某个容器(网格,StackPanel中)。

In order to work the MediaElement must be part of the logical tree of your application and therefore must be added to some container (Grid, StackPanel) in your application.

您可以通过XAML添加的MediaElement(如你以前做过的话),或者你可以通过

You can add the MediaElement via XAML (as you have done it before) or you can add it during runtime via

LayoutRoot.Children.Add(player);

而不是使用MediaElement的,你应该使用 MediaPlayer的类。这将工作(至少对我来说),而不将其连接到XAML。

Instead of using the MediaElement you should use the MediaPlayer class. This will work (at least for me) without attaching it to XAML.

MediaPlayer player = new MediaPlayer();
player.Open(new Uri(fileName, UriKind.RelativeOrAbsolute));
player.Play();

这篇关于是否当它被嵌入在XAML code一个MediaElement的只能玩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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