怎样用C#实现一边播放一边录制

pxboy 2008-11-29 07:45:24
我用AxWMPLib.AxWindowsMediaPlayer插件可以实现播放,但是我想在播放的同时录制正在播放的流媒体,请问要如何做呢?不用这个插件实现也可以的,请高手关照关照。
...全文
845 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
pxboy 2008-12-11
  • 打赏
  • 举报
回复
许多东西要涉及,directshow, wmencoder, wmformat等
pxboy 2008-11-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ztwz 的回复:]
第一段最后两个}}放在第二段最后,代码也是网上找的,不过测试过了,可以用。
[/Quote]

非常感谢,看起来是对音频设备的捕获,但是我想对正在播放的如wmv流媒体做录制;我觉得原理与这个差不多,需要拿到正在录制的流媒体的缓存流,现在还是知道怎么去获得这个缓存流,还有这种方式不知道能否按指定的起始与结束时间点进行存盘,这个拿缓存流的方式目前还是不知道呢, 我要找个写缓存的视频播放程序看看
pxboy 2008-11-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chagel 的回复:]
利用c#版本的LiquidVideo,一个完整的播放器实现
这里有源码:
http://www.codeproject.com/KB/audio-video/LiquidVideo.aspx
[/Quote]

谢谢,下载了,还在下载directX SDK,不知道有没有录制功能
ztwz 2008-11-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 pxboy 的回复:]
引用 9 楼 ztwz 的回复:
第一段最后两个}}放在第二段最后,代码也是网上找的,不过测试过了,可以用。


非常感谢,看起来是对音频设备的捕获,但是我想对正在播放的如wmv流媒体做录制;我觉得原理与这个差不多,需要拿到正在录制的流媒体的缓存流,现在还是知道怎么去获得这个缓存流,还有这种方式不知道能否按指定的起始与结束时间点进行存盘,这个拿缓存流的方式目前还是不知道呢, 我要找个写缓存的视频播放程序看看
[/Quote]
我也想知道怎么只捕获一个播放设备的声音,按指定时间录音和结束很简单,用个timer就搞定了。用上面程序录出来的声音很小声,把播放声音开到最大录,最多只有原声一半天,不知道要怎么搞搞。
pxboy 2008-11-30
  • 打赏
  • 举报
回复
up
zlb789 2008-11-30
  • 打赏
  • 举报
回复
学习下
xupeihuagudulei 2008-11-30
  • 打赏
  • 举报
回复
up
wangping_li 2008-11-30
  • 打赏
  • 举报
回复
没弄过,不过很想知道,呵呵
up
pxboy 2008-11-30
  • 打赏
  • 举报
回复
录音应该不难,视频流我觉得比较复杂了,首先一般视频文件格式有多种,播放时应该不用同的解码器来播放的,即使拿到缓存流,保存成什么格式也是个问题,肯定不是固定的,播放的什么格式就是什么格式,如果从时间点上的截,文件头信息是没有的,所以格式也是未知的,这些感觉都比较麻烦,或者是我想的太复杂了?
ztwz 2008-11-29
  • 打赏
  • 举报
回复
第一段最后两个}}放在第二段最后,代码也是网上找的,不过测试过了,可以用。
ztwz 2008-11-29
  • 打赏
  • 举报
回复
//接上面。


