Skip to content

Video Player

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.

  • 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
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.

Starts or resumes video playback.

m_videoPlayer->play();

Pauses the video.

m_videoPlayer->pause();

Stops playback and resets internal state.

m_videoPlayer->stop();

Seeks to a specified position in milliseconds.

m_videoPlayer->setMediaPosition(5000); // 5 seconds

Initializes the media player instance if it was previously destroyed. Also reattaches the video surface and restores last playback state.

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)

Fetches and logs all available metadata from the media file.

m_videoPlayer->getMetaData();
PropertyTypeDescription
videoSurfaceQAbstractVideoSurface*Surface used for rendering frames.
positionintCurrent playback position (ms).
durationintTotal media duration (ms).
stateQMediaPlayer::StateCurrent playback state.
void setVideoSurface(QAbstractVideoSurface *surface)
Section titled “void setVideoSurface(QAbstractVideoSurface *surface)”

Attaches a new video rendering surface to the media player.

Updates the current playback position.

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 received
m_surface->present(frame);
void onMediaStatusChanged(QMediaPlayer::MediaStatus status)
Section titled “void onMediaStatusChanged(QMediaPlayer::MediaStatus status)”

Handles changes in media status like buffering, loaded, etc.

Sets the current playback position and emits positionChanged.

Sets media duration and emits durationChanged.

Sets player state and emits stateChanged.

SignalDescription
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.
m_videoPlayer->play();
m_videoPlayer->pause();
m_videoPlayer->stop();
m_videoPlayer->setMediaPosition(120000); // Jump to 2 minutes
m_videoPlayer->destroyMediaPlayer();
m_videoPlayer->initMediaPlayer();
m_videoPlayer->getMetaData(); // Logs all metadata keys & values
QObject::connect(m_videoPlayer, &VideoPlayer_C::positionChanged, [](int pos){
qDebug() << "Position: " << pos;
});

The constructor attempts to automatically resolve a path like:

/rw_data/sample_video.mp4

or

$HOME/sample_video.mp4

It uses getcwd() and conditional compilation with OS_EMBEDDED to resolve it appropriately.

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:

  • VideoOutput is a built-in QML type that renders video frames.
  • source: VideoPlayer_Q connects it to a video player object.
  • When you call VideoPlayer_Q.play(), the video will appear inside this VideoOutput.