Silverlight中播放聲音mp3檔案

暖楓無敵發表於2014-04-10

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、效果如下圖:

相關文章