Inspired by this post chiefs, similar to the first part of the original post: View original post , some only slightly supplement not mentioned, after all, knowledge shortsighted.
Page design section
The same as the original paste, adding a label only long duration and total song playback to show the current playback section. Add windows comes windowsMediaPlayer control method: Right Toolbox - click options - click the COM component - to find and check the windowsMediaPlayer-- determine onto the form.
Note: windowsMediaPlayer own control interface controls can be used directly
Code implementation part
1. Add a song
First, we declare a few variables. max represents the total length of the song time, min represents the position of the currently playing song, bal expressed as a percentage of the length when the song is playing.
double max, min, bal; //为播放进度条提前声明变量
Add Songs button code:
private void button_Addmusic_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog(); //实例化一个通用对话框
open.Filter = "音频文件(*.mp3)|*.mp3"; //选择文件格式
if (open.ShowDialog() == DialogResult.OK)
{
//还原最大值最小值及进度条位置
max = 0.0;
min = 0.0;
bal = 0.0;
trackBar_progress.Value = 0;
myMediaPlayer.URL = open.FileName; //添加到播放器组件
listBox_music.Items.Add(open.FileName); //将音频文件路径添加到列表框内
listBox_music.SelectedIndex = listBox_music.Items.Count - 1; //列表框选中项为添加的歌曲路径
timer_progress.Enabled = true; //开始检测进度
}
}
2. playback progress
windowsMediaPlayer control comes with a progress bar, but here we use trackBar control from a system, because the progress bar change depending time, so add a timer control, named timer_progress, which Interval property to 1000, an event that is triggered once per second. Where the tick to register an event, as follows:
private void timer_progress_Tick(object sender, EventArgs e)
{
try
{
max = myMediaPlayer.currentMedia.duration; //获取文件的时间长度
min = myMediaPlayer.Ctlcontrols.currentPosition;//获取文件的当前播放位置
bal = min / max; //计算百分比
trackBar_progress.Value = (int)(bal * 100); //设置滑块位置
//将时长转化为XX:XX并显示在相应label上
int intmax = (int)max;
int intmin = (int)min;
int intminM = intmin / 60;
int intminS = intmin % 60;
int intmaxM = intmax / 60;
int intmaxS = intmax % 60;
label_time.Text = intminM.ToString() + ":" + intminS.ToString() + "/" + intmaxM.ToString() + ":" + intmaxS.ToString();
//截取路径的文件名部分显示在“当前歌曲”的label上
string nowsong = listBox_music.SelectedItem.ToString();
label_nowsong.Text = nowsong.Substring(nowsong.LastIndexOf("\\") + 1);
}
catch (Exception)
{
//切换歌曲瞬间可能导致max,min无值,添加try跳转报错但并不对报错进行处理
}
}
3. Drag Play
To realize drag that is due to play "press pause", "release continued," two events. When the progress bar is pressed, playback pauses. After sliding to a specified location, release the button progress bar, playback continues from the current release position.
The "Press Pause" that trackBar the MouseDown event code is as follows:
private void trackBar_progress_MouseDown(object sender, MouseEventArgs e)
{
timer_progress.Enabled = false; //停止检测播放进度
myMediaPlayer.Ctlcontrols.pause(); //暂停当前播放文件
}
The "release continued," that is trackBar the MouseUp event code is as follows:
private void trackBar_progress_MouseUp(object sender, MouseEventArgs e)
{
double newValue = trackBar_progress.Value * 0.1 * 0.1 * max;
myMediaPlayer.Ctlcontrols.currentPosition = newValue; //为播放控件赋予新进度
myMediaPlayer.Ctlcontrols.play(); //从当前进度开始播放
timer_progress.Enabled = true; //开始检测播放进度
}
4. start playing
Click the "Start button" that play, as follows:
private void button_Start_Click(object sender, EventArgs e)
{
myMediaPlayer.Ctlcontrols.play(); //开始播放
}
Of course, you can play songs by specifying a path, double-click the song list, the code is as follows:
private void listBox_music_DoubleClick(object sender, EventArgs e)
{
//若点击的歌曲列表有效,为播放控件赋予新路径
if (listBox_music.SelectedIndex != -1)
{
myMediaPlayer.URL = listBox_music.SelectedItem.ToString();
}
}
5. Pause Play
Click the "pause button" that is playing to pause, as follows:
private void button_Stop_Click(object sender, EventArgs e)
{
myMediaPlayer.Ctlcontrols.pause(); //暂停播放
}
6. Stop Play
Click the "stop button" stop playing the current code is as follows:
private void button_End_Click(object sender, EventArgs e)
{
myMediaPlayer.Ctlcontrols.stop(); //停止播放
}
Of course, just close the program can stop playing.
Postscript part
Select a song, show schedule, start, pause, stop playing at this time of simple music player has been basically run.On follow-up lyrics display, you can look at the second part of the C # synchronous lyrics displayWhen the next original post As mentioned in the list of songs loaded to open, next time, after all, knowledge shallow, and forgive us.