Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderZsq committed Mar 22, 2021
1 parent c0e7c64 commit e69a4fe
Show file tree
Hide file tree
Showing 16 changed files with 661 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.1, 2021-03-19T23:50:01. -->
<!-- Written by QtCreator 4.14.1, 2021-03-22T23:21:25. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
4 changes: 2 additions & 2 deletions StudyNotes/Audio Video/08_record_audio/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {
#define FILENAME "F:/out.pcm"
#else
#define FMT_NAME "avfoundation"
#define DEVICE_NAME ":0"
#define DEVICE_NAME ":1"
#define FILENAME "/Users/zhushuangquan/Desktop/out.pcm"
#endif

Expand All @@ -44,7 +44,7 @@ void MainWindow::on_audioButton_clicked() {

qDebug() << "on_audioButton_clicked" << QThread::currentThread();

return;
// return;
// 获取输入格式对象
AVInputFormat *fmt = av_find_input_format(FMT_NAME);
if (!fmt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ win32 {

macx {
FFMPEG_HOME = /usr/local/Cellar/ffmpeg/4.3.2
QMAKE_INFO_PLIST = mac/Info.plist
}

INCLUDEPATH += $${FFMPEG_HOME}/include
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.1, 2021-03-19T23:56:07. -->
<!-- Written by QtCreator 4.14.1, 2021-03-22T23:19:43. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
35 changes: 26 additions & 9 deletions StudyNotes/Audio Video/09_record_audio_sub_thread/audiothread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QDebug>
#include <QFile>
#include <QDateTime>

extern "C" {
// 设备
Expand All @@ -18,11 +19,11 @@ extern "C" {
// 设备名称
#define DEVICE_NAME "audio=线路输入 (3- 魅声T800)"
// PCM文件名
#define FILENAME "F:/out.pcm"
#define FILEPATH "F:/"
#else
#define FMT_NAME "avfoundation"
#define DEVICE_NAME ":0"
#define FILENAME "/Users/zhushuangquan/Desktop/out.pcm"
#define DEVICE_NAME ":1"
#define FILEPATH "/Users/zhushuangquan/Desktop/"
#endif

AudioThread::AudioThread(QObject *parent) : QThread(parent) {
Expand All @@ -32,6 +33,8 @@ AudioThread::AudioThread(QObject *parent) : QThread(parent) {
}

AudioThread::~AudioThread() {
// 断开所有的连接
disconnect();
// 内存回收之前,正常结束线程
requestInterruption();
// 安全退出
Expand Down Expand Up @@ -65,12 +68,16 @@ void AudioThread::run() {
}

// 文件名
QFile file(FILENAME);
QString filename = FILEPATH;

filename += QDateTime::currentDateTime().toString("MM_dd_HH_mm_ss");
filename += ".pcm";
QFile file(filename);

// 打开文件
// WriteOnly:只写模式。如果文件不存在,就创建文件;如果文件存在,就会清空文件内容
if (!file.open(QFile::WriteOnly)) {
qDebug() << "文件打开失败" << FILENAME;
qDebug() << "文件打开失败" << filename;

// 关闭设备
avformat_close_input(&ctx);
Expand All @@ -79,10 +86,20 @@ void AudioThread::run() {

// 数据包
AVPacket pkt;
// 不断采集数据
while (!isInterruptionRequested() && av_read_frame(ctx, &pkt) == 0) {
// 将数据写入文件
file.write((const char *) pkt.data, pkt.size);
while (!isInterruptionRequested()) {
// 不断采集数据
ret = av_read_frame(ctx, &pkt);

if (ret == 0) { // 读取成功
// 将数据写入文件
file.write((const char *) pkt.data, pkt.size);
} else {
// if (ret == AVERROR(EAGAIN))
char errbuf[1024];
av_strerror(ret, errbuf, sizeof (errbuf));
qDebug() << "av_read_frame error" << errbuf << ret;
break;
}
}
// while (!_stop && av_read_frame(ctx, &pkt) == 0) {
// // 将数据写入文件
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSMicrophoneUsageDescription</key>
<string>申请使用麦克风进行录音</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ void MainWindow::on_audioButton_clicked() {
_audioThread = new AudioThread(this);
_audioThread->start();

connect(_audioThread, &AudioThread::finished,
[this]() { // 线程结束
_audioThread = nullptr;
ui->audioButton->setText("开始录音");
});

// 设置按钮文字
ui->audioButton->setText("结束录音");
} else { // 点击了“结束录音”
Expand Down
46 changes: 46 additions & 0 deletions StudyNotes/Audio Video/10_sdl_play_pcm/10_sdl_play_pcm.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mainwindow.cpp \
playthread.cpp

HEADERS += \
mainwindow.h \
playthread.h

FORMS += \
mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32 {
SDL_HOME = F:/Dev/SDL2-2.0.14/x86_64-w64-mingw32
}

mac {
SDL_HOME = /usr/local/Cellar/sdl2/2.0.14_1
}

INCLUDEPATH += $${SDL_HOME}/include

LIBS += -L $${SDL_HOME}/lib \
-lSDL2
Loading

0 comments on commit e69a4fe

Please sign in to comment.