一.C#模拟酷我音乐盒
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace DamonMusic { public partial class Form1 : Form { private String[] songs; private String[] lyrics; private String[] pics; private int songPointer = 0; private Label[] lyricLabels = new Label[100]; public Form1() { InitializeComponent(); } // type = 1 下一首 type = -1 上一首 private int checkBound(int songPointer, int type){ Boolean arrivedMaximum = false; Boolean arrivedMinimum = false; if(type == 1) songPointer += 1; else{ songPointer -= 1; } if (songPointer == songs.Length) { arrivedMaximum = true; } if (songPointer == -1) { arrivedMinimum = true; } if (arrivedMaximum) { songPointer = 0; } if (arrivedMinimum) { songPointer = songs.Length - 1; } return songPointer; } private void Form1_Load(object sender, EventArgs e) { songs = Directory.GetFiles("song", "*.mp3"); lyrics = Directory.GetFiles("lyric", "*.lrc"); pics = Directory.GetFiles("image", "*.jpg"); //相对路径 if (songs.Length != 0){ axWindowsMediaPlayer1.URL = songs[songPointer]; LyricFiles lyricFiles = new LyricFiles(); lyricFiles.LoadLyric(lyrics[songPointer]); int yStep = 0; for (int i = 0; i < lyricFiles.lstLyric.Count; i++) { Label lblLyric = new Label(); lblLyric.Size = new System.Drawing.Size(500, 30); lblLyric.BackColor = Color.Transparent; lblLyric.ForeColor = Color.White; lblLyric.Font = new System.Drawing.Font("微软雅黑", 14); lblLyric.Text = lyricFiles.lstLyric[i].strLyric; lblLyric.Location = new Point(60, yStep); yStep += 40; lyricLabels[i] = lblLyric; this.Controls.Add(lblLyric); } this.BackgroundImage = Image.FromFile(pics[songPointer]); //songPointer = checkBound(songPointer,1); } else axWindowsMediaPlayer1.URL = null; axWindowsMediaPlayer1.Ctlcontrols.stop(); //初始化 准备工作 pnlControlBar.BackColor = Color.FromArgb(127, 200, 200, 200); }3.1 歌曲播放设定实现
bool isplay = false; private void pictureBox9_Click(object sender, EventArgs e) { isplay = !isplay; //播放歌曲 if (isplay) { lblShowDuration.Text = axWindowsMediaPlayer1.currentMedia.durationString; axWindowsMediaPlayer1.Ctlcontrols.play(); picPlay.BackgroundImage = Properties.Resources.pause_circle_o; } else//暂停播放 { axWindowsMediaPlayer1.Ctlcontrols.pause(); picPlay.BackgroundImage=Properties.Resources.play_circle_o; } //打开文件 // Encoding encode = Encoding.GetEncoding("GB2312"); // FileStream fs=new FileStream ("音阙诗听-红昭愿.lrc",FileMode .Open ); // StreamReader sr=new StreamReader (fs,encode ); // // string line = sr.ReadLine(); // //操作文件 // //richTextBox1.Text = sr.ReadToEnd(); // //关闭文件 // sr.Close(); // fs.Close(); } private void pictureBox10_Click(object sender, EventArgs e) { isplay = true; for (int i = 0; i < 100; i++) { if (lyricLabels[i] == null) { continue; } this.Controls.Remove(lyricLabels[i]); }3.2歌词显示实现
songPointer = checkBound(songPointer, 1); axWindowsMediaPlayer1.URL = songs[songPointer]; axWindowsMediaPlayer1.Ctlcontrols.play(); picPlay.BackgroundImage = Properties.Resources.pause_circle_o; LyricFiles lyricFiles = new LyricFiles(); lyricFiles.LoadLyric(lyrics[songPointer]); int yStep = 0; for (int i = 0; i < lyricFiles.lstLyric.Count; i++) { Label lblLyric = new Label(); lblLyric.Size = new System.Drawing.Size(500, 30); lblLyric.BackColor = Color.Transparent; lblLyric.ForeColor = Color.White; lblLyric.Font = new System.Drawing.Font("微软雅黑", 14); lblLyric.Text = lyricFiles.lstLyric[i].strLyric; lblLyric.Location = new Point(60, yStep); yStep += 40; lyricLabels[i] = lblLyric; this.Controls.Add(lblLyric); } this.BackgroundImage = Image.FromFile(pics[songPointer]); } private void pictureBox8_Click(object sender, EventArgs e) { isplay = true; for (int i = 0; i < 100; i++) { if (lyricLabels[i] == null) { continue; } this.Controls.Remove(lyricLabels[i]); } songPointer = checkBound(songPointer, -1); axWindowsMediaPlayer1.URL = songs[songPointer]; axWindowsMediaPlayer1.Ctlcontrols.play(); picPlay.BackgroundImage = Properties.Resources.pause_circle_o; LyricFiles lyricFiles = new LyricFiles(); lyricFiles.LoadLyric(lyrics[songPointer]); int yStep = 0; for (int i = 0; i < lyricFiles.lstLyric.Count; i++) { Label lblLyric = new Label(); lblLyric.Size = new System.Drawing.Size(500, 30); lblLyric.BackColor = Color.Transparent; lblLyric.ForeColor = Color.White; lblLyric.Font = new System.Drawing.Font("微软雅黑", 14); lblLyric.Text = lyricFiles.lstLyric[i].strLyric; lblLyric.Location = new Point(60, yStep); yStep += 40; lyricLabels[i] = lblLyric; this.Controls.Add(lblLyric); } this.BackgroundImage = Image.FromFile(pics[songPointer]); } } }