libVLC 音频立体声模式切换

在libVLC中,可以使用libvlc_audio_set_channel函数来设置音频的立体声模式。这个函数允许选择不同的音频通道,例如立体声、左声道、右声道、环绕声等。

/*** Set current audio channel.** \param p_mi media player* \param channel the audio channel, \see libvlc_audio_output_channel_t* \return 0 on success, -1 on error*/
LIBVLC_API int libvlc_audio_set_channel( libvlc_media_player_t *p_mi, int channel );

参数说明:

  • p_mi:媒体播放器实例。
  • channel:要设置的音频通道,是一个 libvlc_audio_output_channel_t 类型的枚举值。

返回值是一个整数,如果设置成功则返回 0,如果设置失败则返回一个负数。

的可能值包括:

/*** Audio channels*/
typedef enum libvlc_audio_output_channel_t {libvlc_AudioChannel_Error   = -1, //错误通道libvlc_AudioChannel_Stereo  =  1, //立体声libvlc_AudioChannel_RStereo =  2, //翻转立体声libvlc_AudioChannel_Left    =  3, //左声道libvlc_AudioChannel_Right   =  4, //右声道libvlc_AudioChannel_Dolbys  =  5  //杜比环绕声
} libvlc_audio_output_channel_t;

以下是VLC播放器中使用的视频裁剪,我们仿照这个做一个界面。  

以下是做出来的效果:

首先做一个菜单:

	QMenu *m_moduleMenu = nullptr;		//立体声模式菜单QAction *m_actionModule = nullptr;	//立体声模式QAction *m_actionMono = nullptr;//单声道QAction *m_actionStereo = nullptr;//立体声QAction *m_actionLeft = nullptr;//左声道QAction *m_actionRight = nullptr;//右声道QAction *m_actionRevStereo = nullptr;//反转立体声m_moduleMenu = new QMenu(this);m_actionMono = m_moduleMenu->addAction("单声道");m_actionMono->setData((int)libvlc_AudioChannel_Dolbys);m_actionStereo = m_moduleMenu->addAction("立体声");m_actionStereo->setData((int)libvlc_AudioChannel_Stereo);m_actionLeft = m_moduleMenu->addAction("左声道");m_actionLeft->setData((int)libvlc_AudioChannel_Left);m_actionRight = m_moduleMenu->addAction("右声道");m_actionRight->setData((int)libvlc_AudioChannel_Right);m_actionRevStereo = m_moduleMenu->addAction("反转立体声");m_actionRevStereo->setData((int)libvlc_AudioChannel_RStereo);m_actionModule->setMenu(m_moduleMenu);

接受槽函数的响应事件:

void showWidget::slotActionModuleTriggered(QAction *action)
{if (!vlc_mediaPlayer)return;libvlc_audio_output_channel_t channel = (libvlc_audio_output_channel_t)action->data().toInt();int cc = libvlc_audio_set_channel(vlc_mediaPlayer, channel);action->setIcon(QIcon(":/image/images/point.png"));m_preAction->setIcon(QIcon());m_preAction = action;
}

设置鼠标右键弹出菜单:重写鼠标点击事件,弹出菜单。

首先设置libvlc忽略鼠标事件libvlc_video_set_mouse_input(vlc_mediaPlayer, 0);

再重写鼠标事件:

void showWidget::mousePressEvent(QMouseEvent *event)
{switch (event->button()){case Qt::RightButton://this->setWindowState(Qt::WindowMinimized);m_menu->exec(event->globalPos());break;default:QWidget::mousePressEvent(event);}
}

完整代码:

头文件。

#pragma once#include <QtWidgets/QWidget>
#include "ui_showWidget.h"
#include <QMenu>
#include <QActionGroup>
#include <vlc/vlc.h>
#include <QDebug>
#include <QFileDialog>
#include <QThread>
#include <QMouseEvent>
#include <QKeyEvent>enum Rate
{Rate2X,Rate1_5X,Rate1_25X,Rate1_0X,Rate0_75X,Rate0_5X
};class showWidget : public QWidget
{Q_OBJECTpublic:showWidget(QWidget *parent = nullptr);~showWidget();private slots:void slotOpenFile();void slotPlay();void slotPause();void slotStop();void slotValueChanged(int value);void slotCurrentIndexChanged(int index);void slotActionTriggered(QAction *action);void slotActionModuleTriggered(QAction *action);protected:virtual void mousePressEvent(QMouseEvent *event);private://事件处理回调static void vlcEvents(const libvlc_event_t *ev, void *param);private:Ui::showWidgetClass ui;private:libvlc_instance_t *vlc_base = nullptr;libvlc_media_t *vlc_media = nullptr;libvlc_media_player_t *vlc_mediaPlayer = nullptr;QList<float> m_lstRate;QList<QString> m_lstAudioDevice;QMenu *m_menu = nullptr;QMenu *m_deviceMenu = nullptr;			//音频设备菜单QMenu *m_moduleMenu = nullptr;		//立体声模式菜单QAction *m_actionAudioDev = nullptr;	//音频设备QAction *m_actionModule = nullptr;	//立体声模式QAction *m_actionMono = nullptr;//单声道QAction *m_actionStereo = nullptr;//立体声QAction *m_actionLeft = nullptr;//左声道QAction *m_actionRight = nullptr;//右声道QAction *m_actionRevStereo = nullptr;//反转立体声QActionGroup *m_group = nullptr;QActionGroup *m_groupModule = nullptr;QAction *m_preAction = nullptr;
};

cpp文件。 

#include "showWidget.h"
#include <QTimer>
#include <QTime>#pragma execution_character_set("utf-8")showWidget::showWidget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);this->setWindowTitle("视频播放器");m_menu = new QMenu(this);m_actionAudioDev = m_menu->addAction("音频设备");m_actionModule = m_menu->addAction("立体声模式");m_deviceMenu = new QMenu(this);m_actionAudioDev->setMenu(m_deviceMenu);m_moduleMenu = new QMenu(this);m_actionMono = m_moduleMenu->addAction("单声道");m_actionMono->setData((int)libvlc_AudioChannel_Dolbys);m_actionStereo = m_moduleMenu->addAction("立体声");m_actionStereo->setData((int)libvlc_AudioChannel_Stereo);m_actionLeft = m_moduleMenu->addAction("左声道");m_actionLeft->setData((int)libvlc_AudioChannel_Left);m_actionRight = m_moduleMenu->addAction("右声道");m_actionRight->setData((int)libvlc_AudioChannel_Right);m_actionRevStereo = m_moduleMenu->addAction("反转立体声");m_actionRevStereo->setData((int)libvlc_AudioChannel_RStereo);m_actionModule->setMenu(m_moduleMenu);m_group = new QActionGroup(this);connect(m_group, &QActionGroup::triggered, this, &showWidget::slotActionTriggered);m_groupModule = new QActionGroup(this);m_groupModule->addAction(m_actionMono);m_groupModule->addAction(m_actionStereo);m_groupModule->addAction(m_actionLeft);m_groupModule->addAction(m_actionRight);m_groupModule->addAction(m_actionRevStereo);connect(m_groupModule, &QActionGroup::triggered, this, &showWidget::slotActionModuleTriggered);vlc_base = libvlc_new(0, NULL);m_lstAudioDevice.clear();libvlc_audio_output_device_t *pDevList = nullptr;libvlc_audio_output_t *pOutputList = libvlc_audio_output_list_get(vlc_base);while (pOutputList){//获取设备pDevList = libvlc_audio_output_device_list_get(vlc_base, pOutputList->psz_name);while (pDevList){// 找到我想要的那个设备,跳出循环,这里使用i控制,我知道我想要的设备位置if (strcmp(pOutputList->psz_name, "mmdevice") == 0){QString des = QString::fromStdString(std::string(pDevList->psz_description, strlen(pDevList->psz_description)));m_lstAudioDevice.append(des);QAction *action = m_deviceMenu->addAction(des);action->setData(pDevList->psz_device);m_group->addAction(action);}pDevList = pDevList->p_next;}pOutputList = pOutputList->p_next;}ui.cbxRate->setCurrentIndex(Rate1_0X);m_lstRate << 2.0 << 1.5 << 1.25 << 1.0 << 0.75 << 0.5;ui.btnOpen->setFocusPolicy(Qt::NoFocus);ui.btnPlay->setFocusPolicy(Qt::NoFocus);ui.btnPause->setFocusPolicy(Qt::NoFocus);ui.btnStop->setFocusPolicy(Qt::NoFocus);ui.hSliderVolumn->setFocusPolicy(Qt::NoFocus);ui.cbxRate->setFocusPolicy(Qt::NoFocus);connect(ui.btnOpen, &QPushButton::clicked, this, &showWidget::slotOpenFile);connect(ui.btnPlay, &QPushButton::clicked, this, &showWidget::slotPlay);connect(ui.btnPause, &QPushButton::clicked, this, &showWidget::slotPause);connect(ui.btnStop, &QPushButton::clicked, this, &showWidget::slotStop);connect(ui.hSliderVolumn, &QSlider::valueChanged, this, &showWidget::slotValueChanged);connect(ui.cbxRate,SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentIndexChanged(int)));
}showWidget::~showWidget()
{libvlc_release(vlc_base); //减少libvlc实例的引用计数,并销毁
}void showWidget::slotOpenFile()
{/*选择文件*/QString filename = QFileDialog::getOpenFileName(this, "选择打开的文件", "D:/", tr("*.*"));std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\\'));vlc_media = libvlc_media_new_path(vlc_base, filename.toUtf8().data());if (!vlc_media) {return;}// 创建libvlc实例和媒体播放器vlc_mediaPlayer = libvlc_media_player_new_from_media(vlc_media);if (!vlc_mediaPlayer) {return;}//libvlc_media_add_option(vlc_media, "--video-filter=transform");//libvlc_media_add_option(vlc_media, "---transform-type=270");// 等待元数据加载完成libvlc_media_parse(vlc_media);libvlc_video_set_mouse_input(vlc_mediaPlayer, 0);libvlc_video_set_key_input(vlc_mediaPlayer, 0);// 获取各种元数据const char *title = libvlc_media_get_meta(vlc_media, libvlc_meta_Title);const char *artist = libvlc_media_get_meta(vlc_media, libvlc_meta_Artist);const char *album = libvlc_media_get_meta(vlc_media, libvlc_meta_Album);const char *url = libvlc_media_get_meta(vlc_media, libvlc_meta_URL);const char *date = libvlc_media_get_meta(vlc_media, libvlc_meta_Date);const char *lang = libvlc_media_get_meta(vlc_media, libvlc_meta_Language);int duration = libvlc_media_get_duration(vlc_media);  // 获取时长(单位:毫秒)qDebug("Title: %s", title ? title : "N/A");qDebug("Artist: %s", artist ? artist : "N/A");qDebug("Album: %s", album ? album : "N/A");qDebug("Duration: %d ms", duration);qDebug("url: %s", url ? url : "N/A");qDebug("date: %s", date ? date : "N/A");qDebug("lang: %s", lang ? lang : "N/A");libvlc_media_track_t **tracks;int track_count = libvlc_media_tracks_get(vlc_media,&tracks);for (unsigned i = 0; i < track_count; i++) {libvlc_media_track_t* track = tracks[i];// 显示轨道信息printf("Track #%u: %s\n", i, track->psz_description);// 这里可以获取到每一个轨道的信息,比如轨道类型 track->i_type// 可能是 libvlc_track_video, libvlc_track_audio 或者 libvlc_track_text (字幕)if (track->i_type == libvlc_track_video) {// 处理视频轨道信息qDebug("width = %d",track->video->i_width);qDebug("height = %d", track->video->i_height);qDebug("rate_num = %d", track->video->i_frame_rate_num);qDebug("rate_den = %d", track->video->i_frame_rate_den);}else if (track->i_type == libvlc_track_audio) {// 处理音频轨道信息qDebug("channels = %d", track->audio->i_channels);qDebug("rate = %d", track->audio->i_rate);}else if (track->i_type == libvlc_track_text) {// 处理字幕轨道信息}}//获取事件管理器libvlc_event_manager_t *em = libvlc_media_player_event_manager(vlc_mediaPlayer);// 注册事件监听器libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui.widgetShow->winId());libvlc_video_set_adjust_int(vlc_mediaPlayer, libvlc_adjust_Enable, 1);//获取当前的channelint curChannel = libvlc_audio_get_channel(vlc_mediaPlayer);m_actionStereo->setIcon(QIcon(":/image/images/point.png"));m_preAction = m_actionStereo;QTimer::singleShot(1000, this, &showWidget::slotPlay);libvlc_video_filter_list_get(vlc_base);
}void showWidget::slotPlay()
{if (vlc_mediaPlayer){libvlc_media_player_play(vlc_mediaPlayer);}}void showWidget::slotPause()
{if (vlc_mediaPlayer)libvlc_media_player_pause(vlc_mediaPlayer);
}void showWidget::slotStop()
{if (vlc_mediaPlayer)libvlc_media_player_stop(vlc_mediaPlayer);
}void showWidget::slotValueChanged(int value)
{if (vlc_mediaPlayer)libvlc_audio_set_volume(vlc_mediaPlayer, value);
}void showWidget::slotCurrentIndexChanged(int index)
{if (vlc_mediaPlayer)libvlc_media_player_set_rate(vlc_mediaPlayer, m_lstRate[index]);
}void showWidget::slotActionTriggered(QAction *action)
{if (!vlc_mediaPlayer)return;QString txt = action->data().toString();//指定设备输出pDevList->psz_devicelibvlc_audio_output_device_set(vlc_mediaPlayer, NULL, txt.toStdString().c_str());
}void showWidget::slotActionModuleTriggered(QAction *action)
{if (!vlc_mediaPlayer)return;libvlc_audio_output_channel_t channel = (libvlc_audio_output_channel_t)action->data().toInt();int cc = libvlc_audio_set_channel(vlc_mediaPlayer, channel);action->setIcon(QIcon(":/image/images/point.png"));m_preAction->setIcon(QIcon());m_preAction = action;
}void showWidget::mousePressEvent(QMouseEvent *event)
{switch (event->button()){case Qt::RightButton://this->setWindowState(Qt::WindowMinimized);m_menu->exec(event->globalPos());break;default:QWidget::mousePressEvent(event);}
}//事件回调
void showWidget::vlcEvents(const libvlc_event_t *ev, void *param)
{showWidget *w = (showWidget*)param;//处理不同的事件switch (ev->type) {case libvlc_MediaPlayerTimeChanged:{//qDebug() << "VLC媒体播放器时间已更改";qint64 len = libvlc_media_player_get_time(w->vlc_mediaPlayer);libvlc_time_t lenSec = len / 1000;libvlc_time_t totalLen = libvlc_media_player_get_length(w->vlc_mediaPlayer);libvlc_time_t totalLenSec = totalLen / 1000;int thh, tmm, tss;thh = lenSec / 3600;tmm = (lenSec % 3600) / 60;tss = (lenSec % 60);QTime time(thh, tmm, tss);w->ui.lbCurTime->setText(time.toString("hh:mm:ss"));thh = totalLenSec / 3600;tmm = (totalLenSec % 3600) / 60;tss = (totalLenSec % 60);QTime TotalTime(thh, tmm, tss);w->ui.lbTotalTime->setText(TotalTime.toString("hh:mm:ss"));double pos = (double)lenSec / totalLenSec * 100;w->ui.horizontalSlider->setValue(pos);}break;case libvlc_MediaPlayerEndReached:qDebug() << "VLC播放完毕.";break;case libvlc_MediaPlayerStopped:qDebug() << "VLC停止播放";break;case libvlc_MediaPlayerPlaying:qDebug() << "VLC开始播放";break;case libvlc_MediaPlayerPaused:qDebug() << "VLC暂停播放";break;}
}

更多参考:

libVLC 事件机制-CSDN博客

libVLC windows开发环境搭建-CSDN博客

libVLC 元数据-CSDN博客

libVLC 添加图片和文本水印-CSDN博客

libVLC 音频输出设备切换-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/599851.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java(二)面向对象进阶

目录 面向对象 多态性 向下转型 Object equals() toString() clone() finalize() Static 单例模式 代码块 final 抽象类与抽象方法(或abstract关键字&#xff09; 接口 接口的多态性 接口的默认方法 内部类 成员内部类 局部内部类 枚举类 实现接口的枚举类 …

网络安全基础之网络协议与安全威胁

OSI(OpenSystem Interconnect)&#xff0c;即开放式系统互联。 一般都叫OSI参考模型&#xff0c;是ISO(国际标准化组织)组织在1985年研究的网络互联模型。 网络协议的简介&#xff1a; 定义&#xff1a;协议是网络中计算机或设备之间进行通信的一系列规则集合。 什么是规则?…

本地化部署离线开源免费语音识别API,支持多模态AI能力引擎

思通数科作为一家专注于多模态AI能力开源引擎平台&#xff0c;其技术产品涵盖了自然语言处理、情感分析、实体识别、图像识别与分类、OCR识别以及语音识别等多个领域。在语音识别这一细分市场&#xff0c;思通数科的技术产品中的音频文件转写服务有着相似的应用场景和功能特点。…

【Linux】达梦数据库安装部署(附详细图文)

目录 一、安装前的准备工作 1.检查操作系统配置 &#xff08;1&#xff09;获取系统位数 getconf LONG_BIT &#xff08;2&#xff09;查看操作系统release信息 cat /etc/system-release &#xff08;3&#xff09;查询系统名称 uname -a &#xff08;4&#xff09;查看操…

CSS - 你遇到过动画卡顿的问题吗

难度级别:中高级及以上 提问概率:70% 回答这道题,首先要说的就是,浏览器在每一帧动画里大概做了什么事情。首先浏览器会执行Javascript,或是操作DOM元素,紧接着需要对DOM元素进行样式计算,当计算完成后,就需要针对DOM元素的位置以及大小…

海外媒体宣发套餐推广8个要点解析为标题-华媒舍

在当前全球化的时代背景下&#xff0c;海外市场的开拓对于企业的发展至关重要。而海外媒体宣传是一种有效的推广方式&#xff0c;可以帮助企业在全球范围内打开市场。本文将对8个海外媒体宣发套餐的推广要点进行解析&#xff0c;帮助企业了解如何在海外市场进行宣传推广。 1. 媒…

自动装卸另类方案

导语 大家好&#xff0c;我是智能仓储物流技术研习社的社长&#xff0c;你的老朋友&#xff0c;老K。行业群 知名企业 读者福利&#xff1a; &#x1f449;抄底-仓储机器人-即买即用-免调试 智能制造-话题精读 1、西门子、ABB、汇川&#xff1a;2024中国工业数字化自动化50强 2…

LLMs时代的推荐系统

论文地址&#xff1a;https://arxiv.org/pdf/2307.02046.pdf 0、概述 随着电子商务和网络应用的繁荣发展&#xff0c;推荐系统&#xff08;RecSys&#xff09;已经成为我们日常生活中的重要组成部分&#xff0c;它们提供个性化建议&#xff0c;迎合用户偏好。虽然深度神经网络…

【Redis 知识储备】读写分离/主从分离架构 -- 分布系统的演进(4)

读写分离/主从分离架构 简介出现原因架构工作原理技术案例架构优缺点 简介 将数据库读写操作分散到不同的节点上, 数据库服务器搭建主从集群, 一主一从, 一主多从都可以, 数据库主机负责写操作, 从机只负责读操作 出现原因 数据库成为瓶颈, 而互联网应用一般读多写少, 数据库…

数仓开发之Flume《一》:Flume的概述及安装

目录 1. &#x1f959;Flume概述 1.1 Flume简介 1.2 Flume的架构 1. &#x1f9c0;agent介绍 2. ​Agent 主要有 3 个部分组成&#xff0c;Source、Channel、Sink。 &#x1f957;2.1 Source &#x1f957;2.2 Sink &#x1f957;2.3 Channel 3. &#x1f9c0;Flume 自…

milvus search api的数据结构

search api的数据结构 此api的功能是向量相似度搜索(vector similarity search) 一个完整的search例子: 服务端collection是一个hnsw类型的索引。 import random from pymilvus import (connections,Collection, )dim 128if __name__ __main__:connections.connect(alias…

zdpdjango_argonadmin使用Django开发一个美观的后台管理系统

初始代码 安装依赖 pip install -r requirements.txt生成管理员账户 迁移模型&#xff1a; python manage.py makemigrations python manage.py migrate创建超级用户&#xff1a; python manage.py createsuperuser启动服务 python manage.py runserver浏览器访问&#xf…