-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathClass1.cs
More file actions
65 lines (62 loc) · 2.09 KB
/
Class1.cs
File metadata and controls
65 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using NAudio.Wave;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace RecordAudio
{
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual), IDispatchImpl(IDispatchImplType.InternalImpl)]
class Record
{
private static WasapiLoopbackCapture capture = null;
public static string startRecord(string path, int maxTimeSecond)
{
var outputFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wave");
if (path != null)
{
outputFolder = path;
}
if (maxTimeSecond == 0)
{
maxTimeSecond = 60;
}
//test
Console.Out.WriteLine(outputFolder);
Directory.CreateDirectory(outputFolder);
var fileName = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss") + "-Record.wav";
var outputFilePath = Path.Combine(outputFolder, fileName);
capture = new WasapiLoopbackCapture();
var writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);
capture.DataAvailable += (s, a) =>
{
writer.Write(a.Buffer, 0, a.BytesRecorded);
if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * maxTimeSecond)
{
//test
Console.Out.WriteLine($"out Time:{maxTimeSecond}");
capture.StopRecording();
}
};
capture.RecordingStopped += (s, a) =>
{
writer.Dispose();
writer = null;
capture.Dispose();
};
capture.StartRecording();
return outputFilePath;
}
public static bool stopRecord()
{
if (capture == null)
{
return false;
}
if (capture.CaptureState != NAudio.CoreAudioApi.CaptureState.Stopped)
{
capture.StopRecording();
return true;
}
return false;
}
}
}