我的应用程序(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);
这是我类(class)的代码:
rePlay.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);
}
和 header 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"));
编辑 3:
为什么如果我在另一个名为 firstpanel
的类中将 rePlay *mp3_apertura_porta;
声明为 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();
如果您对 lambda 不满意,您可以使用的另一个选项类似于您在 pthreads
中使用的选项:
// run MP3::play_mp3 on object mp3
std::thread second(&MP3::play_mp3, mp3,
"/home/robodyne/Project/audio/scegli-rifiuto.mp3");
// ...
second.join();
关于c++ - 正确编写一个类并使用pthread与vlc库和c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58537021/