c++ - 正确写一个类和使用pthread配合vlc库和c++
问题描述
我的应用程序(C++、WxWidgets、Ubuntu)必须根据用户操作播放不同的 mp3 文件。目前,我使用vlc库,我总是调用一个新函数来重现音频文件,但这需要太多代码,我认为它不是那么专业。由于我不想在播放 mp3 时停止应用程序的流程,因此我使用线程。
我试图为 mp3 编写一个类,但我认为这是不正确的,因为我收到了这个错误:
/home/isola/Documents/Isola02/secondpanel.cpp:68:102: error: invalid use of void expression
pthread_create(&thread, NULL, mp3->play_mp3("/home/user/Project/audio/scegli-rifiuto.mp3"), NULL);
这是我班的代码:
重播.cpp
#include "rePlay.h"
#include <vlc/vlc.h>
rePlay::rePlay()
{
//ctor
}
rePlay::~rePlay()
{
//dtor
}
void rePlay::play_mp3(const char* path){
// load the vlc engine
inst = libvlc_new(0, NULL);
printf("apro il file %d\n", inst);
// create a new item
m = libvlc_media_new_path(inst, path);
// create a media play playing environment
mp = libvlc_media_player_new_from_media(m);
// no need to keep the media now
libvlc_media_release(m);
// play the media_player
libvlc_media_player_play(mp);
printf("Done.\n");
}
void rePlay::stop_mp3(){
// stop playing
libvlc_media_player_stop(mp);
// free the media_player
libvlc_media_player_release(mp);
libvlc_release(inst);
}
和头文件rePlay.h
#ifndef REPLAY_H
#define REPLAY_H
#include <vlc/vlc.h>
class rePlay
{
public:
rePlay();
virtual ~rePlay();
void play_mp3(const char*);
void stop_mp3();
protected:
libvlc_instance_t *inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
private:
};
#endif // REPLAY_H
我的想法是打电话:
pthread_t thread;
rePlay *mp3;
mp3->新的重播();pthread_create(&thread, NULL, mp3->play_mp3("/home/user/Project/audio/scegli-rifiuto.mp3"), NULL);
每次我想重现 mp3 时传递文件的路径,然后调用:
pthread_create(&thread, NULL, mp3->stop_mp3, NULL);
当我想阻止它时。
目前,我从编译器那里得到这个关于 pthread_create 的错误,但我认为应该还有其他问题,因为我不知道 play_mp3() 和 stop_mp3() 是否可以工作。
你能帮我吗?
EDIT1:如果我不使用 pthread_create 函数,该类可以工作
EDIT2:如果我使用我会得到同样的错误:
std::thread first (mp3->play_mp3("/home/robodyne/Project/audio/scegli-rifiuto.mp3"));
错误:
/home/isola/Documents/Isola02/secondpanel.cpp:85:85: error: invalid use of void expression
std::thread first (mp3->play_mp3("/home/robodyne/Project/audio/scegli-rifiuto.mp3"));
EDIT3:
为什么如果我rePlay *mp3_apertura_porta;
在另一个名为firstpanel
as的类中声明public
,则会收到此错误:
/home/isola/Documents/Isola02/firstpanel.cpp: In member function ‘void firstpanel::check_cf(wxTimerEvent&)’:
/home/isola/Documents/Isola02/firstpanel.cpp:160:44: error: capture of non-variable ‘firstpanel::mp3_apertura_porta’
std::thread second = std::thread([&mp3_apertura_porta]() noexcept {
^~~~~~~~~~~~~~~~~~
In file included from /home/isola/Documents/Isola02/firstpanel.cpp:1:0:
/home/isola/Documents/Isola02/firstpanel.h:20:12: note: ‘rePlay* firstpanel::mp3_apertura_porta’ declared here
rePlay *mp3_apertura_porta;
^~~~~~~~~~~~~~~~~~
/home/isola/Documents/Isola02/firstpanel.cpp: In lambda function:
/home/isola/Documents/Isola02/firstpanel.cpp:161:9: error: ‘this’ was not captured for this lambda function
mp3_apertura_porta->play_mp3("/home/robodyne/Project/audio/scegli-rifiuto.mp3"); });
当我打电话时
rePlay *mp3_apertura_porta = new rePlay();
std::thread first = std::thread([&mp3_apertura_porta]() noexcept {
mp3_apertura_porta->play_mp3("/home/isola/Documents/Isola02/audio/errore-ripetere-la-strisciata.mp3"); });
first.join();
在 firstpanel.cpp 中?
解决方案
您在EDIT2中启动线程的语法不正确。这是使用 lambda 的一种方法:
std::thread first = std::thread([&mp3]() noexcept {
try {
mp3->play_mp3("/home/robodyne/Project/audio/scegli-rifiuto.mp3");
} catch(...) {
}
});
//...
first.join();
如果您对 lambdas 不满意,您可以使用的另一个选项类似于您将使用的选项pthreads
:
// run MP3::play_mp3 on object mp3
std::thread second(&MP3::play_mp3, mp3,
"/home/robodyne/Project/audio/scegli-rifiuto.mp3");
// ...
second.join();
推荐阅读
- cobol - 如何在大型机ispf中列出项目中的所有cobol程序
- reactjs - React-Beautiful-DnD:不变式失败:provided.innerRef 尚未提供 HTMLElement
- r - 将函数列表应用于值向量
- oracle - 循环游标 - substr(char) 到日期,减去 sysdate - PLSQL Oracle
- java - ArrayList 泛型数组使用 accolades
- linux - 从用户空间到设备的 I/O
- python - 我收到此错误“NoneType”对象没有属性“组”
- email - 在 ASP 中发送多条消息
- spring - 在配置类中创建的模拟 bean - 使用 JUnit 5 的 Spring Boot
- python - 使用 jedi-vim 使用 gobject introspection 在 python 中自动完成自己的 gobject 派生库