繁体   English   中英

如何在Windows Universal应用程序中播放Assets中的声音

[英]how to play sound from Assets in windows universal app

我在project.shared中的资产中添加了声音,并在XAML页面中添加了MediaElement

我想在点击图像时播放声音。

这是我的代码,但是没有用。

</Grid>
    <GridView VerticalAlignment="Bottom" Margin="-10,-10,10,10" Height="260" Grid.RowSpan="2">
        <StackPanel Orientation="Vertical">
            <Image Source="/Assets/Animals/cat.png" Height="200" Width="200" HorizontalAlignment="Center" Tapped="Image_Tapped"/>
            <TextBlock Text="Cat" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30"/>
        </StackPanel>
    </GridView>
    <MediaElement  Name="AudioPlayer" HorizontalAlignment="Left" Height="100" Margin="297,290,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100" Visibility="Collapsed" Volume="1"/>
</Grid> 

这是我的事件处理程序

private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
    cat.play();
}

首先,您需要定义源文件。 您甚至可以通过添加以下内容,将其添加到MediaElement的XAML属性中

Source =“ mediafile.mp3”

到线。

其次,您的MediaElement的名称为“ AudioPlayer”,因此您应该从事件处理程序中调用

AudioPlayer.Play();

而不是cat.play();

暂无
暂无

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

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