播放背景音乐

上文来自: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:\00";
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();
}
}
}

  

播放MP3的更多相关文章

  1. 读取SD卡文件夹下的MP3文件和播放MP3文件

    首先获取SD卡path路径下的所有的MP3文件,并将文件名和文件大小存入List数组(此代码定义在FileUtils类中): /** * 读取目录中的Mp3文件的名字和大小 */ public Lis ...

  2. 遭遇flash播放mp3诡异问题

    在部分ie10+flash player 播放mp3,播放第二句话时,中断无法正常播放,(客户的机器上),自己公司的机器测试了几个,都没发现这个问题.其它浏览器(chrome,firefox)也没发现 ...

  3. Android命令行播放MP3音乐

    /*************************************************************************** * Android命令行播放MP3音乐 * 说 ...

  4. ios开发——实用技术篇Swift篇&播放MP3

    播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...

  5. 在C语言控制台程序中播放MP3音乐

    游戏没有声音多单调. 这里做一个简单的范例,用 mciSendString 函数播放 MP3 格式的音乐,先看看代码吧: // 编译该范例前,请把 background.mp3 放在项目文件夹中 // ...

  6. ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题

    1.ubuntu 13.10 Rhythmbox不能播放mp3的解决方法 软件中心搜索(ubuntu额外的版权受限软件)不带括号 2.中文乱码问题解决方法: 终端顺序操作 : 1.  sudo ged ...

  7. Ionic2 播放mp3功能实现

    在开发app的过程中有需要播放mp3的功能,一直想实现,但苦于具体的困难一直未能实现,经过一段时间的资料查询和测试,最终摸索出来,现记录如下: 1.最重要的是安装第三方插件ionic-audio,开源 ...

  8. VC播放mp3的方法

    1.使用msi库 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") ....... //打开文件 MCI_O ...

  9. IOS使用AVAudioPlayer播放mp3歌曲文件并监听来电打断

    本实例实现了AVAudioPlayer播放mp3歌曲文件,实现了播放.暂停.继续操作,音乐音量控制.播放进度显示,同时监听来电打断事件 一.控件初始化 - (void)viewDidLoad { [s ...

  10. 使用openal与mpg123播放MP3,附带工程文件(转)

    使用openal与mpg123播放MP3,附带工程文件 使用openal和mpg123播放MP3文件 使用静态编译,相关文件都在附件里 相关工程文件:openal_mpg123_player.7z 使 ...

随机推荐

  1. python基础 抽象类(接口类)

    Python中没有接口.接口类,抽象类:定义 制定一个规范 #必须要导入from abc import ABCMeta,abstractmethod class Payment(metaclass = ...

  2. A1050

    输入两个字符串,将第一个字符串中包含的第二个字符串的字符去掉(包括空格),然后输出. gets()不能用了,我混搭了string和length(),不用纠结长度还是很好的. 第二个字符串所在HashT ...

  3. 小程序开发-10-新版Music组件、组件通信与wxss样式复用

    加入缓存提升用户体验 思路:先从缓存中寻找数据或者从服务器中获取数据写入缓存中 优点:减少网络访问次数,提升用户体验 解决缓存带来的问题 问题:比如原先是不喜欢的在点击喜欢的时候,跳到下一期刊后返回来 ...

  4. Milking Order

    Milking Order 题意:给出m个描述状态,其中包含若干个边的关系,问最多能取x (x<=m)个状态,使得形成的图没有环.就是说取x个状态,用状态中的关系建边,其中不能有环. 题解:最大 ...

  5. 北京Uber优步司机奖励政策(4月1日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. fastDFS 分布式文件系统应用

    环境准备 使用的系统软件 名称 说明 centos 7.x libfatscommon FastDFS分离出的一些公用函数包 FastDFS FastDFS本体 fastdfs-nginx-modul ...

  7. Python Road

    引子 雁离群兮不知所归,路遥远兮吾将何往   Python Road[第一篇]:Python简介 Python Road[第二篇]:Python基本数据类型 Python Road[第三篇]:Pyth ...

  8. Soliworks 2016建模细节总结(1)

    Soliworks 2016建模小细节总结(1) 1.Solidworks 2016三维建模的时候,如果想要在一个视图里面呈现出四个视图(包括三个独立的视图以及三维结构的实体模型图),可以直接按鼠标会 ...

  9. Siki_Unity_2-10_数据结构与算法

    Unity 2-10 数据结构与算法 任务1-1:数据结构简介 数据结构:数据存储的结构,数据之间的关系 数据结构分类: 集合:同属于一个集合 线性结构:数据元素存在一对一的关系 树形结构:数据元素存 ...

  10. 前端开发工程师 - 01.页面制作 - 第4章.CSS

    第4章.CSS CSS简介 Cascading Style Sheet 层叠样式表:定义页面中的表现样式 history: CSS1(1996)--CSS2(1998)--着手CSS3草案(拆分成很多 ...