如何在Silverlight中连续播放小型声音文件?
我有两个有关Silverlight的SoundPlay操作和属性的问题。我的情况是这样的:如何在Silverlight中连续播放小型声音文件?
我有两个故事板: 第一个故事板有一个图像和一个声音文件;当silverlight应用程序加载时,声音会自动播放,但如果有人点击图像,声音文件将停止,第二个故事板将以新的声音文件开始。
1)我的第一个问题是如何在第二个故事板以第二个声音文件开始时停止第一个故事板的第一个声音文件。
2)我的第二个问题是如何连续播放声音文件;例如,在Silverlight中,我们可以使用RepeatBehavior =“Forever”连续播放故事板;但我无法找到一种永久或连续播放我的10秒声音文件的方法。
注意:我附加了一个小的XAML文件来显示我在说什么;我还指出,如果不是图像文件,如果有按钮,那么我可以在点击按钮后停止第一个音乐文件,并用新的声音文件启动我的第二个故事板,但我想使用图像文件而不是按钮。可能吗?如果是这样,怎么做?
因此,请你回答我以下两个问题,或者提供
1)如何当第二个故事板与第二声音文件开始停止第一个故事板的第一个声音文件大提示或网页教程链接(当可点击元素是图片而不是按钮时) 2)如何连续播放10秒的声音文件?
............代码段......................
XAML .... ........
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="testPrj.MainPage"
Width="640" Height="480">
<UserControl.Resources>
<Storyboard x:Name="Storyboard1" RepeatBehavior="Forever"/>
<Storyboard x:Name="Storyboard2"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Red">
<Button HorizontalAlignment="Left" Margin="212,0,0,111" VerticalAlignment="Bottom" Width="75" Content="Button" Click="onClick"/>
<MediaElement x:Name="sound2_mp3" Height="0" HorizontalAlignment="Left" Margin="105,230,0,0" VerticalAlignment="Top" Width="0" Source="/sound2.mp3" Stretch="Fill"/>
<MediaElement x:Name="sound1_mp1" Height="0" HorizontalAlignment="Left" Margin="190,164,0,0" VerticalAlignment="Top" Width="0" Source="/sound1.mp3" Stretch="Fill" AutoPlay="False"/>
</Grid>
</UserControl>
...............
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace testPrj
{
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
private void onClick(object sender, System.Windows.RoutedEventArgs e)
{
Storyboard1.Stop();
sound2_mp3.Stop();
sound1_mp1.Play();
}
}
}
这两个问题得到了解决:
2)我的第二个问题是如何连续播放一个声音文件;例如,在Silverlight中,我们可以使用RepeatBehavior =“Forever”连续播放故事板;但我无法找到一种永久或连续播放我的10秒声音文件的方法。
添加MediaEnded事件处理程序后,问题得到解决;现在
我改变了XAML代码: .. .. ...
<MediaElement x:Name="sound2_mp3" Height="0" HorizontalAlignment="Left" Margin="105,230,0,0" VerticalAlignment="Top" Width="0" Source="/sound2.mp3" Stretch="Fill" DataContext="{Binding}" MediaEnded="start" AutoPlay="True" />
<MediaElement x:Name="sound1_mp1" Height="0" HorizontalAlignment="Left" Margin="190,164,0,0" VerticalAlignment="Top" Width="0" Source="/sound1.mp3" Stretch="Fill" AutoPlay="False" MediaEnded="start1"/>
... ... ...
我的隐藏文件代码:
private void start(object sender, RoutedEventArgs e)
{
sound2_mp3.Position = TimeSpan.FromSeconds(0);
sound2_mp3.Play();
}
private void start1(object sender, RoutedEventArgs e)
{
sound1_mp1.Position = TimeSpan.FromSeconds(0);
sound1_mp1.Play();
}
我的第一个问题是:
1)如何当第二个故事板与第二开始停止第一个故事板的第一个声音文件声音文件(当可点击的元素是一个图像,而不是一个按钮)
这是可行的,并得到解决。我不知道为什么我没有调查和尝试使用图像元素的事件类型。因此,结论是: 如果可点击元素是图像,则可以触发事件;例如在下面的代码段中,我使用MouseLeftButtonUp事件。
XAML
<Image HorizontalAlignment="Left" Margin="88,239,0,118" Width="95" Source="me1.png" Stretch="Fill" MouseLeftButtonUp="imgGotClicked"/>
代码文件
private void imgGotClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("Hello World!");
Storyboard1.Stop();
Storyboard2.Begin();
}
背后因此,问题#1和问题2都得到了解决。这个线程的问题解决了。因此,它应该被关闭。
感谢buddy..problem解决了 – Rakeshyadvanshi 2015-09-06 15:33:16