/// <summary>
/// 创建录音格式,此处使用16bit,16KHz,Mono的录音格式
/// </summary>
/// <returns>WaveFormat结构体</returns>
private WaveFormat CreateWaveFormat()
{
WaveFormat format = new WaveFormat();
format.FormatTag = WaveFormatTag.Pcm; // PCM
format.SamplesPerSecond = 8000; // 16KHz
format.BitsPerSample =16; // 16Bit
format.Channels = 1; // Mono
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
return format;
}
/// <summary>
/// 创建录音使用的缓冲区
/// </summary>
private void CreateCaptureBuffer()
{
// 缓冲区的描述对象
CaptureBufferDescription bufferdescription = new CaptureBufferDescription();
if (null != mNotify)
{
mNotify.Dispose();
mNotify = null;
}
if (null != mRecBuffer)
{
mRecBuffer.Dispose();
mRecBuffer = null;
}
// 设定通知的大小,默认为1s钟
mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8);
mNotifySize -= mNotifySize % mWavFormat.BlockAlign;
// 设定缓冲区大小
mBufferSize = mNotifySize * cNotifyNum;
// 创建缓冲区描述
bufferdescription.BufferBytes = mBufferSize;
bufferdescription.Format = mWavFormat; // 录音格式
// 创建缓冲区
mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);
mNextCaptureOffset = 0;
}
/// <summary>
/// 初始化通知事件,将原缓冲区分成16个缓冲队列,在每个缓冲队列的结束点设定通知点.
/// </summary>
/// <returns>是否成功</returns>
private bool InitNotifications()
{
if (null == mRecBuffer)
{
MessageBox.Show("未创建录音缓冲区");
return false;
}
// 创建一个通知事件,当缓冲队列满了就激发该事件.
mNotificationEvent = new AutoResetEvent(false);
// 创建一个线程管理缓冲区事件
if (null == mNotifyThread)
{
mNotifyThread = new Thread(new ThreadStart(WaitThread));
mNotifyThread.Start();
}
// 设定通知的位置
BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];
for (int i = 0; i < cNotifyNum; i++)
{
PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;
PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;
}
mNotify = new Notify(mRecBuffer);
mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);
return true;
}
/// <summary>
/// 将录制的数据写入wav文件
/// </summary>
private void RecordCapturedData()
{
byte[] CaptureData = null;
int ReadPos;
int CapturePos;
int LockSize;
mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
LockSize = ReadPos - mNextCaptureOffset;
if (LockSize < 0)
LockSize += mBufferSize;
// 对齐缓冲区边界,实际上由于开始设定完整,这个操作是多余的.
LockSize -= (LockSize % mNotifySize);
if (0 == LockSize)
return;
// 读取缓冲区内的数据
CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
// 写入Wav文件
mWriter.Write(CaptureData, 0, CaptureData.Length);
// 更新已经录制的数据长度.
mSampleCount += CaptureData.Length;
// 移动录制数据的起始点,通知消息只负责指示产生消息的位置,并不记录上次录制的位置
mNextCaptureOffset += CaptureData.Length;
mNextCaptureOffset %= mBufferSize; // Circular buffer
}

/// <summary>
/// 接收缓冲区满消息的处理线程
/// </summary>
private void WaitThread()
{
while (true)
{
// 等待缓冲区的通知消息
mNotificationEvent.WaitOne(Timeout.Infinite, true);
// 录制数据
RecordCapturedData();
}

}

/// <summary>
/// 创建保存的波形文件,并写入必要的文件头.
/// </summary>
private void CreateSoundFile()
{
mWaveFile = new FileStream(mFileName, FileMode.Create);
mWriter = new BinaryWriter(mWaveFile);
// Set up file with RIFF chunk info.
char[] ChunkRiff = { 'R', 'I', 'F', 'F' };
char[] ChunkType = { 'W', 'A', 'V', 'E' };
char[] ChunkFmt = { 'f', 'm', 't', ' ' };
char[] ChunkData = { 'd', 'a', 't', 'a' };
short shPad = 1; // File padding
int nFormatChunkLength = 0x10; // Format chunk length.
int nLength = 0; // File length, minus first 8 bytes of RIFF description. This will be filled in later.
short shBytesPerSample = 0; // Bytes per sample.
// 一个样本点的字节数目
if (8 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels)
shBytesPerSample = 1;
else if ((8 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) || (16 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels))
shBytesPerSample = 2;
else if (16 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels)
shBytesPerSample = 4;
// RIFF 块
mWriter.Write(ChunkRiff);
mWriter.Write(nLength);
mWriter.Write(ChunkType);
// WAVE块
mWriter.Write(ChunkFmt);
mWriter.Write(nFormatChunkLength);
mWriter.Write(shPad);
mWriter.Write(mWavFormat.Channels);
mWriter.Write(mWavFormat.SamplesPerSecond);
mWriter.Write(mWavFormat.AverageBytesPerSecond);
mWriter.Write(shBytesPerSample);
mWriter.Write(mWavFormat.BitsPerSample);

// 数据块
mWriter.Write(ChunkData);
mWriter.Write((int)0); // The sample length will be written in later.

}

