C#中如果播放 音频流数据(byte[])
音频流数据来的byte[],如何播放(没压缩也没干什么的,原始格式)
必须用WIN,API吗!如果是,用哪个API?
[解决办法]
system.media
可以用mediaplayer控件,
不一定要api吧,
还要看是什么格式的音频流。
[解决办法]
Interop.MediaPlayer.dll
你要是C#.net来写的话,是不需要通过API调用,只需要应用一写这个Dll就可以了.
我写的一个Demo 部分源码
private void button1_Click(object sender, System.EventArgs e)
{//浏览MP3文件
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.listView1.Items.Clear();
string []FileNames=this.openFileDialog1.FileNames;
foreach(string FileName in FileNames)
{
//取得文件大小
FileInfo MyFileInfo=new FileInfo(FileName);
float MyFileSize=(float)MyFileInfo.Length/(1024*1024);
this.axMediaPlayer1.FileName=FileName;
//取得作者信息
string MyAuthor=this.axMediaPlayer1.GetMediaInfoString(MediaPlayer.MPMediaInfoType.mpClipAuthor);
//取得不含路径的文件名
string MyShortFileName=FileName.Substring(FileName.LastIndexOf("\\")+1);
MyShortFileName=MyShortFileName.Substring(0,MyShortFileName.Length-4);
//填充歌曲列表
string[] SubItem={MyShortFileName,MyAuthor,MyFileSize.ToString().Substring(0,4)+"M",FileName};
ListViewItem Item=new ListViewItem(SubItem);
this.listView1.Items.Add(Item);
this.listView1.Items[0].Selected=true;
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{//播放MP3文件
if(this.listView1.Items.Count>0)
{
if(this.listView1.SelectedItems.Count>0)
{
int iPos=this.listView1.SelectedItems[0].Index;
string FileName=this.listView1.Items[iPos].SubItems[3].Text;
this.axMediaPlayer1.FileName=FileName;
this.axMediaPlayer1.Play();
}
}
else
{
MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button4_Click(object sender, System.EventArgs e)
{//暂停播放
if(this.axMediaPlayer1.FileName.Length>0)
this.axMediaPlayer1.Pause();
else
{
MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button5_Click(object sender, System.EventArgs e)
{//停止播放
if(this.axMediaPlayer1.FileName.Length>0)
this.axMediaPlayer1.Stop();
else
{
MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button6_Click(object sender, System.EventArgs e)
{//上一首歌曲
if(this.listView1.Items.Count>0)
{
if(this.listView1.SelectedItems.Count>0)
{
int iPos=this.listView1.SelectedItems[0].Index;
if(iPos>0)
{
this.listView1.Items[iPos-1].Selected=true;
string FileName=this.listView1.Items[iPos-1].SubItems[3].Text;
this.axMediaPlayer1.FileName=FileName;
this.axMediaPlayer1.Play();
}
else
{
MessageBox.Show("已经是第一首歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button7_Click(object sender, System.EventArgs e)
{//下一首歌曲
if(this.listView1.Items.Count>0)
{
if(this.listView1.SelectedItems.Count>0)
{
int iPos=this.listView1.SelectedItems[0].Index;
if(iPos<this.listView1.Items.Count-1)
{
this.listView1.Items[iPos+1].Selected=true;
string FileName=this.listView1.Items[iPos+1].SubItems[3].Text;
this.axMediaPlayer1.FileName=FileName;
this.axMediaPlayer1.Play();
}
else
{
MessageBox.Show("已经是最后一首歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
[解决办法]
使用SoundPlayer 类控制 .wav 文件中的声音播放。
SoundPlayer 类提供了加载和播放 .wav 文件的简单界面。SoundPlayer 类支持从文件路径、URL、包含 .wav 文件的 Stream 或包含 .wav 文件的嵌入资源中加载 .wav 文件。
要使用 SoundPlayer 类播放声音,请用 .wav 文件的路径配置 SoundPlayer 并调用某个播放方法。可以使用某个构造函数或通过设置 SoundLocation 或 Stream 属性来标识要播放的 .wav 文件。可以在播放前使用某个加载方法加载文件,或者将加载推迟到调用某个播放方法时。被配置为从 Stream 或 URL 中加载 .wav 文件的 SoundPlayer 必须在播放开始前将 .wav 文件加载到内存中。
可以同步或异步地加载或播放 .wav 文件。如果调用同步加载或播放方法,调用线程将一直等到方法返回,这可能会导致绘制和其他事件中断。调用异步加载或播放方法则允许调用线程继续执行,而不会中断。有关异步方法调用的更多信息,请参见如何:在后台运行操作。
当 SoundPlayer 加载完 .wav 文件后,它会引发 LoadCompleted 事件。可以检查事件处理程序中的 AsyncCompletedEventArgs,确定加载是成功还是失败。当音频源设置为新文件路径或 URL 时,引发 SoundLocationChanged 事件。当音频源设置为新 Stream 时,引发 StreamChanged 事件。有关处理事件的更多信息,请参见使用事件。
使用 SoundPlayer 类可以很容易地在应用程序中包含声音。
SoundPlayer 类可以播放来自资源或者来自 UNC 或 HTTP 位置的 .wav 格式的声音文件。此外,SoundPlayer 类使您能够异步加载或播放声音。
您也可以使用 SystemSounds 类播放常见的系统声音,包括警告声
[解决办法]
不懂
[解决办法]
SoundPlayer有个Stream属性,把你的流写到stream里,就可以了
[解决办法]
- C# code
using System.Media;//...void Play(byte[] buf){ MemoryStream ms = new MemoryStream(buf); SoundPlayer sp = new SoundPlayer(ms); sp.Play();}
[解决办法]
mark!