Video Player
Introduction
Section titled “Introduction”The VideoPlayer_C module enables video playback functionality using Qt’s QMediaPlayer, with support for rendering via a QAbstractVideoSurface. This class provide functionality to integrate video player in qml.
Supported Features:
Section titled “Supported Features:”- Video playback: .mp4, .avi, etc.
- Play / Pause / Stop control
- Duration and position tracking
- Surface rendering via QAbstractVideoSurface
- Metadata extraction
- QML integration with Q_PROPERTY and Q_INVOKABLE
- Dynamic creation and destruction of media player
Initialization
Section titled “Initialization”VideoPlayer_C* m_videoPlayer = std::make_shared<AudioVideoPlayer_CPlayer_C>(this);m_engine->rootContext()->setContextProperty("VideoPlayer_Q", m_videoPlayer.get());Internally, the constructor sets the media to a sample file like /rw_data/sample_video.mp4.
Public API Functions
Section titled “Public API Functions”void play()
Section titled “void play()”Starts or resumes video playback.
m_videoPlayer->play();void pause()
Section titled “void pause()”Pauses the video.
m_videoPlayer->pause();void stop()
Section titled “void stop()”Stops playback and resets internal state.
m_videoPlayer->stop();void setMediaPosition(int newPosition)
Section titled “void setMediaPosition(int newPosition)”Seeks to a specified position in milliseconds.
m_videoPlayer->setMediaPosition(5000); // 5 secondsvoid initMediaPlayer()
Section titled “void initMediaPlayer()”Initializes the media player instance if it was previously destroyed. Also reattaches the video surface and restores last playback state.
void destroyMediaPlayer()
Section titled “void destroyMediaPlayer()”Destroys the player and frees resources. Useful for switching contexts or releasing video hardware.
void setFormat(int width, int height, int format)
Section titled “void setFormat(int width, int height, int format)”Sets the video format for the rendering surface. (Currently not fully used in source code)
void getMetaData()
Section titled “void getMetaData()”Fetches and logs all available metadata from the media file.
m_videoPlayer->getMetaData();Properties (Q_PROPERTY)
Section titled “Properties (Q_PROPERTY)”| Property | Type | Description |
|---|---|---|
videoSurface | QAbstractVideoSurface* | Surface used for rendering frames. |
position | int | Current playback position (ms). |
duration | int | Total media duration (ms). |
state | QMediaPlayer::State | Current playback state. |
void setVideoSurface(QAbstractVideoSurface *surface)
Section titled “void setVideoSurface(QAbstractVideoSurface *surface)”Attaches a new video rendering surface to the media player.
void onPositionChanged(qint64 value)
Section titled “void onPositionChanged(qint64 value)”Updates the current playback position.
void onDurationChangeed(qint64 value)
Section titled “void onDurationChangeed(qint64 value)”Updates the media duration.
void onStateChanged(QMediaPlayer::State value)
Section titled “void onStateChanged(QMediaPlayer::State value)”Updates the player’s internal state.
void onNewVideoContentReceived(const QVideoFrame &frame)
Section titled “void onNewVideoContentReceived(const QVideoFrame &frame)”Receives raw frames and pushes them to the video surface.
// Called when a new frame is receivedm_surface->present(frame);void onMediaStatusChanged(QMediaPlayer::MediaStatus status)
Section titled “void onMediaStatusChanged(QMediaPlayer::MediaStatus status)”Handles changes in media status like buffering, loaded, etc.
void setPosition(int position)
Section titled “void setPosition(int position)”Sets the current playback position and emits positionChanged.
void setDuration(int duration)
Section titled “void setDuration(int duration)”Sets media duration and emits durationChanged.
void setState(QMediaPlayer::State state)
Section titled “void setState(QMediaPlayer::State state)”Sets player state and emits stateChanged.
Signals
Section titled “Signals”| Signal | Description |
|---|---|
void frameFinished() | Emitted when a video frame is completed. |
void positionChanged(int) | Emitted when playback position updates. |
void durationChanged(int) | Emitted when total video duration changes. |
void stateChanged(State) | Emitted on play/pause/stop state change. |
Usage Example
Section titled “Usage Example”Basic Video Playback
Section titled “Basic Video Playback”m_videoPlayer->play();Pause / Stop
Section titled “Pause / Stop”m_videoPlayer->pause();m_videoPlayer->stop();Seek to Position
Section titled “Seek to Position”m_videoPlayer->setMediaPosition(120000); // Jump to 2 minutesDynamic Initialization / Destruction
Section titled “Dynamic Initialization / Destruction”m_videoPlayer->destroyMediaPlayer();m_videoPlayer->initMediaPlayer();Metadata Logging
Section titled “Metadata Logging”m_videoPlayer->getMetaData(); // Logs all metadata keys & valuesConnecting to Signals
Section titled “Connecting to Signals”QObject::connect(m_videoPlayer, &VideoPlayer_C::positionChanged, [](int pos){ qDebug() << "Position: " << pos;});Default Video Path Resolution
Section titled “Default Video Path Resolution”The constructor attempts to automatically resolve a path like:
/rw_data/sample_video.mp4or
$HOME/sample_video.mp4It uses getcwd() and conditional compilation with OS_EMBEDDED to resolve it appropriately.
Usage of VideoPlayer_Q with VideoOutput
Section titled “Usage of VideoPlayer_Q with VideoOutput”In this code, you’re using the VideoOutput element to display video content from a backend media player object named VideoPlayer_Q.
VideoOutput { id: videoOut source: VideoPlayer_Q anchors.fill: parent }What’s Happening:
VideoOutputis a built-in QML type that renders video frames.- source:
VideoPlayer_Qconnects it to a video player object. - When you call
VideoPlayer_Q.play(), the video will appear inside thisVideoOutput.