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(@"\bin");
if (index > 0)
{
str = str.Substring(0, index) + @"\sound\AlarmSound.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:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.wma"
// Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma")
var path = @"C:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.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:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.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(@"\bin"); if (index > 0) { str = str.Substring(0, index) + @"\sound\AlarmSound.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:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.wma" // Src = new Uri("ms-appx:///HPControls/Assets/AlarmSound.wma") var path = @"C:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.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:\Users\bruce\Desktop\UWP\WinTest\sound\AlarmSound.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(); } }