美文网首页
vlc播放器--linux

vlc播放器--linux

作者: Caiaolun | 来源:发表于2020-03-06 15:33 被阅读0次

大神地址: https://blog.csdn.net/leixiaohua1020/article/details/42363079

VLC API: https://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__media__player.html

安装库:

sudo apt-get install libvlc-dev
sudo apt-get install vlc
class MediaPlayer : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MediaPlayer(QWidget *parent = 0);
    ~MediaPlayer();

private slots:
    void OnVideoPlayButtonClicked();
    void OnVideoStopButtonClicked();

    void OnProgressSliderValueChanged(int value);
    void OnVolumeSliderValueChanged(int value);

    void OnActionOpenTriggered();
    void UpdateUserInterface();
    
private:
    Ui::MediaPlayer *       ui;

    libvlc_instance_t*      m_pInstance;
    libvlc_media_player_t*  m_pVlcPlayer;

    int                     image_width;
    int                     image_height;
    DisplayWidget*          m_pDisplayWidget;

    MediaPlayStatus         m_eMediaPlayStatus;

    QTimer*                 m_updateTimer;

private:
    void MediaPlayerSetDrawableWindow(libvlc_media_player_t* player);
    void MediaPlayerPlay();
    void MediaPlayerStop();
    void ShowFrame(QImage image);
    int GetImageWidth(){return image_width;}
    int GetImageHeight(){return image_height;}

    static void* lockCallback(void *opaque, void **plane);
    static void unlockCallback(void *opaque, void *picture, void *const *plane);
    static void displayCallback(void *opaque, void *picture);
};
#include <QFileDialog>
#include <QDebug>
#include <QMutex>
#include <QTime>

#include "mediaplayer.h"
#include "ui_mediaplayer.h"

unsigned char* video_callback_outBuffer=NULL;
QMutex      buff_Mutex;

MediaPlayer::MediaPlayer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MediaPlayer)
{
    ui->setupUi(this);

    m_pInstance=libvlc_new(0,NULL);

    if(m_pInstance ==NULL){
        qDebug()<<"Instance NULL";

    }else{
        qDebug()<<"Instance call ok...";
    }

    image_width=1280;
    image_height=720;

    m_pVlcPlayer=NULL;

    m_eMediaPlayStatus=MEDIA_STATUS_STOPED;

    connect(ui->playButton,SIGNAL(clicked()),this,SLOT(OnVideoPlayButtonClicked()));
    connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(OnVideoStopButtonClicked()));

    ui->progressSlider->setMaximum(1000);
    ui->volumeSlider->setMaximum(100);

    connect(ui->progressSlider,SIGNAL(valueChanged(int)),this,SLOT(OnProgressSliderValueChanged(int)));
    connect(ui->volumeSlider,SIGNAL(valueChanged(int)),this,SLOT(OnVolumeSliderValueChanged(int)));

    connect(ui->actionOpen,SIGNAL(triggered()),this,SLOT(OnActionOpenTriggered()));

    QVBoxLayout* vLayout= new QVBoxLayout(ui->videoWidget);
    vLayout->setContentsMargins(0,0,0,0);

    m_pDisplayWidget= new DisplayWidget();
    vLayout->addWidget( m_pDisplayWidget);
//    ui->videoWidget->setLayout(vLayout);

    video_callback_outBuffer= (unsigned char*)malloc(MAX_VIDE_OUTBUFFER_SIZE);

    m_updateTimer=new QTimer(this);

    connect(m_updateTimer,SIGNAL(timeout()),this,SLOT(UpdateUserInterface()));

}

MediaPlayer::~MediaPlayer()
{

    if(m_pVlcPlayer != NULL){
        libvlc_media_player_release(m_pVlcPlayer);
         m_pVlcPlayer=NULL;

    }
    if(m_pInstance != NULL){
        libvlc_release(m_pInstance);
        m_pInstance=NULL;
    }

    if(m_updateTimer !=NULL){
        m_updateTimer->stop();
        m_updateTimer=NULL;
    }

    if(m_pDisplayWidget != NULL){
        delete m_pDisplayWidget;
        m_pDisplayWidget=NULL;
    }
    if(video_callback_outBuffer != NULL){
        free(video_callback_outBuffer);
        video_callback_outBuffer=NULL;
    }
    delete ui;
}

void MediaPlayer::OnVideoPlayButtonClicked()
{
    MediaPlayerPlay();
}
void MediaPlayer::OnVideoStopButtonClicked()
{
    MediaPlayerStop();
}

void MediaPlayer::OnProgressSliderValueChanged(int value)
{
    if(m_pVlcPlayer ==NULL){
        return;
    }

    if(libvlc_media_player_is_playing(m_pVlcPlayer))
    {
        float pos=(float)value/(float)ui->progressSlider->maximum();
        libvlc_media_player_set_position(m_pVlcPlayer,pos);
    }
}

