LibVLC 미디어 재생기 프로그래밍 방법 C++, QT

by 엉뚱도마뱀 posted Apr 20, 2018
?

Shortcut

PrevPrev Article

NextNext Article

ESCClose

Larger Font Smaller Font Up Down Go comment Print

비디오 재생기 만들때 참조하세요.

  1. #include <QtGlobal>
  2. #include <QtDebug>
  3. #include <cassert>
  4. #include "VLCMediaPlayer.h"
  5. #include "VLCInstance.h"
  6. #include "VLCMedia.h"
  7. using namespace LibVLCpp;
  8. MediaPlayer::MediaPlayer() : m_media( NULL )
  9. {
  10.     m_internalPtr = libvlc_media_player_new( LibVLCpp::Instance::getInstance()->getInternalPtr() );
  11.     // Initialize the event manager
  12.     p_em = libvlc_media_player_event_manager( m_internalPtr );
  13.     registerEvents();
  14. }
  15. MediaPlayer::MediaPlayer( Media* media ) : m_media( media )
  16. {
  17.     m_internalPtr = libvlc_media_player_new_from_media( media->getInternalPtr() );
  18.     // Initialize the event manager
  19.     p_em = libvlc_media_player_event_manager( m_internalPtr );
  20.     registerEvents();
  21. }
  22. MediaPlayer::~MediaPlayer()
  23. {
  24.     libvlc_event_detach( p_em, libvlc_MediaPlayerSnapshotTaken, callbacks, this );
  25.     libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, callbacks, this );
  26.     libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, callbacks, this );
  27.     libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, callbacks, this );
  28.     libvlc_event_detach( p_em, libvlc_MediaPlayerStopped, callbacks, this );
  29.     libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, callbacks, this );
  30.     libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this );
  31.     stop();
  32.     libvlc_media_player_release( m_internalPtr );
  33. }
  34. void
  35. MediaPlayer::registerEvents()
  36. {
  37.     // Register the callback
  38.     libvlc_event_attach( p_em, libvlc_MediaPlayerSnapshotTaken,   callbacks, this );
  39.     libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged,     callbacks, this );
  40.     libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying,         callbacks, this );
  41.     libvlc_event_attach( p_em, libvlc_MediaPlayerPaused,          callbacks, this );
  42.     libvlc_event_attach( p_em, libvlc_MediaPlayerStopped,         callbacks, this );
  43.     libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached,      callbacks, this );
  44.     libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this );
  45.     libvlc_event_attach( p_em, libvlc_MediaPlayerLengthChanged,   callbacks, this );
  46.     libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError,callbacks, this );
  47.     libvlc_event_attach( p_em, libvlc_MediaPlayerPausableChanged, callbacks, this );
  48.     libvlc_event_attach( p_em, libvlc_MediaPlayerSeekableChanged, callbacks, this );
  49. }
  50. /**
  51.  * Event dispatcher.
  52.  */
  53. void
  54. MediaPlayer::callbacks( const libvlc_event_t* event, void* ptr )
  55. {
  56.     MediaPlayer* self = reinterpret_cast<MediaPlayer*>( ptr );
  57.     switch ( event->type )
  58.     {
  59.     case libvlc_MediaPlayerPlaying:
  60.         //qDebug() << "Media player playing";
  61.         self->emit playing();
  62.         break;
  63.     case libvlc_MediaPlayerPaused:
  64.         //qDebug() << "Media player paused";
  65.         self->emit paused();
  66.         break;
  67.     case libvlc_MediaPlayerStopped:
  68.         //qDebug() << "Media player stopped";
  69.         self->emit stopped();
  70.         break;
  71.     case libvlc_MediaPlayerEndReached:
  72.         //qDebug() << "Media player end reached";
  73.         self->emit endReached();
  74.         break;
  75.     case libvlc_MediaPlayerTimeChanged:
  76.         self->emit timeChanged( event->u.media_player_time_changed.new_time );
  77.         break;
  78.     case libvlc_MediaPlayerPositionChanged:
  79.         //qDebug() << self << "position changed : " << event->u.media_player_position_changed.new_position;
  80.         self->emit positionChanged( event->u.media_player_position_changed.new_position );
  81.         break;
  82.     case libvlc_MediaPlayerLengthChanged:
  83.         self->emit lengthChanged( event->u.media_player_length_changed.new_length );
  84.         break;
  85.     case libvlc_MediaPlayerSnapshotTaken:
  86.         self->emit snapshotTaken( event->u.media_player_snapshot_taken.psz_filename );
  87.         break;
  88.     case libvlc_MediaPlayerEncounteredError:
  89.         qDebug() << '[' << (void*)self << "] libvlc_MediaPlayerEncounteredError received."
  90.                 << "This is not looking good...";
  91.         self->emit errorEncountered();
  92.         break;
  93.     case libvlc_MediaPlayerSeekableChanged:
  94.         // TODO: Later change it to an event that corresponds volume change, when this thing gets fixed in libvlc
  95.         self->emit volumeChanged();
  96.         break;
  97.     case libvlc_MediaPlayerPausableChanged:
  98.     case libvlc_MediaPlayerTitleChanged:
  99.     case libvlc_MediaPlayerNothingSpecial:
  100.     case libvlc_MediaPlayerOpening:
  101.     case libvlc_MediaPlayerBuffering:
  102.     case libvlc_MediaPlayerForward:
  103.     case libvlc_MediaPlayerBackward:
  104.     default:
  105. //        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
  106.         break;
  107.     }
  108. }
  109. void
  110. MediaPlayer::play()
  111. {
  112.     //qDebug() << "Asking for play media player";
  113.     libvlc_media_player_play( m_internalPtr );
  114. }
  115. void
  116. MediaPlayer::pause()
  117. {
  118.     libvlc_media_player_pause( m_internalPtr );
  119. }
  120. void
  121. MediaPlayer::stop()
  122. {
  123.     //qDebug() << "Asking for stop media player";
  124.     libvlc_media_player_stop( m_internalPtr );
  125. }
  126. int
  127. MediaPlayer::getVolume()
  128. {
  129.     int volume = libvlc_audio_get_volume( m_internalPtr );
  130.     return volume;
  131. }
  132. int
  133. MediaPlayer::setVolume( int volume )
  134. {
  135.     //Returns 0 if the volume was set, -1 if it was out of range
  136.     return libvlc_audio_set_volume( m_internalPtr, volume );
  137. }
  138. qint64
  139. MediaPlayer::getTime()
  140. {
  141.     qint64 t = libvlc_media_player_get_time( m_internalPtr );
  142.     return t;
  143. }
  144. void
  145. MediaPlayer::setTime( qint64 time )
  146. {
  147.     libvlc_media_player_set_time( m_internalPtr, time );
  148. }
  149. float
  150. MediaPlayer::getPosition()
  151. {
  152.     return libvlc_media_player_get_position( m_internalPtr );
  153. }
  154. void
  155. MediaPlayer::setPosition( float pos )
  156. {
  157.     libvlc_media_player_set_position( m_internalPtr, pos );
  158. }
  159. qint64
  160. MediaPlayer::getLength()
  161. {
  162.     return libvlc_media_player_get_length( m_internalPtr );
  163. }
  164. void
  165. MediaPlayer::takeSnapshot( const char* outputFile, unsigned int width, unsigned int heigth )
  166. {
  167.     libvlc_video_take_snapshot( *this, 0, outputFile, width, heigth );
  168. }
  169. bool
  170. MediaPlayer::isPlaying()
  171. {
  172.     return ( libvlc_media_player_is_playing( m_internalPtr ) == 1 );
  173. }
  174. bool
  175. MediaPlayer::isSeekable()
  176. {
  177.     return ( libvlc_media_player_is_seekable( m_internalPtr ) == 1 );
  178. }
  179. void
  180. MediaPlayer::setDrawable( void* drawable )
  181. {
  182. #if defined ( Q_WS_MAC )
  183.     libvlc_media_player_set_nsobject( m_internalPtr, drawable );
  184. #elif defined ( Q_OS_UNIX )
  185.     libvlc_media_player_set_xwindow( m_internalPtr, reinterpret_cast<intptr_t>( drawable ) );
  186. #elif defined ( Q_OS_WIN )
  187.     libvlc_media_player_set_hwnd( m_internalPtr, drawable );
  188. #endif
  189. }
  190. void
  191. MediaPlayer::setMedia( Media* media )
  192. {
  193.     libvlc_media_player_set_media( m_internalPtr, media->getInternalPtr() );
  194. }
  195. void
  196. MediaPlayer::getSize( quint32 *outWidth, quint32 *outHeight )
  197. {
  198.     libvlc_video_get_size( m_internalPtr, 0, outWidth, outHeight );
  199. }
  200. float
  201. MediaPlayer::getFps()
  202. {
  203.     return libvlc_media_player_get_fps( m_internalPtr );
  204. }
  205. void
  206. MediaPlayer::nextFrame()
  207. {
  208.     libvlc_media_player_next_frame( m_internalPtr );
  209. }
  210. bool
  211. MediaPlayer::hasVout()
  212. {
  213.     return libvlc_media_player_has_vout( m_internalPtr );
  214. }
  215. const QString&
  216. MediaPlayer::getLoadedFileName() const
  217. {
  218.     return m_media->getFileName();
  219. }
  220. QString
  221. MediaPlayer::getLoadedMRL()
  222. {
  223.     Media::internalPtr     media = libvlc_media_player_get_media( m_internalPtr );
  224.     char* str = libvlc_media_get_mrl( media );
  225.     return QString( str );
  226. }
  227. int
  228. MediaPlayer::getNbAudioTrack()
  229. {
  230.     int res = libvlc_audio_get_track_count( m_internalPtr );
  231.     return res;
  232. }
  233. int
  234. MediaPlayer::getNbVideoTrack()
  235. {
  236.     int res = libvlc_video_get_track_count( m_internalPtr );
  237.     return res;
  238. }
  239. void
  240. MediaPlayer::setKeyInput( bool enabled )
  241. {
  242.     libvlc_video_set_key_input( m_internalPtr, enabled );
  243. }
  244.  
TAG •

Articles

1 2 3 4 5 6