Silverlight中播放声音mp3文件

1、案例场景:

在水利防汛中,当出现险情时,需要预警,在使用Silverlight框架中,如何播放音频文件?

 

2、实现办法

Silverlight控件中有MediaPlayer控件,可以使用,在SL项目中播放承载SL的Web项目路径下的视频文件,就可以实现这样的目的

 

     <Border x:Name="bWarnning" Height="35" Margin="350,8,0,0" VerticalAlignment="Top" BorderBrush="#FFA7A7A7" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Left" Width="229" Background="White" Opacity="0.9">
            <Grid x:Name="gWarning" Margin="0,0,-2,0" d:LayoutOverrides="HorizontalAlignment">
                <TextBlock TextWrapping="Wrap" Text="报警声音:" Width="74" FontWeight="Bold" FontFamily="Microsoft YaHei" Foreground="Red" FontSize="14.667" Canvas.Left="-73" Margin="0,6,132,8" HorizontalAlignment="Right" d:LayoutOverrides="Height"/>
                <Canvas Width="24" Margin="104,0,93,0" Height="24">
                    <Canvas.Background>
                        <ImageBrush Stretch="Fill" ImageSource="Images/sound.png"/>
                    </Canvas.Background>
                  
  <MediaElement x:Name="me_warning" Width="24" Height="24" Cursor="Hand" MediaEnded="me_warning_MediaEnded">
                    </MediaElement>

                </Canvas>
                <CheckBox x:Name="cb_jy" Content="" Canvas.ZIndex="1" Click="cb_jy_Click" Canvas.Left="38" Canvas.Top="3" Margin="146,9,0,8" HorizontalAlignment="Left" Width="17" d:LayoutOverrides="Height"/>
                <TextBlock Margin="168,6,24,-1" TextWrapping="Wrap" Text="静音" FontSize="14.667" FontFamily="Microsoft YaHei" Foreground="Red" FontWeight="Bold"/>
            </Grid>
        </Border>

 

#region 预警声音控制

        /// <summary>
        /// 设置预警声音来源
        /// </summary>
        public void SetMediaPlayer()
        {
           
string[] s = { "ClientBin" };
            string url = App.Current.Host.Source.OriginalString.ToString().Split(s, StringSplitOptions.RemoveEmptyEntries)[0] + "ClientBin/media/jb.mp3"; //这里是Web目录下的音频文件地址
            this.me_warning.Source = new Uri(url, UriKind.Absolute);

        }

        /// <summary>
        /// 开始播放
        /// </summary>
        public void PlayYJSound()
        {
            this.me_warning.Play();
        }

        /// <summary>
        /// 结束后重新播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_warning_MediaEnded(object sender, RoutedEventArgs e)
        {
           
MediaElement media = (MediaElement)sender;
            media.Position = new TimeSpan(0);
            media.Play();

        }


        /// <summary>
        /// 暂停播放
        /// </summary>
        public void PauseYJSound()
        {
            this.me_warning.Pause();
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void StopYJSound()
        {
            this.me_warning.Stop();
        }

        /// <summary>
        /// 静音操作控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_jy_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CheckBox currRadioButton;
            currRadioButton = sender as CheckBox;
            if (currRadioButton != null && currRadioButton.IsChecked.Value)
            {
                this.me_warning.IsMuted = true;
            }
            else
            {
                this.me_warning.IsMuted = false;
            }
        }

        #endregion

 

3、效果如下图:

Silverlight中播放声音mp3文件_第1张图片

你可能感兴趣的:(Silverlight中播放声音mp3文件)