繁体   English   中英

如何在Silverlight中连续播放小声音文件?

[英]How to play small sound file continuously in Silverlight?

我有一个关于Silverlight的SoundPlay动作和性质的两个问题。 我的情况是这样的:

我有两个故事板:第一个故事板具有图像和声音文件; 当加载Silverlight应用程序时,声音会自动开始播放,但是如果有人单击图像,声音文件将停止,第二个情节提要将以新的声音文件开始。

1)我的第一个问题是,当第二个故事板从第二个声音文件开始时,如何停止第一个故事板的第一个声音文件。

2)我的第二个问题是如何连续播放声音文件; 例如,在Silverlight中,我们可以使用RepeatBehavior =“ Forever”连续播放故事板; 但是我找不到永久或连续播放10秒声音文件的方法。

注意:我已经附加了一个小的XAML文件来显示我在说什么。 我还要说明的是,如果不是图像文件,而是按钮,那么可以在单击按钮后停止第一个音乐文件,并使用新的声音文件启动第二个故事板,但是我想使用图像文件而不是按钮。 可能吗? 如果是,该怎么办?

因此,请回答我以下两个问题或给大暗示或网站链接教程

1)当第二个故事板以第二个声音文件开始时,如何停止第一个故事板的第一个声音文件(当clickable元素是图像而不是按钮时)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)当第二个故事板以第二个声音文件开头时,如何停止第一个故事板的第一个声音文件(当clickable元素是图像而不是按钮时)

这是可行的并得到解决。 我不知道为什么我没有调查和试验图像元素的事件类型。 因此,得出的结论是:如果可点击元素是图像,则可以触发事件。 例如在下面的代码段中,我正在使用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都得到解决。 该线程的问题已解决。 因此,应将其关闭。

暂无
暂无

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

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