播放MP3
播放背景音乐

上文来自: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的更多相关文章
- 读取SD卡文件夹下的MP3文件和播放MP3文件
首先获取SD卡path路径下的所有的MP3文件,并将文件名和文件大小存入List数组(此代码定义在FileUtils类中): /** * 读取目录中的Mp3文件的名字和大小 */ public Lis ...
- 遭遇flash播放mp3诡异问题
在部分ie10+flash player 播放mp3,播放第二句话时,中断无法正常播放,(客户的机器上),自己公司的机器测试了几个,都没发现这个问题.其它浏览器(chrome,firefox)也没发现 ...
- Android命令行播放MP3音乐
/*************************************************************************** * Android命令行播放MP3音乐 * 说 ...
- ios开发——实用技术篇Swift篇&播放MP3
播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...
- 在C语言控制台程序中播放MP3音乐
游戏没有声音多单调. 这里做一个简单的范例,用 mciSendString 函数播放 MP3 格式的音乐,先看看代码吧: // 编译该范例前,请把 background.mp3 放在项目文件夹中 // ...
- ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题
1.ubuntu 13.10 Rhythmbox不能播放mp3的解决方法 软件中心搜索(ubuntu额外的版权受限软件)不带括号 2.中文乱码问题解决方法: 终端顺序操作 : 1. sudo ged ...
- Ionic2 播放mp3功能实现
在开发app的过程中有需要播放mp3的功能,一直想实现,但苦于具体的困难一直未能实现,经过一段时间的资料查询和测试,最终摸索出来,现记录如下: 1.最重要的是安装第三方插件ionic-audio,开源 ...
- VC播放mp3的方法
1.使用msi库 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") ....... //打开文件 MCI_O ...
- IOS使用AVAudioPlayer播放mp3歌曲文件并监听来电打断
本实例实现了AVAudioPlayer播放mp3歌曲文件,实现了播放.暂停.继续操作,音乐音量控制.播放进度显示,同时监听来电打断事件 一.控件初始化 - (void)viewDidLoad { [s ...
- 使用openal与mpg123播放MP3,附带工程文件(转)
使用openal与mpg123播放MP3,附带工程文件 使用openal和mpg123播放MP3文件 使用静态编译,相关文件都在附件里 相关工程文件:openal_mpg123_player.7z 使 ...
随机推荐
- django_orm 基本操作
单表操作 增的操作: 一种方式:表名.objects.create(name='xxoo') 第二种方式:表名(name='xxoo') obj=表名(name='xxoo') obj.save() ...
- Python(9-18天总结)
day9:函数:def (形参): 函数体 函数名(实参)形参:在函数声明位置的变量 1. 位置参数 2. 默认值参数 3. 混合 位置, 默认值 4. 动态传参, *args 动态接收位置参数, * ...
- python教程(二)·条件语句
条件语句一般用来判断给定的条件是否成立,根据结果来执行不同的代码,也就是说,有了条件语句,才可以根据不同的情况做不同的事,从而控制程序的流程. 布尔类型 前面说到数据类型的时候,其中有一种叫 &quo ...
- C程序设计语言笔记-第一章
The C Programming language notes 一 基础变量类型.运算符和判断循环 char 字符型 character ...
- 从零开始一个http服务器(三)-返回response 构造
从零开始一个http服务器(三) 代码地址 : https://github.com/flamedancer/cserver git checkout step3 运行: gcc request.h ...
- python2.7入门---CGI编程&文件上传&文件下载
这次我们来看下文件下载和上传的操作.首先是上传,HTML设置上传文件的表单需要设置 enctype 属性为 multipart/form-data,代码如下所示: <!DOCTYPE h ...
- netty之编解码
1.netty的编码和解码,在数据传输的时候,考虑数据安全,数据完整性都是很有必要的.这里主要是介绍netty3和netty5的编解码方式.其实从StringEncoder和StringDecoder ...
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- VIM第七版
ZZ:退出并保存 e!:退回到上次保存时的样子 cw:修改单词(自动进入插入模式) cc:修改一整行的内容 cs:修改一个词(自动进入插入模式) .:可以重复上一个命令 J:将下一行内容合并到本行末尾 ...
- Python 列表下标操作
Python 列表下标操作 引用网址: https://www.jianshu.com/p/a98e935e4d46