正确地编写一个类,并将pthread与vlc库和c++一起使用

Correctly write a class and use pthread with vlc library and c++

本文关键字:vlc pthread 库和 c++ 一起 并将 一个 正确地      更新时间:2023-10-16

我的应用程序(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);

这是我班的代码:

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 %dn", 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->新的rePlay((;pthread_create(&thread,NULL,mp3->播放_mp3("/home/user/Project/audio/scegli-rifuto.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:为什么如果我将另一个名为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();

如果你对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();