void MediaPlayer::OnVolumeSliderValueChanged(int value)
{
    if(m_pVlcPlayer ==NULL){
        return;
    }

    if(libvlc_media_player_is_playing(m_pVlcPlayer))
    {
        int volume=value;
        libvlc_audio_set_volume(m_pVlcPlayer,volume);
    }

}
void MediaPlayer::OnActionOpenTriggered()
{
    if(m_pInstance ==NULL){
        return;
    }
    if(m_pVlcPlayer!=NULL){
        MediaPlayerStop();
    }
    QString filePath=QFileDialog::getOpenFileName(this,tr("Open media file"),".",tr("Media Files (*.avi *.mp4 *.mkv *.ogg *.mp3 *.wma *.wma)"));
    if(filePath.length() !=0)
    {
        qDebug()<<"PATH:::"<<filePath;
        filePath=QDir::toNativeSeparators(filePath);
        libvlc_media_t* media=libvlc_media_new_path(m_pInstance,filePath.toUtf8().data());
        m_pVlcPlayer=libvlc_media_player_new_from_media(media);
        libvlc_media_release(media);

        MediaPlayerSetDrawableWindow(m_pVlcPlayer);

        MediaPlayerPlay();

    }
}
void MediaPlayer::UpdateUserInterface()
{

    if(m_pVlcPlayer==NULL){
        return;
    }

    if(libvlc_media_player_is_playing(m_pVlcPlayer))
    {
        int progressPos=libvlc_media_player_get_position(m_pVlcPlayer)*ui->progressSlider->maximum();
        int volumePos=libvlc_audio_get_volume(m_pVlcPlayer);


        bool states=ui->progressSlider->blockSignals(true);
        ui->progressSlider->setValue(progressPos);
        ui->progressSlider->blockSignals(states);

        bool statesVolume=ui->volumeSlider->blockSignals(true);
        ui->volumeSlider->setValue(volumePos);
        ui->volumeSlider->blockSignals(statesVolume);

        QTime startTime;
        startTime=startTime.addMSecs(libvlc_media_player_get_time(m_pVlcPlayer));
        ui->startTimeLabel->setText(startTime.toString("hh:mm:ss"));

        QTime remainTime;
        remainTime=remainTime.addMSecs(libvlc_media_player_get_length(m_pVlcPlayer) -libvlc_media_player_get_time(m_pVlcPlayer));
        ui->endTimeLabel->setText(remainTime.toString("hh:mm:ss"));

    }
}

void MediaPlayer::MediaPlayerSetDrawableWindow(libvlc_media_player_t *player)
{
    libvlc_video_set_callbacks(player,lockCallback,unlockCallback,displayCallback,this);
    libvlc_video_set_format(player,"RGBA",image_width,image_height,image_width*4);
#if defined(Q_OS_WIN32)
    libvlc_media_player_set_hwnd(player,(void*)ui->videoWidget->winId());
#elif defined(Q_OS_MAC)
    //libvlc_media_player_set_nsobject(player,(void*)ui->videoWidget->winId());

#elif defined(Q_OS_UNIX)
    libvlc_media_player_set_xwindow(player,(int)ui->videoWidget->winId());
#endif

    if(m_updateTimer!=NULL){

        m_updateTimer->start(100);
    }
}

void MediaPlayer::MediaPlayerPlay()
{
    if(m_pVlcPlayer ==NULL){
        return;
    }

    if(m_eMediaPlayStatus==MEDIA_STATUS_PLAY)
    {
        libvlc_media_player_pause(m_pVlcPlayer);
        m_eMediaPlayStatus=MEDIA_STATUS_PAUSE;
    }
    else if(m_eMediaPlayStatus==MEDIA_STATUS_PAUSE)
    {
        libvlc_media_player_play(m_pVlcPlayer);
        m_eMediaPlayStatus=MEDIA_STATUS_PLAY;

    }
    else{
        libvlc_media_player_play(m_pVlcPlayer);
        m_eMediaPlayStatus=MEDIA_STATUS_PLAY;
    }



}
void MediaPlayer::MediaPlayerStop()
{
    if(m_pVlcPlayer!=NULL)
    {
        libvlc_media_player_release(m_pVlcPlayer);
        m_pVlcPlayer=NULL;
        m_eMediaPlayStatus=MEDIA_STATUS_STOPED;
    }
}

void* MediaPlayer::lockCallback(void *opaque, void **plane)
{
    buff_Mutex.lock();
    *plane=video_callback_outBuffer;
    return NULL;
}

void MediaPlayer::unlockCallback(void *opaque, void *picture, void *const *plane)
{
    MediaPlayer* player=(MediaPlayer*)opaque;
    if(player ==NULL){
        return;
    }
    int width=player->GetImageWidth();
    int height=player->GetImageHeight();
    QImage image((unsigned char*)video_callback_outBuffer,width,height,QImage::Format_ARGB32);
    player->ShowFrame(image.rgbSwapped());

    buff_Mutex.unlock();

    return;
}

void MediaPlayer::displayCallback(void *opaque, void *picture)
{
    return;
}
void MediaPlayer::ShowFrame(QImage image)
{
    if(m_pDisplayWidget !=NULL){
        m_pDisplayWidget->SetDisplayImage(image);
    }
}
```d

相关文章

网友评论

      本文标题:vlc播放器--linux

      本文链接:https://www.haomeiwen.com/subject/nrbirhtx.html