可以桌面字幕透明显示,这个前一篇说过
同步就是判断歌曲当前播放时间去匹配歌词文件里的时间段标志。
很简单的,当然你还可以自动下载歌曲的歌词并同步。
FormLrc 是显示歌词的窗体:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
namespace MyPlayer
{
public partial class FormLrc : Form
{
public FormLrc()
{
TimerLrc2.Interval = 60;
TimerLrc2.Tick += new EventHandler(TimerLrc2_Tick);
InitializeComponent();
this.DoubleBuffered = true;
this.TopMost = true;
timerMask.Tick += new EventHandler(timerMask_Tick);
timerLrc.Tick += new EventHandler(timerLrc_Tick);
timerLrc.Interval = 100;
timerMask.Interval = 200;
InitializeComponent();
timerMask.Enabled = true;
}
/// <summary>
/// 结构体
/// </summary>
struct Lrc
{
public Lrc(string _sTime, string _sText)
{
sTime = _sTime;
sText = _sText;
}
public string sTime;
public string sText;
public override string ToString()
{
return sTime + sText;
}
}
System.Windows.Forms.Timer TimerLrc2 = new Timer();
ArrayList LrcList = new ArrayList();
string sFile = "刘德华 - 冰雨.lrc";
string sOut = "";
string sText = "";
DateTime LrcTime = new DateTime();
Graphics g;
Bitmap bmp = new Bitmap(1024, 500);
Bitmap bmpMask = new Bitmap(1024, 500);
Graphics BmpG;
Graphics MaskGr;
Point p = new Point(2, 2);
Point p1 = new Point(4, 4);
Point p2 = new Point(3, 3);
Brush b = new SolidBrush(Color.Red);
Brush bMask = new SolidBrush(Color.Yellow);// mask color
Brush b2 = new SolidBrush(Color.FromArgb(11, 211, 1, 1));
//Font f = new Font("Times New Roman", 44, FontStyle.Bold);
Font f = new Font("宋体", 44, FontStyle.Bold);
Font f2 = new Font("宋体", 44, FontStyle.Bold);
Color transColor = new Color();
MemoryStream ms = new MemoryStream();
int TextIndex = 0; //ths need mask text index
System.Windows.Forms.Timer timerMask = new Timer();
System.Windows.Forms.Timer timerLrc = new Timer();
public static string SongCurrentTime = "00:00.00";
private void FormTest_Load(object sender, EventArgs e)
{
this.Size = Screen.PrimaryScreen.Bounds.Size;
//LoadLrc("刘德华 - 冰雨.lrc");
}
public void LoadLrc(string _sFile)
{
OpenFile(_sFile);
TimerLrc2.Enabled = true;
// StartShowLrc("刘德华 - 冰雨.lrc");
}
private void button1_Click(object sender, EventArgs e)
{
timerMask.Enabled = false;
richTextBox1.Text = sText;
panel1.Visible = true;
// timerMask.Enabled = (!timerMask.Enabled);
}
protected override void OnPaint(PaintEventArgs e)
{
// if (sText == "") { return; }
g = e.Graphics;
bmp = new Bitmap(1024, 500); ;//reset image
bmpMask = new Bitmap(1024, 124);
// ms = new MemoryStream();//reset
BmpG = Graphics.FromImage(bmp);
transColor = bmp.GetPixel(1, 1);
this.TransparencyKey = transColor;
g.Clear(transColor);
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//BmpG.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
BmpG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
BmpG.DrawString(sText, f2, b2, p1);
BmpG.DrawString(sText, f, b2, p2);
BmpG.DrawString(sText, f, b, p);
//Mask
try
{
BmpG.DrawString(sText.Substring(0, TextIndex), f2, bMask, p);
// MaskGr.DrawString(sText.Substring(0, TextIndex), f, b2, p);
}
catch (Exception ex) { }
bmp.MakeTransparent(Color.White);
// bmp.Save(ms, ImageFormat.Bmp);
g.DrawImage(bmp, p);
// g.DrawImage(bmp, p);
//this.BackgroundImage =(Image) bmp;
//pictureBox1.Image = bmp;
sOut += "Paint \r\n";
// base.OnPaint(e);
// BmpG.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// e.Graphics.DrawString("wgscd 的说法的", f, b, p);
//this.TransparencyKey = transColor;
sOut += "Back Paint\r\n";
base.OnPaintBackground(e);
}
private void FormTest_Click(object sender, EventArgs e)
{
}
#region "拖动窗体"
private bool moveFlag = false;
private int x = 0;
private int y = 0;
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag && (e.Button == MouseButtons.Left))
this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
base.OnMouseMove(e);
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (!moveFlag && e.Clicks >= 1)
moveFlag = true;
x = e.X;
y = e.Y;
base.OnMouseDown(e);
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e);
}
#endregion
void timerMask_Tick(object sender, EventArgs e)
{
// if (sText.Trim() == "") { return; };
this.Refresh();
TextIndex++;
if (TextIndex > sText.Length)
{
TextIndex = 0;
}
}
void timerLrc_Tick(object sender, EventArgs e)
{
//LrcTime = new DateTime(2009, 10, 10, 0, 0, 0, 0);
//if (TextIndex > sText.Length)
//{
// TextIndex = 0;
//}
}
private void button2_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Trim() == "") { return; }
sText = richTextBox1.Text;
panel1.Visible = false;
timerMask.Enabled = true;
}
/// <summary>
///
///读取歌词
/// </summary>
void OpenFile(string _strFile)
{
if (!File.Exists(_strFile))
{
MessageBox.Show("File not fount");
return;
}
string sContent = "";
Stream oStream = null;
StreamReader oReader = null;
string AllLines = "";
string oneLine = "";
try
{
oStream = File.Open(_strFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
oReader = new StreamReader(oStream, Encoding.GetEncoding("gb2312"));
LrcList.Clear();
while (!oReader.EndOfStream)
{
oneLine = oReader.ReadLine() + "\r\n";
AllLines += oneLine;
regLrc(oneLine);
}
oReader.Close();
oStream.Close();
}
catch (Exception ex)
{
oReader.Close();
oStream.Close();
}
richTextBox1.AppendText(AllLines);
}
/// <summary>
/// match the lrcs all lines in to lrc list
/// </summary>
/// <param name="strInput"></param>
/// <returns></returns>
string regLrc(string strInput)
{
//string sPattner="(?<t>\\[\\d[^[]+])(?<w>.*\r\n)";
string sPattner = "(?<t>\\[\\d.*\\]+)(?<w>[^\\[]+\r\n)";
Regex reg = new Regex(sPattner);
foreach (Match mc in reg.Matches(strInput))
{
richTextBox1.AppendText(mc.Groups["t"].ToString() + "\r\n");
richTextBox1.AppendText(mc.Value + "\r\n");
LrcList.Add(new Lrc(mc.Groups["t"].ToString(), mc.Groups["w"].ToString()));
}
return "";
}
/// <summary>
/// 通过时间找歌词
/// </summary>
/// <param name="_sTime"></param>
/// <returns></returns>
string m = "";
Lrc findMatch = new Lrc();
public string FindLrc(string _sTime)
{
foreach (Lrc w in LrcList)
{
// MessageBox.Show(w.ToString());
if (w.sTime.Contains(_sTime.Substring(0, 6)))
{
if (findMatch.Equals(w)) return "";
findMatch = w;
sText = w.sText;
m += w.sText;
return w.sText;
}
}
return "";
}
private void showWord(object sender, EventArgs e)
{
//[03:26.03][02:50.67][01:16.59]冷冷的冰雨在脸上胡乱的拍
// FindLrc2("[02:50.67]");
MessageBox.Show(m);
}
DateTime d1 = new DateTime(1900, 1, 1, 0, 0, 0, 0);
DateTime d2;
DateTime d3;
TimeSpan tsp;
string tTime;
public void TimerLrc2_Tick(object sender, EventArgs e)
{
FindLrc(ClassCommon.SongCrrentTime);
return;
//if (sText.Trim() == "") { return; };
d2 = DateTime.Now;
if (d1.Year == 1900)
{
d1 = d2;
return;
}
TimerLrc2.Enabled = false;
tsp = (TimeSpan)(d2 - d1);
d3 = new DateTime(1900, 1, 1, 0, tsp.Minutes, tsp.Seconds, tsp.Milliseconds);
tTime = d3.ToString("[mm:ss.ff]");
ClassCommon.SongCrrentTime = tTime;
FindLrc(tTime);
this.Text = tTime;
TimerLrc2.Enabled = true;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
namespace MyPlayer
{
public partial class FormLrc : Form
{
public FormLrc()
{
TimerLrc2.Interval = 60;
TimerLrc2.Tick += new EventHandler(TimerLrc2_Tick);
InitializeComponent();
this.DoubleBuffered = true;
this.TopMost = true;
timerMask.Tick += new EventHandler(timerMask_Tick);
timerLrc.Tick += new EventHandler(timerLrc_Tick);
timerLrc.Interval = 100;
timerMask.Interval = 200;
InitializeComponent();
timerMask.Enabled = true;
}
/// <summary>
/// 结构体
/// </summary>
struct Lrc
{
public Lrc(string _sTime, string _sText)
{
sTime = _sTime;
sText = _sText;
}
public string sTime;
public string sText;
public override string ToString()
{
return sTime + sText;
}
}
System.Windows.Forms.Timer TimerLrc2 = new Timer();
ArrayList LrcList = new ArrayList();
string sFile = "刘德华 - 冰雨.lrc";
string sOut = "";
string sText = "";
DateTime LrcTime = new DateTime();
Graphics g;
Bitmap bmp = new Bitmap(1024, 500);
Bitmap bmpMask = new Bitmap(1024, 500);
Graphics BmpG;
Graphics MaskGr;
Point p = new Point(2, 2);
Point p1 = new Point(4, 4);
Point p2 = new Point(3, 3);
Brush b = new SolidBrush(Color.Red);
Brush bMask = new SolidBrush(Color.Yellow);// mask color
Brush b2 = new SolidBrush(Color.FromArgb(11, 211, 1, 1));
//Font f = new Font("Times New Roman", 44, FontStyle.Bold);
Font f = new Font("宋体", 44, FontStyle.Bold);
Font f2 = new Font("宋体", 44, FontStyle.Bold);
Color transColor = new Color();
MemoryStream ms = new MemoryStream();
int TextIndex = 0; //ths need mask text index
System.Windows.Forms.Timer timerMask = new Timer();
System.Windows.Forms.Timer timerLrc = new Timer();
public static string SongCurrentTime = "00:00.00";
private void FormTest_Load(object sender, EventArgs e)
{
this.Size = Screen.PrimaryScreen.Bounds.Size;
//LoadLrc("刘德华 - 冰雨.lrc");
}
public void LoadLrc(string _sFile)
{
OpenFile(_sFile);
TimerLrc2.Enabled = true;
// StartShowLrc("刘德华 - 冰雨.lrc");
}
private void button1_Click(object sender, EventArgs e)
{
timerMask.Enabled = false;
richTextBox1.Text = sText;
panel1.Visible = true;
// timerMask.Enabled = (!timerMask.Enabled);
}
protected override void OnPaint(PaintEventArgs e)
{
// if (sText == "") { return; }
g = e.Graphics;
bmp = new Bitmap(1024, 500); ;//reset image
bmpMask = new Bitmap(1024, 124);
// ms = new MemoryStream();//reset
BmpG = Graphics.FromImage(bmp);
transColor = bmp.GetPixel(1, 1);
this.TransparencyKey = transColor;
g.Clear(transColor);
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//BmpG.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
BmpG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
BmpG.DrawString(sText, f2, b2, p1);
BmpG.DrawString(sText, f, b2, p2);
BmpG.DrawString(sText, f, b, p);
//Mask
try
{
BmpG.DrawString(sText.Substring(0, TextIndex), f2, bMask, p);
// MaskGr.DrawString(sText.Substring(0, TextIndex), f, b2, p);
}
catch (Exception ex) { }
bmp.MakeTransparent(Color.White);
// bmp.Save(ms, ImageFormat.Bmp);
g.DrawImage(bmp, p);
// g.DrawImage(bmp, p);
//this.BackgroundImage =(Image) bmp;
//pictureBox1.Image = bmp;
sOut += "Paint \r\n";
// base.OnPaint(e);
// BmpG.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// e.Graphics.DrawString("wgscd 的说法的", f, b, p);
//this.TransparencyKey = transColor;
sOut += "Back Paint\r\n";
base.OnPaintBackground(e);
}
private void FormTest_Click(object sender, EventArgs e)
{
}
#region "拖动窗体"
private bool moveFlag = false;
private int x = 0;
private int y = 0;
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag && (e.Button == MouseButtons.Left))
this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
base.OnMouseMove(e);
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (!moveFlag && e.Clicks >= 1)
moveFlag = true;
x = e.X;
y = e.Y;
base.OnMouseDown(e);
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e);
}
#endregion
void timerMask_Tick(object sender, EventArgs e)
{
// if (sText.Trim() == "") { return; };
this.Refresh();
TextIndex++;
if (TextIndex > sText.Length)
{
TextIndex = 0;
}
}
void timerLrc_Tick(object sender, EventArgs e)
{
//LrcTime = new DateTime(2009, 10, 10, 0, 0, 0, 0);
//if (TextIndex > sText.Length)
//{
// TextIndex = 0;
//}
}
private void button2_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Trim() == "") { return; }
sText = richTextBox1.Text;
panel1.Visible = false;
timerMask.Enabled = true;
}
/// <summary>
///
///读取歌词
/// </summary>
void OpenFile(string _strFile)
{
if (!File.Exists(_strFile))
{
MessageBox.Show("File not fount");
return;
}
string sContent = "";
Stream oStream = null;
StreamReader oReader = null;
string AllLines = "";
string oneLine = "";
try
{
oStream = File.Open(_strFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
oReader = new StreamReader(oStream, Encoding.GetEncoding("gb2312"));
LrcList.Clear();
while (!oReader.EndOfStream)
{
oneLine = oReader.ReadLine() + "\r\n";
AllLines += oneLine;
regLrc(oneLine);
}
oReader.Close();
oStream.Close();
}
catch (Exception ex)
{
oReader.Close();
oStream.Close();
}
richTextBox1.AppendText(AllLines);
}
/// <summary>
/// match the lrcs all lines in to lrc list
/// </summary>
/// <param name="strInput"></param>
/// <returns></returns>
string regLrc(string strInput)
{
//string sPattner="(?<t>\\[\\d[^[]+])(?<w>.*\r\n)";
string sPattner = "(?<t>\\[\\d.*\\]+)(?<w>[^\\[]+\r\n)";
Regex reg = new Regex(sPattner);
foreach (Match mc in reg.Matches(strInput))
{
richTextBox1.AppendText(mc.Groups["t"].ToString() + "\r\n");
richTextBox1.AppendText(mc.Value + "\r\n");
LrcList.Add(new Lrc(mc.Groups["t"].ToString(), mc.Groups["w"].ToString()));
}
return "";
}
/// <summary>
/// 通过时间找歌词
/// </summary>
/// <param name="_sTime"></param>
/// <returns></returns>
string m = "";
Lrc findMatch = new Lrc();
public string FindLrc(string _sTime)
{
foreach (Lrc w in LrcList)
{
// MessageBox.Show(w.ToString());
if (w.sTime.Contains(_sTime.Substring(0, 6)))
{
if (findMatch.Equals(w)) return "";
findMatch = w;
sText = w.sText;
m += w.sText;
return w.sText;
}
}
return "";
}
private void showWord(object sender, EventArgs e)
{
//[03:26.03][02:50.67][01:16.59]冷冷的冰雨在脸上胡乱的拍
// FindLrc2("[02:50.67]");
MessageBox.Show(m);
}
DateTime d1 = new DateTime(1900, 1, 1, 0, 0, 0, 0);
DateTime d2;
DateTime d3;
TimeSpan tsp;
string tTime;
public void TimerLrc2_Tick(object sender, EventArgs e)
{
FindLrc(ClassCommon.SongCrrentTime);
return;
//if (sText.Trim() == "") { return; };
d2 = DateTime.Now;
if (d1.Year == 1900)
{
d1 = d2;
return;
}
TimerLrc2.Enabled = false;
tsp = (TimeSpan)(d2 - d1);
d3 = new DateTime(1900, 1, 1, 0, tsp.Minutes, tsp.Seconds, tsp.Milliseconds);
tTime = d3.ToString("[mm:ss.ff]");
ClassCommon.SongCrrentTime = tTime;
FindLrc(tTime);
this.Text = tTime;
TimerLrc2.Enabled = true;
}
}
}
FormLrc 界面类(FormLrc.Designer.cs):
view plaincopy to clipboardprint?
namespace MyPlayer
partial class FormLrc
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.button1.Location = new System.Drawing.Point(292, 169);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(169, 62);
this.button1.TabIndex = 1;
this.button1.Text = "添加字幕";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.richTextBox1);
this.panel1.Location = new System.Drawing.Point(110, 31);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(511, 77);
this.panel1.TabIndex = 3;
this.panel1.Visible = false;
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.button2.ForeColor = System.Drawing.SystemColors.InactiveBorder;
this.button2.Location = new System.Drawing.Point(432, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 70);
this.button2.TabIndex = 1;
this.button2.Text = "确 定";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox1
//
this.richTextBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
this.richTextBox1.Font = new System.Drawing.Font("SimHei", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.richTextBox1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.richTextBox1.Location = new System.Drawing.Point(4, 3);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(422, 70);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// FormTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(867, 318);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FormTest";
this.Text = "FormTest";
this.Load += new System.EventHandler(this.FormTest_Load);
this.Click += new System.EventHandler(this.FormTest_Click);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
}
namespace MyPlayer
{
partial class FormLrc
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.button1.Location = new System.Drawing.Point(292, 169);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(169, 62);
this.button1.TabIndex = 1;
this.button1.Text = "添加字幕";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.richTextBox1);
this.panel1.Location = new System.Drawing.Point(110, 31);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(511, 77);
this.panel1.TabIndex = 3;
this.panel1.Visible = false;
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.button2.ForeColor = System.Drawing.SystemColors.InactiveBorder;
this.button2.Location = new System.Drawing.Point(432, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 70);
this.button2.TabIndex = 1;
this.button2.Text = "确 定";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox1
//
this.richTextBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
this.richTextBox1.Font = new System.Drawing.Font("SimHei", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.richTextBox1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.richTextBox1.Location = new System.Drawing.Point(4, 3);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(422, 70);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// FormTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(867, 318);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FormTest";
this.Text = "FormTest";
this.Load += new System.EventHandler(this.FormTest_Load);
this.Click += new System.EventHandler(this.FormTest_Click);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
}
}
播放窗体类(FormPlayer.cs ):
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyPlayer
{
public partial class FormPlayer : Form
{
Song Music = new Song();
public FormPlayer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "mp3文件|*.mp3|wma文件|*.wma|wav文件|*.wav";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.InitialDirectory = "E:\\music";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
timer1.Stop();
Music.Stop();
string fName = openFileDialog.FileName;
textBox1.Text = fName;
Music.FilePath = textBox1.Text;//将文件路径赋给 播放类中的 FilePath;
Music.OpenFile();//打开播放文件,准备播放;
textBox2.Text = Music.FileName;//获取文件名;
textBox3.Text = Music.Duration.ToString();//获取文件长度;
textBox6.Text = Music.Status().ToString();//获取文件状态;
trackBar1.Value = 50;
Music.SetVolume(500);//将音量设置成 500;
textBox5.Text = Music.TheVolume().ToString();//获取音量;
timer1.Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
Music.Pause();//暂停;
textBox6.Text = Music.Status().ToString();
}
private void button4_Click(object sender, EventArgs e)
{
Music.Stop();//停止;
timer1.Enabled = false;
textBox6.Text = Music.Status().ToString();
}
private void button5_Click(object sender, EventArgs e)
{
Music.PlayHalfSpeed();//半速播放;
textBox6.Text = Music.Status().ToString();
}
private void button6_Click(object sender, EventArgs e)
{
Music.PlayDoubleSpeed();//倍速播放;
textBox6.Text = Music.Status().ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
//获取文件当前播放位置;
textBox4.Text = Convert.ToString(Music.CurrentPosition);
//textBox4.Text = Convert.ToString(Music.CurrentPosition.ToString("mm:ss:ff"));
//textBox4.Text = (Music.CurrentPosition / 1000 / 60).ToString("00") + ":" + (Music.CurrentPosition / 1000 / 60).ToString("00");
// Complete number of seconds
int s = Music.CurrentPosition;
TimeSpan t = new TimeSpan(1900,0,0,0,s);
ClassCommon.SongCrrentTime = t.Minutes.ToString("00") + ":" + t.Seconds.ToString("00") + "." + t.Milliseconds.ToString("00");
textBox4.Text = t.Minutes.ToString("00") + ":" + t.Seconds.ToString("00") + ":" + t.Milliseconds.ToString("00");
return;
// Seconds to display
int ss = s % 60;
// Complete number of minutes
int m = (s - ss) / 60;
// Minutes to display
int mm = m % 60;
// Complete number of hours
int h = (m - mm) / 60;
// Make "hh:mm:ss"
textBox4.Text = h.ToString("D2") + ":" + mm.ToString("D2") + ":" + ss.ToString("D2");
}
private void button7_Click(object sender, EventArgs e)
{
//设置指定位置开始播放;
Music.CurrentPosition = Convert.ToInt32(textBox8.Text);
}
private void button9_Click(object sender, EventArgs e)
{
//进行录音;
Music.Record(Convert.ToInt32(textBox9.Text));
}
private void button10_Click(object sender, EventArgs e)
{
Music.VolumeOn();//取消静音;
}
private void button11_Click(object sender, EventArgs e)
{
Music.VolumeOff();//静音;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
//设置音量;
Music.SetVolume(Convert.ToInt32(trackBar1.Value * 10));
textBox5.Text = Music.TheVolume().ToString();
}
private void FormPlayer_Load(object sender, EventArgs e)
{
}
FormLrc oFormLrc = new FormLrc();
private void button2_Click(object sender, EventArgs e)
{
//if(Music.FilePath){}
Music.OpenFile();
Music.Play();//播放;
// oFormLrc.LoadLrc("刘德华 - 冰雨.lrc");
oFormLrc.LoadLrc("刘德华-天意.lrc");
oFormLrc.Show();
timer1.Enabled = true;
textBox6.Text = Music.Status().ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyPlayer
{
public partial class FormPlayer : Form
{
Song Music = new Song();
public FormPlayer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "mp3文件|*.mp3|wma文件|*.wma|wav文件|*.wav";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.InitialDirectory = "E:\\music";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
timer1.Stop();
Music.Stop();
string fName = openFileDialog.FileName;
textBox1.Text = fName;
Music.FilePath = textBox1.Text;//将文件路径赋给 播放类中的 FilePath;
Music.OpenFile();//打开播放文件,准备播放;
textBox2.Text = Music.FileName;//获取文件名;
textBox3.Text = Music.Duration.ToString();//获取文件长度;
textBox6.Text = Music.Status().ToString();//获取文件状态;
trackBar1.Value = 50;
Music.SetVolume(500);//将音量设置成 500;
textBox5.Text = Music.TheVolume().ToString();//获取音量;
timer1.Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
Music.Pause();//暂停;
textBox6.Text = Music.Status().ToString();
}
private void button4_Click(object sender, EventArgs e)
{
Music.Stop();//停止;
timer1.Enabled = false;
textBox6.Text = Music.Status().ToString();
}
private void button5_Click(object sender, EventArgs e)
{
Music.PlayHalfSpeed();//半速播放;
textBox6.Text = Music.Status().ToString();
}
private void button6_Click(object sender, EventArgs e)
{
Music.PlayDoubleSpeed();//倍速播放;
textBox6.Text = Music.Status().ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
//获取文件当前播放位置;
textBox4.Text = Convert.ToString(Music.CurrentPosition);
//textBox4.Text = Convert.ToString(Music.CurrentPosition.ToString("mm:ss:ff"));
//textBox4.Text = (Music.CurrentPosition / 1000 / 60).ToString("00") + ":" + (Music.CurrentPosition / 1000 / 60).ToString("00");
// Complete number of seconds
int s = Music.CurrentPosition;
TimeSpan t = new TimeSpan(1900,0,0,0,s);
ClassCommon.SongCrrentTime = t.Minutes.ToString("00") + ":" + t.Seconds.ToString("00") + "." + t.Milliseconds.ToString("00");
textBox4.Text = t.Minutes.ToString("00") + ":" + t.Seconds.ToString("00") + ":" + t.Milliseconds.ToString("00");
return;
// Seconds to display
int ss = s % 60;
// Complete number of minutes
int m = (s - ss) / 60;
// Minutes to display
int mm = m % 60;
// Complete number of hours
int h = (m - mm) / 60;
// Make "hh:mm:ss"
textBox4.Text = h.ToString("D2") + ":" + mm.ToString("D2") + ":" + ss.ToString("D2");
}
private void button7_Click(object sender, EventArgs e)
{
//设置指定位置开始播放;
Music.CurrentPosition = Convert.ToInt32(textBox8.Text);
}
private void button9_Click(object sender, EventArgs e)
{
//进行录音;
Music.Record(Convert.ToInt32(textBox9.Text));
}
private void button10_Click(object sender, EventArgs e)
{
Music.VolumeOn();//取消静音;
}
private void button11_Click(object sender, EventArgs e)
{
Music.VolumeOff();//静音;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
//设置音量;
Music.SetVolume(Convert.ToInt32(trackBar1.Value * 10));
textBox5.Text = Music.TheVolume().ToString();
}
private void FormPlayer_Load(object sender, EventArgs e)
{
}
FormLrc oFormLrc = new FormLrc();
private void button2_Click(object sender, EventArgs e)
{
//if(Music.FilePath){}
Music.OpenFile();
Music.Play();//播放;
// oFormLrc.LoadLrc("刘德华 - 冰雨.lrc");
oFormLrc.LoadLrc("刘德华-天意.lrc");
oFormLrc.Show();
timer1.Enabled = true;
textBox6.Text = Music.Status().ToString();
}
}
}
播放窗体界面类(FormPlayer.Designer.cs):
view plaincopy to clipboardprint?
namespace MyPlayer
{
partial class FormPlayer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button7 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.textBox8 = new System.Windows.Forms.TextBox();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.label8 = new System.Windows.Forms.Label();
this.textBox9 = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.trackBar1 = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 0;
this.label1.Text = "歌曲路径:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(101, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(328, 21);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 92);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(83, 12);
this.label2.TabIndex = 2;
this.label2.Text = "歌曲长度(s):";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(101, 46);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(328, 21);
this.textBox2.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 55);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 4;
this.label3.Text = "歌曲名称:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(101, 83);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(328, 21);
this.textBox3.TabIndex = 5;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 127);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(83, 12);
this.label4.TabIndex = 6;
this.label4.Text = "当前位置(s):";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(101, 118);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(95, 21);
this.textBox4.TabIndex = 7;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(12, 159);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(65, 12);
this.label5.TabIndex = 8;
this.label5.Text = "当前音量:";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(101, 150);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(328, 21);
this.textBox5.TabIndex = 9;
//
// button1
//
this.button1.Location = new System.Drawing.Point(2, 299);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 10;
this.button1.Text = "打开文件";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(83, 299);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 11;
this.button2.Text = "播放";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(164, 299);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 12;
this.button3.Text = "暂停";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(245, 299);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 13;
this.button4.Text = "停止";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(326, 299);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 14;
this.button5.Text = "半速播放";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(407, 299);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 15;
this.button6.Text = "倍速播放";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// timer1
//
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button7
//
this.button7.Location = new System.Drawing.Point(224, 118);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(116, 23);
this.button7.TabIndex = 16;
this.button7.Text = "设置当前位置(s)";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(12, 223);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(89, 12);
this.label6.TabIndex = 17;
this.label6.Text = "文件当前状态:";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(107, 220);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(95, 21);
this.textBox6.TabIndex = 18;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(12, 188);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(65, 12);
this.label7.TabIndex = 19;
this.label7.Text = "音量调节:";
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(346, 118);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(83, 21);
this.textBox8.TabIndex = 22;
//
// button9
//
this.button9.Location = new System.Drawing.Point(245, 258);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(75, 23);
this.button9.TabIndex = 23;
this.button9.Text = "录音/播放";
this.button9.UseVisualStyleBackColor = true;
//
// button10
//
this.button10.Location = new System.Drawing.Point(2, 340);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(75, 23);
this.button10.TabIndex = 24;
this.button10.Text = "声音开";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(101, 340);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(75, 23);
this.button11.TabIndex = 25;
this.button11.Text = "声音关";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(12, 263);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(83, 12);
this.label8.TabIndex = 26;
this.label8.Text = "录音时间(s):";
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(101, 258);
this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(129, 21);
this.textBox9.TabIndex = 27;
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(222, 229);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(143, 12);
this.label9.TabIndex = 28;
this.label9.Text = "1:播放,2:暂停,3:停止";
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(101, 178);
this.trackBar1.Maximum = 100;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(328, 45);
this.trackBar1.TabIndex = 29;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// FormPlayer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(488, 364);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.label9);
this.Controls.Add(this.textBox9);
this.Controls.Add(this.label8);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.label7);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.label6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "FormPlayer";
this.Text = "实例";
this.Load += new System.EventHandler(this.FormPlayer_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TrackBar trackBar1;
}
}
namespace MyPlayer
{
partial class FormPlayer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button7 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.textBox8 = new System.Windows.Forms.TextBox();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.label8 = new System.Windows.Forms.Label();
this.textBox9 = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.trackBar1 = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 0;
this.label1.Text = "歌曲路径:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(101, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(328, 21);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 92);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(83, 12);
this.label2.TabIndex = 2;
this.label2.Text = "歌曲长度(s):";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(101, 46);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(328, 21);
this.textBox2.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 55);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 4;
this.label3.Text = "歌曲名称:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(101, 83);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(328, 21);
this.textBox3.TabIndex = 5;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 127);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(83, 12);
this.label4.TabIndex = 6;
this.label4.Text = "当前位置(s):";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(101, 118);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(95, 21);
this.textBox4.TabIndex = 7;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(12, 159);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(65, 12);
this.label5.TabIndex = 8;
this.label5.Text = "当前音量:";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(101, 150);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(328, 21);
this.textBox5.TabIndex = 9;
//
// button1
//
this.button1.Location = new System.Drawing.Point(2, 299);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 10;
this.button1.Text = "打开文件";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(83, 299);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 11;
this.button2.Text = "播放";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(164, 299);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 12;
this.button3.Text = "暂停";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(245, 299);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 13;
this.button4.Text = "停止";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(326, 299);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 14;
this.button5.Text = "半速播放";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(407, 299);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 15;
this.button6.Text = "倍速播放";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// timer1
//
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button7
//
this.button7.Location = new System.Drawing.Point(224, 118);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(116, 23);
this.button7.TabIndex = 16;
this.button7.Text = "设置当前位置(s)";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(12, 223);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(89, 12);
this.label6.TabIndex = 17;
this.label6.Text = "文件当前状态:";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(107, 220);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(95, 21);
this.textBox6.TabIndex = 18;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(12, 188);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(65, 12);
this.label7.TabIndex = 19;
this.label7.Text = "音量调节:";
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(346, 118);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(83, 21);
this.textBox8.TabIndex = 22;
//
// button9
//
this.button9.Location = new System.Drawing.Point(245, 258);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(75, 23);
this.button9.TabIndex = 23;
this.button9.Text = "录音/播放";
this.button9.UseVisualStyleBackColor = true;
//
// button10
//
this.button10.Location = new System.Drawing.Point(2, 340);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(75, 23);
this.button10.TabIndex = 24;
this.button10.Text = "声音开";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(101, 340);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(75, 23);
this.button11.TabIndex = 25;
this.button11.Text = "声音关";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(12, 263);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(83, 12);
this.label8.TabIndex = 26;
this.label8.Text = "录音时间(s):";
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(101, 258);
this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(129, 21);
this.textBox9.TabIndex = 27;
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(222, 229);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(143, 12);
this.label9.TabIndex = 28;
this.label9.Text = "1:播放,2:暂停,3:停止";
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(101, 178);
this.trackBar1.Maximum = 100;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(328, 45);
this.trackBar1.TabIndex = 29;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// FormPlayer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(488, 364);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.label9);
this.Controls.Add(this.textBox9);
this.Controls.Add(this.label8);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.label7);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.label6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "FormPlayer";
this.Text = "实例";
this.Load += new System.EventHandler(this.FormPlayer_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TrackBar trackBar1;
}
}
播放文件的类(Song.cs):
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyPlayer
{
public class Song
{
//声明调用“GetShortPathName”函数,功能:获得短文件名;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);
//声明调用“mciSendString”函数,功能:向 mci 设备发送播放等命令;
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
//声明调用 "waveOutGetVolume"函数,功能:获取系统当前音量;
//[DllImport("Winmm.dll")]
// public static extern uint waveOutGetVolume(
//int hwo,
//out System.UInt32 pdwVolume
// );
//定义在API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)] //指定在托管代码和非托管代码之间传送数据的方式;
private string ShortPathName = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string TotalLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string StrTemp = "";
//定义播放状态枚举变量
public enum StatusState
{
Playing = 1,//定义为播放状态;
Paused = 2,//定义为暂停状态;
Stopped = 3//定义为停止状态;
};
public StatusState FileState;
public string FilePath; //存储文件路径;
public string FilePathName //FilePath 的属性;
{
set
{
FilePath=value;
}
}
public Song()
{
//Song类的构造函数;
// FilePath = File;
}
public void OpenFile()//打开要播放的音频文件;
{
try
{
ShortPathName = ShortPathName.PadLeft(255, Convert.ToChar(" "));
StrTemp = StrTemp.PadLeft(127, Convert.ToChar(" "));
//获取文件短路经名;
int Retu = GetShortPathName(FilePath, ShortPathName, ShortPathName.Length );
string ShortPath = ShortPathName.ToString();
ShortPath = GetPath(ShortPath);//去掉最后一个字符;
mciSendString("close all", StrTemp, StrTemp.Length, 0);
//打开要播放文件,别名为"song";
mciSendString("open " + ShortPath + " alias song", StrTemp, StrTemp.Length , 0);
//设置文件的时间为毫秒;
mciSendString("set song time format milliseconds", StrTemp, StrTemp.Length, 0);
//将文件播放状态存入变量 FileState 中;
FileState = StatusState.Stopped;
}
catch {
MessageBox.Show("文件打开失败,可能不支持此文件的播放!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
//正常播放;
public void Play()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//设置播放速度为"1000" ;
mciSendString("set song speed 1000", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
//如果 FileState 为 StatusState.Stopped,播放状态为"停止",向MCI发送播放命令;
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
//如果 FileState 为 S StatusState.Paused,播放状态为"暂停",向MCI发送继续播放命令;
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//在指定位置播放;
public void Play(int StartTime)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
if (FileState == StatusState.Stopped)
{
//从指定位置开始播放;
mciSendString("play song from " + Convert.ToString(StartTime * 1000), StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
//执行暂停前的播放;
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//播放"StartTime" ~ "EndTime"之间的音频内容;
public void Play(int StartTime,int EndTime)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("play song from " + Convert.ToString(StartTime * 1000) + " to " + Convert.ToString(EndTime * 1000),
StrTemp, StrTemp.Length, 0);
FileState = StatusState.Playing;
}
//半速播放;
public void PlayHalfSpeed()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//这里的 "500" 为播放速度,"1000"为正常播放,大于1000为快速播放,小于1000为慢速播放;
mciSendString("set song speed 500", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//倍速播放
public void PlayDoubleSpeed()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//设置播放速度为 "2"倍,"1000"为正常播放;
mciSendString("set song speed 2000", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//停止;
public void Stop()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//关闭歌曲;
mciSendString("close song", StrTemp, 128, 0);
mciSendString("close all", StrTemp, 128, 0);
FileState = StatusState.Stopped;
}
//暂停;
public void Pause()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//向MCI发送暂停播放命令;
mciSendString("pause song", StrTemp, StrTemp.Length, 0);
FileState = StatusState.Paused;
}
//获取总时间;
public int Duration
{
get
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
//向MCI发送获取文件长度的命令;
mciSendString("status song length", TotalLength, TotalLength.Length, 0);
TotalLength = TotalLength.Trim();//去除多余空格;
if (TotalLength == "")
{
return 0;
}
else
{
return (int)(Convert.ToDouble(TotalLength) / 1000);
}
}
}
//获取/设置当前位置;
public int CurrentPosition
{ //获取文件的当前播放位置;
get
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status song position", TotalLength, TotalLength.Length, 0);
int ireturn=0;
try
{
if (TotalLength == "" || TotalLength.Trim() == "\\0") return 0;
return (int)(Convert.ToDouble(TotalLength.Trim()));
}
catch(Exception ex){
}
return ireturn ;
}
//设置文件播放的当前位置;
set
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("seek song to " + Convert.ToString(value * 1000) , TotalLength, TotalLength.Length, 0);
//如果文件处于播放状态,则到指定位置后继续播放;
if (FileState == StatusState.Playing)
{
mciSendString("play song", TotalLength, TotalLength.Length, 0);
}
}
}
//获取文件名;
public string FileName
{
get
{
int StrIndex = FilePath.LastIndexOf("\\");//查找最后一个字符串"\"的位置;
int PointIndex = FilePath.LastIndexOf(".");//查找最后一个字符串"."的位置;
string ReturnFileName = FilePath.Substring((StrIndex + 1), (PointIndex - StrIndex - 1));
return ReturnFileName;
}
}
//获取文件当前状态;
public int Status()
{
return (int)FileState;
}
//录音;
public void Record(int Time)
{
try
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//创建音频文件
mciSendString("open new type waveaudio alias song", StrTemp, StrTemp.Length, 0);
//设置时间格式为毫秒,采样率为8;
mciSendString("set song time format milliseconds bitspersample 8 samplespersec 11025", StrTemp, 1024, 0);
//设置为立体声播放;
mciSendString("set song channels 2", StrTemp, StrTemp.Length, 0);
//向MCI发送录音命令;
mciSendString("record song to " + Convert.ToString(Time * 1000), StrTemp, StrTemp.Length, 0);
int StartTime = System.Environment.TickCount; //获取系统启动后经历的毫秒数;
//录音开始后,在规定的时间内播放;
while (true)
{
/*用当前获取的系统启动后经历的毫秒数 减去 "StartTime" 的差和 (Time * 1000) 进行比较,
*如果前者大于后者,说明自录音开始后经历了Time(S);
*/
if (System.Environment.TickCount - StartTime > (Time * 1000))
{
mciSendString("stop song", StrTemp, StrTemp.Length, 0);
mciSendString("play song from 1", StrTemp, StrTemp.Length, 0);
break;
}
Application.DoEvents(); //为了不影响系统执行其他程序,这里将控制权提交给操作系统;
}
}
catch {
MessageBox.Show("在录音操作时,产生一个错误!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
//播放录音;
public void Recordplay()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("stop song", StrTemp, StrTemp.Length, 0);
mciSendString("play song from 1", StrTemp, StrTemp.Length, 0);
}
//获取当前音量;
public int TheVolume()
{
/*uint VolumeSize;
* int Flag = 0;
* uint i = waveOutGetVolume(Flag, out VolumeSize);
* return (int)(VolumeSize & 0xFFFF);//右声道的音量;
*/
/*你要求中写的是获取(系统)音量,上面注释部分代码就是获取(系统)
* 音量的,不过我只写了获取右声道的,它和控制音频音量不是一个概念,
* 音频音量的最大值为"1000",如果你要控制音频的音量,下面代码更好一些;
* 如果你要获取的是系统的音量就用上面的代码,并把上半部分调用的
* "waveOutGetVolume" 的声明部分注释去掉;
*/
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("status song volume", StrTemp, StrTemp.Length, 0);
string SongVolume = StrTemp.Trim();
return Convert.ToInt32(SongVolume);
}
//设置音量;
public void SetVolume(int volume)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
int SoundVolume = volume;
if (SoundVolume > 1000) SoundVolume = 1000;//音量最大值为1000;
if (SoundVolume <0) SoundVolume = 0;//音量最小值为0;
//设置音量;
mciSendString("setaudio song volume to " + Convert.ToString(SoundVolume), StrTemp, StrTemp.Length, 0);
}
//声音关;
public void VolumeOff()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//关闭声音;
mciSendString("setaudio song off", StrTemp, StrTemp.Length, 0);
}
//声开;
public void VolumeOn()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
// 打开声音;
mciSendString("setaudio song on", StrTemp, StrTemp.Length, 0);
}
private string GetPath(string FileNameTemp)
{
if (FileNameTemp.Length < 1) return "";
FileNameTemp = FileNameTemp.Trim();//去除两边的空格;
FileNameTemp = FileNameTemp.Substring(0, FileNameTemp.Length - 1);//去除最后一个字符;
return FileNameTemp;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyPlayer
{
public class Song
{
//声明调用“GetShortPathName”函数,功能:获得短文件名;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);
//声明调用“mciSendString”函数,功能:向 mci 设备发送播放等命令;
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
//声明调用 "waveOutGetVolume"函数,功能:获取系统当前音量;
//[DllImport("Winmm.dll")]
// public static extern uint waveOutGetVolume(
//int hwo,
//out System.UInt32 pdwVolume
// );
//定义在API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)] //指定在托管代码和非托管代码之间传送数据的方式;
private string ShortPathName = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string TotalLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string StrTemp = "";
//定义播放状态枚举变量
public enum StatusState
{
Playing = 1,//定义为播放状态;
Paused = 2,//定义为暂停状态;
Stopped = 3//定义为停止状态;
};
public StatusState FileState;
public string FilePath; //存储文件路径;
public string FilePathName //FilePath 的属性;
{
set
{
FilePath=value;
}
}
public Song()
{
//Song类的构造函数;
// FilePath = File;
}
public void OpenFile()//打开要播放的音频文件;
{
try
{
ShortPathName = ShortPathName.PadLeft(255, Convert.ToChar(" "));
StrTemp = StrTemp.PadLeft(127, Convert.ToChar(" "));
//获取文件短路经名;
int Retu = GetShortPathName(FilePath, ShortPathName, ShortPathName.Length );
string ShortPath = ShortPathName.ToString();
ShortPath = GetPath(ShortPath);//去掉最后一个字符;
mciSendString("close all", StrTemp, StrTemp.Length, 0);
//打开要播放文件,别名为"song";
mciSendString("open " + ShortPath + " alias song", StrTemp, StrTemp.Length , 0);
//设置文件的时间为毫秒;
mciSendString("set song time format milliseconds", StrTemp, StrTemp.Length, 0);
//将文件播放状态存入变量 FileState 中;
FileState = StatusState.Stopped;
}
catch {
MessageBox.Show("文件打开失败,可能不支持此文件的播放!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
//正常播放;
public void Play()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//设置播放速度为"1000" ;
mciSendString("set song speed 1000", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
//如果 FileState 为 StatusState.Stopped,播放状态为"停止",向MCI发送播放命令;
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
//如果 FileState 为 S StatusState.Paused,播放状态为"暂停",向MCI发送继续播放命令;
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//在指定位置播放;
public void Play(int StartTime)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
if (FileState == StatusState.Stopped)
{
//从指定位置开始播放;
mciSendString("play song from " + Convert.ToString(StartTime * 1000), StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
//执行暂停前的播放;
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//播放"StartTime" ~ "EndTime"之间的音频内容;
public void Play(int StartTime,int EndTime)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("play song from " + Convert.ToString(StartTime * 1000) + " to " + Convert.ToString(EndTime * 1000),
StrTemp, StrTemp.Length, 0);
FileState = StatusState.Playing;
}
//半速播放;
public void PlayHalfSpeed()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//这里的 "500" 为播放速度,"1000"为正常播放,大于1000为快速播放,小于1000为慢速播放;
mciSendString("set song speed 500", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//倍速播放
public void PlayDoubleSpeed()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//设置播放速度为 "2"倍,"1000"为正常播放;
mciSendString("set song speed 2000", StrTemp, StrTemp.Length, 0);
if (FileState == StatusState.Stopped)
{
mciSendString("play song", StrTemp, StrTemp.Length, 0);
}
else if (FileState == StatusState.Paused)
{
mciSendString("resume song", StrTemp, StrTemp.Length, 0);
}
FileState = StatusState.Playing;
}
//停止;
public void Stop()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//关闭歌曲;
mciSendString("close song", StrTemp, 128, 0);
mciSendString("close all", StrTemp, 128, 0);
FileState = StatusState.Stopped;
}
//暂停;
public void Pause()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//向MCI发送暂停播放命令;
mciSendString("pause song", StrTemp, StrTemp.Length, 0);
FileState = StatusState.Paused;
}
//获取总时间;
public int Duration
{
get
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
//向MCI发送获取文件长度的命令;
mciSendString("status song length", TotalLength, TotalLength.Length, 0);
TotalLength = TotalLength.Trim();//去除多余空格;
if (TotalLength == "")
{
return 0;
}
else
{
return (int)(Convert.ToDouble(TotalLength) / 1000);
}
}
}
//获取/设置当前位置;
public int CurrentPosition
{ //获取文件的当前播放位置;
get
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status song position", TotalLength, TotalLength.Length, 0);
int ireturn=0;
try
{
if (TotalLength == "" || TotalLength.Trim() == "\\0") return 0;
return (int)(Convert.ToDouble(TotalLength.Trim()));
}
catch(Exception ex){
}
return ireturn ;
}
//设置文件播放的当前位置;
set
{
TotalLength = "";
TotalLength = TotalLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("seek song to " + Convert.ToString(value * 1000) , TotalLength, TotalLength.Length, 0);
//如果文件处于播放状态,则到指定位置后继续播放;
if (FileState == StatusState.Playing)
{
mciSendString("play song", TotalLength, TotalLength.Length, 0);
}
}
}
//获取文件名;
public string FileName
{
get
{
int StrIndex = FilePath.LastIndexOf("\\");//查找最后一个字符串"\"的位置;
int PointIndex = FilePath.LastIndexOf(".");//查找最后一个字符串"."的位置;
string ReturnFileName = FilePath.Substring((StrIndex + 1), (PointIndex - StrIndex - 1));
return ReturnFileName;
}
}
//获取文件当前状态;
public int Status()
{
return (int)FileState;
}
//录音;
public void Record(int Time)
{
try
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//创建音频文件
mciSendString("open new type waveaudio alias song", StrTemp, StrTemp.Length, 0);
//设置时间格式为毫秒,采样率为8;
mciSendString("set song time format milliseconds bitspersample 8 samplespersec 11025", StrTemp, 1024, 0);
//设置为立体声播放;
mciSendString("set song channels 2", StrTemp, StrTemp.Length, 0);
//向MCI发送录音命令;
mciSendString("record song to " + Convert.ToString(Time * 1000), StrTemp, StrTemp.Length, 0);
int StartTime = System.Environment.TickCount; //获取系统启动后经历的毫秒数;
//录音开始后,在规定的时间内播放;
while (true)
{
/*用当前获取的系统启动后经历的毫秒数 减去 "StartTime" 的差和 (Time * 1000) 进行比较,
*如果前者大于后者,说明自录音开始后经历了Time(S);
*/
if (System.Environment.TickCount - StartTime > (Time * 1000))
{
mciSendString("stop song", StrTemp, StrTemp.Length, 0);
mciSendString("play song from 1", StrTemp, StrTemp.Length, 0);
break;
}
Application.DoEvents(); //为了不影响系统执行其他程序,这里将控制权提交给操作系统;
}
}
catch {
MessageBox.Show("在录音操作时,产生一个错误!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
//播放录音;
public void Recordplay()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("stop song", StrTemp, StrTemp.Length, 0);
mciSendString("play song from 1", StrTemp, StrTemp.Length, 0);
}
//获取当前音量;
public int TheVolume()
{
/*uint VolumeSize;
* int Flag = 0;
* uint i = waveOutGetVolume(Flag, out VolumeSize);
* return (int)(VolumeSize & 0xFFFF);//右声道的音量;
*/
/*你要求中写的是获取(系统)音量,上面注释部分代码就是获取(系统)
* 音量的,不过我只写了获取右声道的,它和控制音频音量不是一个概念,
* 音频音量的最大值为"1000",如果你要控制音频的音量,下面代码更好一些;
* 如果你要获取的是系统的音量就用上面的代码,并把上半部分调用的
* "waveOutGetVolume" 的声明部分注释去掉;
*/
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
mciSendString("status song volume", StrTemp, StrTemp.Length, 0);
string SongVolume = StrTemp.Trim();
return Convert.ToInt32(SongVolume);
}
//设置音量;
public void SetVolume(int volume)
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
int SoundVolume = volume;
if (SoundVolume > 1000) SoundVolume = 1000;//音量最大值为1000;
if (SoundVolume <0) SoundVolume = 0;//音量最小值为0;
//设置音量;
mciSendString("setaudio song volume to " + Convert.ToString(SoundVolume), StrTemp, StrTemp.Length, 0);
}
//声音关;
public void VolumeOff()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
//关闭声音;
mciSendString("setaudio song off", StrTemp, StrTemp.Length, 0);
}
//声开;
public void VolumeOn()
{
StrTemp = "";
StrTemp = StrTemp.PadLeft(128, Convert.ToChar(" "));
// 打开声音;
mciSendString("setaudio song on", StrTemp, StrTemp.Length, 0);
}
private string GetPath(string FileNameTemp)
{
if (FileNameTemp.Length < 1) return "";
FileNameTemp = FileNameTemp.Trim();//去除两边的空格;
FileNameTemp = FileNameTemp.Substring(0, FileNameTemp.Length - 1);//去除最后一个字符;
return FileNameTemp;
}
}
}
公共类(ClassCommon.cs):
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace MyPlayer
{
public class ClassCommon
{
public static string SongCrrentTime="00:00.00";
}
}