• uwp 中的音频开发


    xml code

    ---------------------------------------------------

    <UserControl x:Class="WinTest.HPControl.ReminderTimer"

                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

                 xmlns:local="clr-namespace:WinTest.HPControl"

                 mc:Ignorable="d" Height="158.036" Width="417.35">

        <Grid>

            <Border  BorderBrush="White"  Background="White" CornerRadius="20" >

                <Grid Background="Transparent" Width="300">

                    <Grid.RowDefinitions>

                        <RowDefinition></RowDefinition>

                        <RowDefinition></RowDefinition>

                        <RowDefinition></RowDefinition>

                        <RowDefinition></RowDefinition>

                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>

                        <ColumnDefinition Width="80"></ColumnDefinition>

                        <ColumnDefinition Width="10"></ColumnDefinition>

                        <ColumnDefinition Width="*"></ColumnDefinition>

                    </Grid.ColumnDefinitions>

                    <Button Background="Transparent"  Grid.Row="0" Grid.RowSpan="3" Click="Button_Click" >

                        <Image   Source="/images/Alarm.png"  ></Image>

                    </Button>

                   

                    <TextBlock FontFamily="{StaticResource RegularFont}" Foreground="#333333" FontSize="18" Grid.Column="2" Grid.Row="0">时间</TextBlock>

                    <TextBlock FontFamily="{StaticResource LightFont}" FontWeight="Light" Foreground="#333333" FontSize="16" Grid.Column="2" Grid.Row="1">时间</TextBlock>

                    <TextBlock FontFamily="{StaticResource LightFont}" FontWeight="Light" Foreground="#666666" FontSize="10" Grid.Column="2" Grid.Row="2">时间</TextBlock>

                    <StackPanel Grid.Column="2" Grid.Row="3" Orientation="Horizontal">

                        <ToggleButton>toggle switch</ToggleButton>

                        <TextBlock Margin="20,0,0,0">提醒已生效</TextBlock>

                        <Button Click="ReminderTimer_replay">replay</Button>

                    </StackPanel>

                </Grid>

            </Border>

        </Grid>

    </UserControl>

    C#  code  后台

    ------------------------------------------------------------------

     public partial class ReminderTimer : UserControl

        {

            private SoundPlayer player = null;

            private MediaPlayer currentPlayer = null;

            public ReminderTimer()

            {

                InitializeComponent();

                this.Loaded += ReminderTimer_Loaded;

                player = new SoundPlayer();

                this.Unloaded += ReminderTimer_Unloaded;

            }

     

            private void ReminderTimer_Unloaded(object sender, RoutedEventArgs e)

            {

                currentPlayer.Stop();

                currentPlayer.Close();

     

            }

     

            private void ReminderTimer_Loaded(object sender, RoutedEventArgs e)

            {

                initalplayer();

            }

            private void inital()

            {

                string str = System.IO.Directory.GetCurrentDirectory();

                string path = Environment.CurrentDirectory;

                string p = new Uri("ms-appx:///sound/AlarmSound.wma").ToString();

                // string m=  Application.StartupPath;

                //  player.SoundLocation = "/sound/美国往事.wav";//(将播放音乐放在应用程序Debug目录下)

                // player.SoundLocation = "美国往事.wav";

                int index = str.LastIndexOf(@"in");

                if (index > 0)

                {

                    str = str.Substring(0, index) + @"soundAlarmSound.wma";

                }

                player.SoundLocation = str;

                player.Load();

                //音乐播放

                player.Play();

            }

            //ToastAudio Audio;

            private void initalplayer()

            {

                currentPlayer = new MediaPlayer();

     

     

                //Audio = new ToastAudio()

                //{

                //    Loop = true,

                //    Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma")

                //}

     

     

                //"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma"

                // Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma")

                var path = @"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma";

                //Uri audioUri = new Uri("ms-appx:///WinTest/sound/AlarmSound.wma");

                Uri audioUri = new Uri(path);

                

                currentPlayer.Open(audioUri);

                currentPlayer.MediaEnded += CurrentPlayer_MediaEnded;

                currentPlayer.Play();

            }

     

            private void CurrentPlayer_MediaEnded(object sender, EventArgs e)

            {

                var path = @"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma";

                //Uri audioUri = new Uri("ms-appx:///WinTest/sound/AlarmSound.wma");

                Uri audioUri = new Uri(path);

     

                currentPlayer.Open(audioUri);

                currentPlayer.Play();

            }

     

            private void Button_Click(object sender, RoutedEventArgs e)

            {

                currentPlayer.Stop();

            }

     

            private void ReminderTimer_replay(object sender, RoutedEventArgs e)

            {

                currentPlayer.Play();

            }

        }

     

     public partial class ReminderTimer : UserControl    {        private SoundPlayer player = null;        private MediaPlayer currentPlayer = null;        public ReminderTimer()        {            InitializeComponent();            this.Loaded += ReminderTimer_Loaded;            player = new SoundPlayer();            this.Unloaded += ReminderTimer_Unloaded;        }
            private void ReminderTimer_Unloaded(object sender, RoutedEventArgs e)        {            currentPlayer.Stop();            currentPlayer.Close();
            }
            private void ReminderTimer_Loaded(object sender, RoutedEventArgs e)        {            initalplayer();        }        private void inital()        {            string str = System.IO.Directory.GetCurrentDirectory();            string path = Environment.CurrentDirectory;            string p = new Uri("ms-appx:///sound/AlarmSound.wma").ToString();            // string m=  Application.StartupPath;            //  player.SoundLocation = "/sound/美国往事.wav";//(将播放音乐放在应用程序Debug目录下)            // player.SoundLocation = "美国往事.wav";            int index = str.LastIndexOf(@"in");            if (index > 0)            {                str = str.Substring(0, index) + @"soundAlarmSound.wma";            }            player.SoundLocation = str;            player.Load();            //音乐播放            player.Play();        }        //ToastAudio Audio;        private void initalplayer()        {            currentPlayer = new MediaPlayer();

                //Audio = new ToastAudio()            //{            //    Loop = true,            //    Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma")            //}

                //"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma"            // Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma")            var path = @"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma";            //Uri audioUri = new Uri("ms-appx:///WinTest/sound/AlarmSound.wma");            Uri audioUri = new Uri(path);                        currentPlayer.Open(audioUri);            currentPlayer.MediaEnded += CurrentPlayer_MediaEnded;            currentPlayer.Play();        }
            private void CurrentPlayer_MediaEnded(object sender, EventArgs e)        {            var path = @"C:UsersruceDesktopUWPWinTestsoundAlarmSound.wma";            //Uri audioUri = new Uri("ms-appx:///WinTest/sound/AlarmSound.wma");            Uri audioUri = new Uri(path);
                currentPlayer.Open(audioUri);            currentPlayer.Play();        }
            private void Button_Click(object sender, RoutedEventArgs e)        {
                currentPlayer.Stop();

            }
            private void ReminderTimer_replay(object sender, RoutedEventArgs e)        {            currentPlayer.Play();        }    }

  • 相关阅读:
    让svn自动更新代码注释中的版本号
    前端开发利器F5
    当inlineblock和textindent遇到IE6,IE7
    DSL与函数式编程
    [译]当Node.js遇上WebMatrix 2
    《大道至简》的读后感
    深度学习之卷积神经网络之一
    ORACLE TRUNC()函数
    oracle rownum
    ORACLE 忽略已有重复值 创建唯一约束
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14225779.html
Copyright © 2020-2023  润新知