Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the audio and video playback function of C++ Qt

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve audio and video playback function in C++ Qt. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Since the recently started Qt project requires video playback, I stepped in a lot of holes to avoid later, so I record the implementation process here.

Qt version 5.9 is based on Clear11 Qt core components and add-ons, please tick when installing, otherwise there may be a lack of video playback module in the project.

1. First create a new project and open the .pro project settings file

New project directory structure

two。 Add a module to the .pro file as shown below, which is the basic module for our audio and video playback. Other settings are added or modified according to your own project requirements, and ctrl+s saves the file after completion of modification.

QT + = multimedia multimediawidgets//QT + = core gui is the default for a new project. You don't need to add it manually.

Add Modul

3. Next, let's set up the interface (GUI). Double-click the .ui file under the Forms folder to open the GUI editing page as follows

Double-click the .ui file Push Button, Widget

At present, we do not do too complex functions, currently only need Push Button and Widget two kinds of controls

Push Button we use to control playback Widget is used to display video

4. Press and hold the left button of the above two controls and drag them into the right window interface.

Drag the control into the

Make some basic settings for the control in order to enhance readability, I have made some changes to the control name here, right-click the control that needs to be modified and select "change object name".

The button on the left has been renamed pushButton_set. The button on the right has been renamed pushButton_start.

The box above is Widget, and this control is basically transparent after being dragged in, only with a border outline, and we renamed it widget_video.

Ctrl+s saves the file

5. Set the playback window

Right-click the Widget control you just dragged in and click "promote to"

Here we need to upgrade Widget to QVideoWidget.

The class name and header file are written as QVideoWidget check the global include and then add and promote

6. Button signal settin

Right-click the first button and select "go to slot"

Select clicked () and click the OK button below

At this point we will jump to the code editing page

We also need to introduce some required header files before writing the code.

# include # include

Now let's create a global player variable

QMediaPlayer * player = new QMediaPlayer ()

Then write the button slot function just now.

Void MainWindow::on_pushButton_set_clicked () {player- > setMedia (QMediaContent (QUrl::fromLocalFile ("C:/Users/Administrator/Music/MV/test.mp4"); / / the file path here is the full path of the local video file. / / player is the newly created player variable player- > setVideoOutput (ui- > widget_video); / / widget_video is the name of the widget control we just dragged in.

After doing this, your source file .cpp should look like this.

Note that the video file path here should be filled in according to your video storage location.

If you copied the path directly from windows, it should be\ it may conflict with the C++ escape character\ at this time, then please add a\ or change\ to / before\.

Modify the direct copy path: C:\ Users\ Administrator\ Music\ MV\ test.mp4 to: C:\\ Users\\ Administrator\\ Music\\ MV\\ test.mp4 or: C:/Users/Administrator/Music/MV/test.mp4

7. Also follow the above procedure to write the slot function of the second button

Right-click the second button in the .ui file and select "go to slot" and select clicked ()

Jump to the slot function writing position to write the following:

Void MainWindow::on_pushButton_start_clicked () {player- > play ();}

The final two slot functions should be like this.

8. Compile and run

So far, the main part has been completed. Let's compile and run the software to see how it works.

The compilation went smoothly without the Error software running successfully. The window we just edited appears.

At this point, when the basic playback function is completed, you can see that the MV I downloaded to the local has been played.

9. Step on the pit to remind you

Click to start the playback does not respond and there is an error message in the Qt information bar: DirectShowPlayerService::doRender: Unresolved error code 0x80040266

This is because there is no DirectShow decoder in the system because Qt's video playback control is based on DirectShowPlayerService.

Solution: install a DirectShow decoder directly download and install one to the system such as: LAV Filters recompile and run

Error prompt: DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80004005 ()

Solution: put the video file in an all-English path with no spaces, modify the path and then recompile and run

So much for sharing the audio and video playback function of C++ Qt. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report