VLC-SDK Call Learning

Source: Internet
Author: User
vlc-sdk Call Learning

VLC belongs to a full open source streaming media server and multimedia player in the video LAN open source project organization. As a streaming media server, VLC Cross-platform support for multiple operating systems and computer architectures; As a multimedia player, VLC can play multiple formats of media files. Mainly include: WMV, ASF, MPG, MP, AVI, H.264 and many other common media formats.

By viewing the official document, learning the next VLC simple call interface, the implementation of a music player, in fact, is quite simple.

This deliberately encapsulates a class of VLC calls:

Qvlcplayer

#ifndef qvlcplayer_h #define QVLCPLAYER_H #include <QObject> typedef void (* pfnposchanged) (void *data, int iPos)  ;
File position change struct libvlc_instance_t;
struct libvlc_media_player_t;

struct libvlc_event_t;
    Class Qvlcplayer:public QObject {q_object public:explicit qvlcplayer (QObject *parent = 0);

    ~qvlcplayer ();                     BOOL Play (QString strpath);                                   The playback path is strpath file void play ();                                   Play void Pause ();                                   pause void Stop ();                         Stop//void Playpreviouse ();                              Play on one//void PlayNext ();                          Play the next void Volume (int nvol);                          Volume is set to Nvol void Volumeincrease ();                            Volume increase void volumereduce ();                          Volume reduction void seekto (int npos);                   Jumps to the specified position NPOs void Seekforward ();          Fast forward void Seekbackward ();            Fast retreat void Setcallback (pfnposchanged pfn);                    Sets the callback function when the file location changes pfnposchanged getcallback ();                               Gets the callback function when the file location changes bool IsOpen ();                            Whether the file is open bool IsPlaying ();                              Whether the file is playing bool Ispause ();                               Whether to pause int getpos ();                               Gets the location where the file is currently playing Qint64 GetTime ();                  Gets the time void Setplaytime (Qint64 times);                             Set playback time Qint64 getlength ();                            Gets the length int getplaystatue ();

Gets the playback state const char* getlibvlcversion ();             private:libvlc_instance_t *m_pvlc_inst;           VLC instance libvlc_media_player_t *m_pvlc_player;                   VLC player pfnposchanged M_PFN;                          The callback function QString M_filepath when the file position is changed;   File path void Init ();  Initialize QString UnicodeToUTF8 (const QString &strwide);                                 Code conversion void Release ();

Clean memory};
 #endif//Qvlcplayer_h

QVlcPlayer.cpp

#include "qvlcplayer.h" #include "vlc/vlc.h" #include "vlc/libvlc_media.h" #include <qmath.h> #include <
qstringlist> #include <QTextCodec>//VLC event management void onvlc_endreached (const libvlc_event_t *event, void *data);

void onvlc_positionchanged (const libvlc_event_t *event, void *data); Qvlcplayer::qvlcplayer (QObject *parent): QObject (parent), m_pvlc_inst (null), M_pvlc_player (null), M

_PFN (NULL) {}//Qvlcplayer::~qvlcplayer () {this->release ();}
    Play media bool Qvlcplayer::P Lay (QString strpath) {if (!m_pvlc_inst) {this->init ();
    } if (Strpath.isempty () | | |!m_pvlc_inst) {return false;

    } this->stop ();
    BOOL BRet = false;

libvlc_media_t *m;
#if defined (q_os_win) M_filepath = Strpath.replace ("/", "\ \", qt::casesensitive);
#elif defined (q_os_linux) M_filepath = strpath;

    #endif//Encode the path of the file (no translation can not be recognized in Chinese, which will result in errors) M_filepath = UnicodeToUTF8 (M_filepath); m = Libvlc_mediA_new_path (M_pvlc_inst,m_filepath.toascii ());
        if (m) {M_pvlc_player = Libvlc_media_player_new_from_media (m);
            if (M_pvlc_player) {libvlc_media_player_play (M_pvlc_player);
            Event Management libvlc_event_manager_t *vlc_evt_man = Libvlc_media_player_event_manager (M_pvlc_player);
            Libvlc_event_attach (Vlc_evt_man, libvlc_mediaplayerendreached,:: onvlc_endreached, this);
            Libvlc_event_attach (Vlc_evt_man, libvlc_mediaplayerpositionchanged,:: Onvlc_positionchanged, this);
        BRet = true;
    } libvlc_media_release (M);
return bRet;
    } void Qvlcplayer::P lay () {if (M_pvlc_player) {libvlc_media_player_play (M_pvlc_player);
    }//Pause void Qvlcplayer::P ause () {if (M_pvlc_player) {libvlc_media_player_pause (M_pvlc_player);    Stop playing media void Qvlcplayer::stop () {if (M_pvlc_player) {libvlc_media_player_stop (M_pvlc_player); /*stopplaying*/libvlc_media_player_release (M_pvlc_player);
    /*free the media_player*/m_pvlc_player = NULL; 
    } void Qvlcplayer::volume (int nvol) {if (M_pvlc_player) {libvlc_audio_set_volume (M_pvlc_player,nvol); }///Increase volume void qvlcplayer::volumeincrease () {if (m_pvlc_player) {int nvol = Libvlc_audio_get_volu
        Me (M_pvlc_player);
    Volume ((int) ceil (NVOL * 1.1)); }///Decrease Volume void qvlcplayer::volumereduce () {if (m_pvlc_player) {int nvol = Libvlc_audio_get_volume (M_PV
        Lc_player);
    Volume ((int) floor (Nvol * 0.9)); } void Qvlcplayer::seekto (int npos) {if (M_pvlc_player) {libvlc_media_player_set_position (m_pvlc_play
    Er, npos/(float) 10000.0);
    }//Fast forward void Qvlcplayer::seekforward () {int npos = GetPos ();
    Seekto (Ceil (NPOs * 1.1));
Seekto (NPOs + 10);
    }//fast rollback void Qvlcplayer::seekbackward () {int npos = GetPos ();
    Seekto (Floor (NPOs * 0.9)); Seekto (nPos-10);

} void Qvlcplayer::setcallback (Pfnposchanged pfn) {m_pfn = PFN;}

