首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MediaElement AutoPlay = "False“错误

MediaElement AutoPlay = "False“错误
EN

Stack Overflow用户
提问于 2012-01-12 04:25:32
回答 2查看 4.4K关注 0票数 3

我正在解析来自XML的多个音频文件uri,并将它们绑定到MediaElement的Source属性。我有八个按钮来触发音频文件。当AutoPlay属性设置为True时,一切工作正常。每个按钮都会触发正确的音频文件。但是,由于我不希望页面一加载就开始播放音频,因此我将AutoPlay设置为False。现在什么都不能用了。页面加载,但按钮不会触发音频文件。我该如何解决这个bug呢?

代码:

代码语言:javascript
复制
public partial class MainPage : PhoneApplicationPage
{
    string name = "C";

    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        SetPlayerSource();
        base.OnNavigatedTo(e);
    }

    private void SetPlayerSource()
    {
        XDocument audioPlayer = XDocument.Load("Audio.xml");

        var aani = (from audio in audioPlayer.Descendants("Note")
                    where audio.Attribute("id").Value == name
                    select new AudioClass
                    {
                        Audio = (string)audio.Element("url").Value

                    }).SingleOrDefault();

        player.Source = new Uri(aani.Audio, UriKind.RelativeOrAbsolute);
    }


    private void C_Key_Click(object sender, RoutedEventArgs e)
    {
        var buttonName = (sender as Button).Name;
        var underscorePos = buttonName.IndexOf('_');
        name = buttonName.Substring(0, underscorePos);
        SetPlayerSource();
        player.Play();

    }

    private void D_Key_Click(object sender, RoutedEventArgs e)
    {
        var buttonName = (sender as Button).Name;
        var underscorePos = buttonName.IndexOf('_');
        name = buttonName.Substring(0, underscorePos);
        SetPlayerSource();
        player.Play();
    }

XAML:

代码语言:javascript
复制
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="41,-8,-17,8">
        <Button x:Name="C_key" Content="" HorizontalAlignment="Left" Height="220" Margin="8,0,0,8" Style="{StaticResource C}" VerticalAlignment="Bottom" Width="75" Click="C_Key_Click"/>
        <Button x:Name="D_key" Content="" HorizontalAlignment="Left" Height="220" Margin="87,0,0,8" Style="{StaticResource D}" VerticalAlignment="Bottom" Width="75" Click="D_Key_Click"/>
        <Button x:Name="E_key" Content="" HorizontalAlignment="Left" Height="220" Margin="166,0,0,8" Style="{StaticResource E}" VerticalAlignment="Bottom" Width="75" Click="E_Key_Click"/>
        <Button x:Name="F_key" Content="" HorizontalAlignment="Left" Height="220" Margin="245,0,0,8" Style="{StaticResource F}" VerticalAlignment="Bottom" Width="75" d:LayoutOverrides="Width" Click="F_Key_Click"/>
        <Button x:Name="G_key" Content="" Height="220" Margin="324,0,305,8" Style="{StaticResource G}" VerticalAlignment="Bottom" Click="G_Key_Click"/>
        <Button x:Name="A_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,226,8" Style="{StaticResource A}" VerticalAlignment="Bottom" Width="75" Click="A_Key_Click"/>
        <Button x:Name="B_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,147,8" Style="{StaticResource B}" VerticalAlignment="Bottom" Width="75" Click="B_Key_Click"/>
        <Button x:Name="C2_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,68,8" Style="{StaticResource C2}" VerticalAlignment="Bottom" Width="75" Click="C2_Key_Click"/>
        <MediaElement Height="120" HorizontalAlignment="Left" Margin="8,6,0,0" Name="player" VerticalAlignment="Top" Width="160" Source="{Binding Audio}" Volume="1" AutoPlay="False"/>
     </Grid>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-12 04:37:18

根据MSDN,您应该在设置AutoPlay属性之前将false设置为Source

SetPlayerSource中设置Source时,可以从XAML中删除设置器。除此之外,代码看起来不错。

但是,我刚刚意识到您是在调用SetPlayerSource之后直接调用Play。这将不会给你的代码在尝试播放之前实际加载媒体的时间。

您需要在MediaOpened事件上调用Play

票数 6
EN

Stack Overflow用户

发布于 2012-01-12 04:45:58

首先,将MediaElement中的Name=player替换为x:Name=player。这有时会导致其他控件出现问题。

如果这不起作用,您可能会在音频加载完成之前尝试调用Play方法。相反,可以尝试处理MediaOpened事件并在那里播放文件。

代码语言:javascript
复制
private void player_MediaOpened(object sender, RoutedEventArgs e)
{
     player.Play();
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8826109

复制
相关文章

相似问题

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