播放背景音乐

上文来自: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. Go中处理文本格式

    首先是xml 解析xml package main import ( "encoding/xml" //xml标准库 "fmt" "io/ioutil ...

  2. 【转载++】C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义

    本文是上一篇“fopen返回0(空指针NULL)且GetLastError是0”的侧面回应.听赶来多么地正确和不容置疑,返回NULL时调用GetLastError来看看报错啊,但当时却返回了0,大家都 ...

  3. [NOIP2017]逛公园(DP)

    先spfa一遍处理出d[]数组,(从n开始bfs一遍标记可以达到n的点) 题意即,在走最短路的基础上,可以最多多走K长度的路径, 考虑DP,每次剩余可走的长度会因决策而改变,所以考虑dp[i][j]为 ...

  4. JSON解析工具——fastjson的简单使用

    从官方文档入手: 常见问题与快速上手:https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 各种使 ...

  5. Hibernate-关系映射

    1.为什么用Hibernate框架: java程序数据保存的变化: * 内存存在:java基础中, 数据保存在内存中,只在内存中暂时存在 * 文件保存:有io/流之后,数据可以保存到文件中 * 数据库 ...

  6. 北京Uber优步司机奖励政策(2月25日)

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

  7. 成都Uber优步司机奖励政策(3月21日)

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

  8. 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  9. Linux命令应用大词典-第22章 GRUB

    22.1 grub-md5-crypt:使用MD5格式加密口令 22.2 grub-install:在设备上安装GRUB 22.3 grub:进入GRUB命令shell 22.4 grub-crypt ...

  10. SpringMVC+mybatis+maven+Ehcache缓存实现

    所谓缓存,就是将程序或系统经常要调用的对象存在内存中,以便其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文 ...