ztwz 2008-11-29
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
namespace record
{
class SoundRecord
{
public const int cNotifyNum = 16; // 缓冲队列的数目
private int mNextCaptureOffset = 0; // 该次录音缓冲区的起始点
private int mSampleCount = 0; // 录制的样本数目
private int mNotifySize = 0; // 每次通知大小
private int mBufferSize = 0; // 缓冲队列大小
private string mFileName = string.Empty; // 文件名
private FileStream mWaveFile = null; // 文件流
private BinaryWriter mWriter = null; // 写文件
private Capture mCapDev = null; // 音频捕捉设备
private CaptureBuffer mRecBuffer = null; // 缓冲区对象
private Notify mNotify = null; // 消息通知对象
private WaveFormat mWavFormat; // 录音的格式
private Thread mNotifyThread = null; // 处理缓冲区消息的线程
private AutoResetEvent mNotificationEvent = null; // 通知事件

/// <summary>
/// 构造函数,设定录音设备,设定录音格式.
/// </summary>
public SoundRecord()
{
// 初始化音频捕捉设备
InitCaptureDevice();
// 设定录音格式
mWavFormat = CreateWaveFormat();
}
/// <summary>
/// 设定录音结束后保存的文件,包括路径
/// </summary>
/// <param name="filename">保存wav文件的路径名</param>
public void SetFileName(string filename)
{
mFileName = filename;
}
/// <summary>
/// 开始录音
/// </summary>
public void RecStart()
{
// 创建录音文件
CreateSoundFile();
// 创建一个录音缓冲区,并开始录音
CreateCaptureBuffer();
// 建立通知消息,当缓冲区满的时候处理方法
InitNotifications();
mRecBuffer.Start(true);
}
/// <summary>
/// 停止录音
/// </summary>
public void RecStop()
{
// 关闭通知消息
if (null != mNotificationEvent)
mNotificationEvent.Set();
// 停止录音
mRecBuffer.Stop();
// 写入缓冲区最后的数据
RecordCapturedData();
// 回写长度信息
mWriter.Seek(4, SeekOrigin.Begin);
mWriter.Write((int)(mSampleCount + 36)); // 写文件长度
mWriter.Seek(40, SeekOrigin.Begin);
mWriter.Write(mSampleCount); // 写数据长度
mWriter.Close();
mWaveFile.Close();
mWriter = null;
mWaveFile = null;
}
/// <summary>
/// 初始化录音设备,此处使用主录音设备.
/// </summary>
/// <returns>调用成功返回true,否则返回false</returns>
private bool InitCaptureDevice()
{

// 获取默认音频捕捉设备
CaptureDevicesCollection devices = new CaptureDevicesCollection(); // 枚举音频捕捉设备
Guid deviceGuid = Guid.Empty; // 音频捕捉设备的ID
if (devices.Count > 0)
deviceGuid = devices[0].DriverGuid;
else
{
MessageBox.Show("系统中没有音频捕捉设备");
return false;
}
// 用指定的捕捉设备创建Capture对象
try
{
mCapDev = new Capture(deviceGuid);
}
catch (DirectXException e)
{
MessageBox.Show(e.ToString());
return false;
}
return true;
}
}
}

slimfeng 2008-11-29
  • 打赏
  • 举报
回复
学习一下
mjjzg 2008-11-29
  • 打赏
  • 举报
回复
没搞过,
hm7921936 2008-11-29
  • 打赏
  • 举报
回复
关注
happychou 2008-11-29
  • 打赏
  • 举报
回复
帮顶。。。。
chagel 2008-11-29
  • 打赏
  • 举报
回复
利用c#版本的LiquidVideo,一个完整的播放器实现
这里有源码:
http://www.codeproject.com/KB/audio-video/LiquidVideo.aspx
xufzu123 2008-11-29
  • 打赏
  • 举报
回复
我也想知道~呵呵~

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