播放背景音乐

上文来自:http://blog.csdn.net/henulwj/article/details/8977738
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Media;
namespace ListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//为了在两个方法中都能访问到
List<string> list = new List<string>();
string[] pathMusic = Directory.GetFiles("E:\00", "*.mp3");
private void Form1_Load(object sender, EventArgs e)
{
String[] path = Directory.GetFiles("E:\00", "*.jpg");
for ( int i = 0; i < path.Length; i++)
{
//根据路径名获取文件名称
string fileName = Path.GetFileName(path[i]);
listBox1.Items.Add(fileName);
//将图片全路径添加到List泛型中;
list.Add(path[i]);
}
for(int i = 0; i < pathMusic.Length; i++)
{
string fileName = Path.GetFileName(pathMusic[i]);
listBox1.Items.Add(fileName);
list.Add(pathMusic[i]);
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//添加图片文件,需要添加全路径
if (listBox1.SelectedItem.ToString().Contains(".jpg"))
{
pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
}
else if (listBox1.SelectedItem.ToString().Contains(".mp3"))
{
//SoundPlayer sp = new SoundPlayer();
//sp.SoundLocation = list[listBox1.SelectedIndex];
//sp.Play();
axWindowsMediaPlayer1.URL = list[listBox1.SelectedIndex];
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void listBox1_Click(object sender, EventArgs e)
{
if(listBox1.SelectedItem.ToString().Contains(".jpg"))
{
pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
}
else if (listBox1.SelectedItem.ToString().Contains(".mp3"))
{
//SoundPlayer sp = new SoundPlayer();
//sp.SoundLocation = list[listBox1.SelectedIndex];
//sp.Play();
//只能播放wmv格式
//可播放MP3
axWindowsMediaPlayer1.URL = list[listBox1.SelectedIndex];
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
}
下面示例提供播放音乐的两种方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;//使用Path
using System.Diagnostics;//使用进程
namespace 播放音乐
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请打开音乐";
ofd.InitialDirectory = @"E: 0";
ofd.Multiselect = true;
ofd.Filter = "音乐文件|*.mp3|所有文件|*.*";
ofd.ShowDialog();
//获得在文件夹中所有文件的全路径
string[] path = ofd.FileNames;
for(int i = 0; i < path.Length; i++)
{
listBox1.Items.Add(Path.GetFileName(path[i]));
musicPath.Add(path[i]);
}
}
List<string> musicPath = new List<string>();
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//使用Window Media Player播放音乐
axWindowsMediaPlayer2.URL = musicPath[listBox1.SelectedIndex];
axWindowsMediaPlayer2.Ctlcontrols.play();
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
{
listBox1.SelectedIndex = 0;
}
else
{
listBox1.SelectedIndex += 1;
axWindowsMediaPlayer2.URL = musicPath[listBox1.SelectedIndex];
axWindowsMediaPlayer2.Ctlcontrols.play();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
listBox1.SelectedIndex = listBox1.Items.Count -1;
}
else
{
listBox1.SelectedIndex -= 1;
axWindowsMediaPlayer2.URL = musicPath[listBox1.SelectedIndex];
axWindowsMediaPlayer2.Ctlcontrols.play();
}
}
private void listBox1_Click(object sender, EventArgs e)
{
//播放音乐的另一种方法
//直接使用默认播放器打开音乐文件
ProcessStartInfo psi = new ProcessStartInfo(musicPath[listBox1.SelectedIndex]);
Process ps = new Process();
ps.StartInfo = psi;
ps.Start();
}
}
}