Pfnposchanged Qvlcplayer::getcallback () {return M_PFN;}

BOOL Qvlcplayer::isopen () {return NULL!= M_pvlc_player;}
    BOOL Qvlcplayer::isplaying () {if (M_pvlc_player) {return libvlc_media_player_is_playing (M_pvlc_player);
return FALSE;
    BOOL Qvlcplayer::ispause () {bool tem;
        if (m_pvlc_player) {int state = Libvlc_media_player_get_state (M_pvlc_player);
            Switch (state) {case Libvlc_paused:case Libvlc_stopped:tem = true;
        Break
        Default:break;
    }else{tem = FALSE;
return tem; //Get the location where the file is currently playing int qvlcplayer::getpos () {if (M_pvlc_player) {return (int) * Libvlc_media_player_ge
    T_position (M_pvlc_player));
return 0; //Get Time: The current playback time Qint64 Qvlcplayer::gettime () {if (M_pvlc_player) {return Libvlc_mediA_player_get_time (M_pvlc_player);
return 0; } void Qvlcplayer::setplaytime (Qint64 time) {if (M_pvlc_player) {libvlc_media_player_set_time (M_PVLC_PL
    Ayer,time); //Get Length: Total time length of current media qint64 Qvlcplayer::getlength () {if (M_pvlc_player) {return libvlc_media_player_ge
    T_length (M_pvlc_player);
return 0; }/* typedef enum LIBVLC_STATE_T {libvlc_nothingspecial = 0, libvlc_opening, libvlc_buffering, Libvlc_pla
Ying, libvlc_paused, libvlc_stopped, libvlc_ended, libvlc_error} libvlc_state_t; */int Qvlcplayer::getplaystatue () {if (m_pvlc_inst) {return libvlc_media_player_get_state M_pvlc_player
    );
return 0;
    const char *qvlcplayer::getlibvlcversion () {if (m_pvlc_inst) {return libvlc_get_version ();

} return "";}
    VLC instance initializes void Qvlcplayer::init () {if (!m_pvlc_inst) {m_pvlc_inst = Libvlc_new (0,null);

} M_filepath = "";} //unicode Turn UTF8 QString qvlcplayer::unicodetoutf8 (const QString &strwide) {QString StrUtf8;
    Qstringlist T;
    Qtextcodec *codec = Qtextcodec::codecforname ("UTF-8");
    for (int i = 0; I <strwide.length (); i+=4) {T.append (Strwide.mid (i,4));
    foreach (const QString &AMP;STR, T) {strutf8.append (str);
Return Codec->fromunicode (STRUTF8);

    } void Qvlcplayer::release () {this->stop ();
        if (m_pvlc_inst) {libvlc_release (m_pvlc_inst);
    M_pvlc_inst = NULL; } void onvlc_endreached (const libvlc_event_t *event,void *data) {} void onvlc_positionchanged (const libvlc_event_t * event, void *data) {switch (event->type) {case libvlc_mediaplayerpositionchanged: {float fPo
        s = event->u.media_player_position_changed.new_position;

        Qvlcplayer *pavplayer = (Qvlcplayer *) data;

 if (pavplayer) {pfnposchanged PFN = Pavplayer->getcallback ();           if (PFN) {PFN (pavplayer, int (FPOS * 100));
    }} break;
    Case Libvlc_mediaplayersnapshottaken:break;
    Default:break;
 }
}

The above code is the core part of the player, the code annotation is very detailed, completely can understand. And VLC is cross-platform, so this can be run in Linux, Mac, only the corresponding dynamic library can be. On the compilation of different platforms VLC Dynamic library, the official website has detailed introduction, here will not do too much introduction.

About this player interface, the implementation is also very simple, reference to my imitation of the cool dog music face, can be achieved.

Finished the player very want to let it run on my arm board, but in compiling VLC library, encountered a lot of problems, had to terminate, in the heart or some of the regrets. There are strange problems on the way to compiling the arm version Dynamic Library, and these problems online information is also very few, the reason is because the VLC relies on the library too much, a lot of error hint is missing the corresponding library, installed on these libraries and then remind a file missing header file, it is a headache. It seems that we can only turn to the MPlayer player.

A screenshot After the configure, this step counts for success.

In the subsequent make process, there are plenty of bugs.

If any great God compiled the arm version of the VLC library, still very much hope to be able to exchange, appreciate.

Technology is to communicate and share ...
Blog address: http://blog.csdn.net/u013704336\
email:kevinlq0912@163.com
qq:936563422

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.