简体   繁体   English

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

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

I have two questions regarding Silverlight's SoundPlay action and properties. 我有一个关于Silverlight的SoundPlay动作和性质的两个问题。 My scenario is like: 我的情况是这样的:

I have two story board: The first story board has an image and a sound file; 我有两个故事板:第一个故事板具有图像和声音文件; when the silverlight application gets loaded, the sound starts to play automatically, but if someone clicks the image, the sound file will stop and the second storyboard will start with a new sound file. 当加载Silverlight应用程序时,声音会自动开始播放,但是如果有人单击图像,声音文件将停止,第二个情节提要将以新的声音文件开始。

1) My first question is how to stop the first sound file of first story board when the second story board starts with the second sound file. 1)我的第一个问题是,当第二个故事板从第二个声音文件开始时,如何停止第一个故事板的第一个声音文件。

2) My second question is how to play a sound file continuously; 2)我的第二个问题是如何连续播放声音文件; for example, in Silverlight we can play a story board continuously with RepeatBehavior="Forever"; 例如,在Silverlight中,我们可以使用RepeatBehavior =“ Forever”连续播放故事板; but I cannot find a way to play my 10 second sound file forever or continuously. 但是我找不到永久或连续播放10秒声音文件的方法。

Note: I have attached a small XAML file to show what I am talking about; 注意:我已经附加了一个小的XAML文件来显示我在说什么。 I am also stating that if instead of an image file, if there were a button, then I can stop the first music file after I click the button and start my second story board with a new sound file, but I would like to use image file instead of a button. 我还要说明的是,如果不是图像文件,而是按钮,那么可以在单击按钮后停止第一个音乐文件,并使用新的声音文件启动第二个故事板,但是我想使用图像文件而不是按钮。 Is it possible? 可能吗? If it is, how to do it? 如果是,该怎么办?

Therefore, please answer my following two questions or give big hint or website tutorial links on 因此,请回答我以下两个问题或给大暗示或网站链接教程

1) How to stop the first sound file of first story board when the second story board starts with the second sound file ( When the clickable element is an image instead of a button) 2) How to play a 10 second sound file continuously? 1)当第二个故事板以第二个声音文件开始时,如何停止第一个故事板的第一个声音文件(当clickable元素是图像而不是按钮时)2)如何连续播放10秒的声音文件?

............Code Snippet...................... ............代码段......................

XAML ............ 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();

        }
    }
}

Both of the problems got resolved: 这两个问题都解决了:

2) My second question is how to play a sound file continuously; 2)我的第二个问题是如何连续播放声音文件; for example, in Silverlight we can play a story board continuously with RepeatBehavior="Forever"; 例如,在Silverlight中,我们可以使用RepeatBehavior =“ Forever”连续播放故事板; but I cannot find a way to play my 10 second sound file forever or continuously. 但是我找不到永久或连续播放10秒声音文件的方法。

The problem got solved after I have added MediaEnded event handler; 添加MediaEnded事件处理程序后,问题得到解决。

Now my changed XAML code is: .. .. ... 现在,我更改的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"/>

... ... ... ……

And my Code behind file has: 我的文件背后的代码有:

    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();
    }

And my first question was: 我的第一个问题是:

1) How to stop the first sound file of first story board when the second story board starts with the second sound file ( When the clickable element is an image instead of a button) 1)当第二个故事板以第二个声音文件开头时,如何停止第一个故事板的第一个声音文件(当clickable元素是图像而不是按钮时)

This is doable and got solved. 这是可行的并得到解决。 I do not have any idea why I have not investigated and experimented with the event types of image element. 我不知道为什么我没有调查和试验图像元素的事件类型。 So, the conclusion is: If the click-able element were an image, an event can be fired; 因此,得出的结论是:如果可点击元素是图像,则可以触发事件。 for example in the following code segment I am using MouseLeftButtonUp event. 例如在下面的代码段中,我正在使用MouseLeftButtonUp事件。

XAML XAML

    <Image HorizontalAlignment="Left" Margin="88,239,0,118" Width="95" Source="me1.png" Stretch="Fill" MouseLeftButtonUp="imgGotClicked"/>    

Code Behind File 文件后面的代码

    private void imgGotClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
          MessageBox.Show("Hello World!");
          Storyboard1.Stop();
          Storyboard2.Begin();
    }

Therefore, problem # 1 and problem # 2 both got solved. 因此,问题1和问题2都得到解决。 This thread's problem got solved. 该线程的问题已解决。 Therefore, it should be closed. 因此,应将其关闭。

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

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