Added CVlcMedia class

This commit is contained in:
Mikhail Lobotskiy
2023-08-11 19:51:31 +04:00
parent 5eb4aaa5c0
commit e4ba29ddcd
6 changed files with 104 additions and 23 deletions

View File

@ -17,7 +17,11 @@ apple_silicon:greaterThan(QT_MAJOR_VERSION, 5) {
INCLUDEPATH += $$PWD
HEADERS += $$PWD/vlcplayer.h \
HEADERS += \
$$PWD/vlcplayer.h \
$$PWD/vlcmedia.h \
$$PWD/libvlc_base.h
SOURCES += $$PWD/vlcplayer.cpp
SOURCES += \
$$PWD/vlcplayer.cpp \
$$PWD/vlcmedia.cpp

View File

@ -115,17 +115,14 @@ void Mwindow::openFile()
{
/* The basic file-select box */
QUrl url = QFileDialog::getOpenFileUrl(this, tr("Load a file"));
QString sFile = QFileDialog::getOpenFileName(this, tr("Load a file"));
/* Create a new Media */
libvlc_media_t *vlcMedia = libvlc_media_new_location(vlcPlayer->getVlcInstance(), qtu(url.toString(QUrl::FullyEncoded)));
if (!vlcMedia)
return;
vlcPlayer->open(vlcMedia);
CVlcMedia* pMedia = new CVlcMedia(vlcPlayer->m_pVlcInstance, sFile, false);
vlcPlayer->open(pMedia);
/* Release the media */
libvlc_media_release(vlcMedia);
delete pMedia;
/* And start playback */
vlcPlayer->play();
@ -234,6 +231,6 @@ void Mwindow::fullscreen()
void Mwindow::closeEvent(QCloseEvent *event)
{
stop();
vlcPlayer->stop();
event->accept();
}

View File

@ -0,0 +1,51 @@
#include "vlcmedia.h"
#include <iostream>
#include <QUrl>
CVlcMedia::CVlcMedia(libvlc_instance_t* pVlcInstance, const QString& sFile, bool bEventForwarding)
{
// create vlc media from local file
QUrl url = QUrl::fromLocalFile(sFile);
m_pMedia = libvlc_media_new_location(pVlcInstance, url.toString(QUrl::FullyEncoded).toUtf8().constData());
init(bEventForwarding);
}
CVlcMedia::CVlcMedia(libvlc_media_t* pVlcMedia, bool bEventForwarding) : m_pMedia(pVlcMedia)
{
init(bEventForwarding);
}
CVlcMedia::~CVlcMedia()
{
if (m_pMedia)
libvlc_media_release(m_pMedia);
}
void CVlcMedia::init(bool bEventForwarding)
{
// if caller don't want to use QT signals
if (bEventForwarding)
{
// attach event callbacks
m_pEventManager = libvlc_media_event_manager(m_pMedia);
libvlc_event_attach(m_pEventManager, libvlc_MediaParsedChanged, onParsedChanged, this);
}
}
void CVlcMedia::onParsedChanged(const libvlc_event_t* pEvent, void* pData)
{
CVlcMedia* pMedia = reinterpret_cast<CVlcMedia*>(pData);
emit pMedia->parsedChanged(static_cast<bool>(pEvent->u.media_parsed_changed.new_status));
}
libvlc_time_t CVlcMedia::duration()
{
return libvlc_media_get_duration(m_pMedia);
}
void CVlcMedia::parse()
{
libvlc_media_parse_with_options(m_pMedia, libvlc_media_parse_local, -1);
}

View File

@ -0,0 +1,35 @@
#ifndef VLC_MEDIA_H
#define VLC_MEDIA_H
#include "libvlc_base.h"
#include <QObject>
class CVlcMedia : public QObject
{
Q_OBJECT
public:
CVlcMedia(libvlc_instance_t* pVlcInstance, const QString& sFile, bool bEventForwarding = true);
CVlcMedia(libvlc_media_t* pVlcMedia, bool bEventForwarding = true);
virtual ~CVlcMedia();
void init(bool bEventForwarding);
public:
// libvlc callbacks
static void onParsedChanged(const libvlc_event_t *pEvent, void *pData);
public:
libvlc_time_t duration();
void parse();
signals:
void parsedChanged(bool bNewStatus);
public:
libvlc_media_t* m_pMedia;
libvlc_event_manager_t* m_pEventManager;
};
#endif // VLC_MEDIA_H

View File

@ -17,7 +17,7 @@ CVlcPlayer::CVlcPlayer()
// get event manager
m_pEventManager = libvlc_media_player_event_manager(m_pVlcPlayer);
// attach event listeners
// attach event callbacks
for (int nEvent = static_cast<int>(libvlc_MediaPlayerNothingSpecial); nEvent <= static_cast<int>(libvlc_MediaPlayerEncounteredError); nEvent++)
{
libvlc_event_attach(m_pEventManager, nEvent, onStateChanged, this);
@ -56,14 +56,14 @@ void CVlcPlayer::integrateIntoWidget(QWidget* pWidget)
#endif
}
void CVlcPlayer::open(libvlc_media_t* pMedia)
void CVlcPlayer::open(CVlcMedia* pMedia)
{
// stop playing old media
if (libvlc_media_player_is_playing(m_pVlcPlayer))
stop();
// set new media
libvlc_media_player_set_media(m_pVlcPlayer, pMedia);
libvlc_media_player_set_media(m_pVlcPlayer, pMedia->m_pMedia);
}
void CVlcPlayer::pause()
@ -104,11 +104,6 @@ libvlc_state_t CVlcPlayer::getState()
return libvlc_media_player_get_state(m_pVlcPlayer);
}
libvlc_instance_t* CVlcPlayer::getVlcInstance()
{
return m_pVlcInstance;
}
void CVlcPlayer::stop()
{
// stop the media player

View File

@ -2,8 +2,9 @@
#define VLC_PLAYER_H
#include "libvlc_base.h"
#include "vlcmedia.h"
#include "QWidget"
#include <QWidget>
class CVlcPlayer : public QWidget
{
@ -14,22 +15,20 @@ public:
virtual ~CVlcPlayer();
public:
// libvlc event callbacks
static void onStateChanged(const libvlc_event_t *pEvent, void *pData);
static void onTimeChanged(const libvlc_event_t *pEvent, void *pData);
public:
void integrateIntoWidget(QWidget* pWidget);
void open(libvlc_media_t* pMedia);
void open(CVlcMedia* pMedia);
void pause();
void play();
void stop();
void setVolume(int nVolume);
void setTime(int nTime);
bool isAudio();
libvlc_state_t getState();
libvlc_instance_t* getVlcInstance();
signals:
void stateChanged(libvlc_state_t newState);