分享时间 | 2023-03-23 22:07 |
最后更新 | 2024-02-20 16:38 |
修订版本 | 1 |
用户许可 | -未设置- |
Quicker版本 | 1.42.9 |
动作大小 | 27.9 KB |
```XMAL
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:qk="https://getquicker.net"
Title="MP3 Player" Height="150" Width="300">
<Grid>
<!-- 定义三行三列 -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 在第一行第一列放置一个按钮 -->
<Button Name="playButton" Content="playButton" Grid.Column = "0" Grid.Row = "0"/>
<Button Name="pauseButton" Content="pauseButton" Grid.Column = "0" Grid.Row = "1"/>
<!-- 在第二行第二列放置一个列表 -->
<ListBox Name = "mp3List" Grid.Column = "1" Grid.Row = "1"/>
<!-- 在第三行跨越三列放置一个进度条 -->
<ProgressBar Name = "mp3Progress" Grid.Column = "0" Grid.Row = "2"
Grid.ColumnSpan = "3"/>
</Grid>
</Window>
```
```cs
using Quicker.Public;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using NAudio.Wave;
using Microsoft.Win32;
using System.Net;
// ...
// 声明一个类级别的字段
public static IWavePlayer waveOutDevice;
public static void OnWindowCreated(Window win, IDictionary<string, object> dataContext,ICustomWindowContext winContext){
Button playButton = win.FindName("playButton") as Button;
playButton.Click += playButton_Click;
Button pauseButton = win.FindName("pauseButton") as Button;
pauseButton.Click += pauseButton_Click;
}
public static void playButton_Click(object sender, EventArgs e){
// 判断 waveOutDevice 是否存在
if(waveOutDevice != null){
// 判断 waveOutDevice 的播放状态是否是暂停
if(waveOutDevice.PlaybackState == PlaybackState.Paused){
// 调用 Play 或 Resume 方法恢复播放
waveOutDevice.Play();
//waveOutDevice.Resume();
// 结束方法
return;
}
// 判断 waveOutDevice 的播放状态是否是播放
if(waveOutDevice.PlaybackState == PlaybackState.Playing){
// 不执行任何操作
return;
}
}
// 如果 waveOutDevice 不存在或者不是暂停或者播放状态,就像之前一样初始化并播放音乐
// 创建一个 WaveOutEvent 的实例并赋值给字段
waveOutDevice = new WaveOutEvent ();
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath = System.IO.Path.Combine(path, "邂逅你的春天.mp3");
using (var client = new System.Net.WebClient())
{
client.DownloadFile("http://music.163.com/song/media/outer/url?id=2031874714.mp3", filePath);
}
AudioFileReader audioFileReader = new AudioFileReader (filePath);
waveOutDevice.Init (audioFileReader);
waveOutDevice.Play ();
}
public static void pauseButton_Click(object sender, EventArgs e){
try{
// 访问字段
if(waveOutDevice != null){
waveOutDevice.Pause ();
}
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
```
修订版本 | 更新时间 | 更新说明 |
---|---|---|
1 | 2024-02-20 16:38 | 简易音频播放器 |
0 | 2023-03-23 22:07 |