gpt4 book ai didi

silverlight - Silverlight 中的 MediaElement 问题

转载 作者:行者123 更新时间:2023-12-04 06:38:06 25 4
gpt4 key购买 nike

我在使用 MediaElement 时遇到困难Windows Phone 7 的 Silverlight 中的控件。我的目标是在用户按下按钮时播放两种音调。我想出的这样做的方法是拥有一个 MediaElement对于每个音调。 (可能有更好的方法吗?)

<phone:PhoneApplicationPage 
x:Class="MediaElementTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<StackPanel x:Name="LayoutRoot" Background="Transparent">

<MediaElement
x:Name="firstTone"
MediaEnded="firstTone_MediaEnded"
Source="{Binding FirstTone}" />
<MediaElement
x:Name="secondTone"
Source="{Binding SecondTone}" />
<Button Content="Play" Click="Button_Click" />

</StackPanel>

</phone:PhoneApplicationPage>

代码隐藏:
public partial class MainPage : PhoneApplicationPage
{
public Uri FirstTone
{
get
{
return new Uri("A.mp3", UriKind.Relative);
}
}

public Uri SecondTone
{
get
{
return new Uri("B.mp3", UriKind.Relative);
}
}

public MainPage()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
firstTone.Stop();
secondTone.Stop();

firstTone.Play();
}

private void firstTone_MediaEnded(object sender, RoutedEventArgs e)
{
secondTone.Play();
}
}

当我单击按钮时,没有声音播放。为什么是这样?我究竟做错了什么?

最佳答案

不要使用 MediaElement为了这。而是添加对 Microsoft.Xna.Framework.dll 的引用(是的,即使在 Silverlight 项目中)然后使用 SoundEffect .

就像是:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using (var stream = TitleContainer.OpenStream("a.mp3"))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();

// This will pause while the first sound plays
// so they don't play over each other but as it also blocks!
Thread.Sleep(effect.Duration);
}

using (var stream = TitleContainer.OpenStream("b.mp3"))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();
}

关于silverlight - Silverlight 中的 MediaElement 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4616543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com