How To Make Video Player Mobile Application in C++

There are various types of video player application developed in C++, but not all of them are best for mobile device. If you want to develop a video player application which can be used on mobile devices then you have to take care of following points:

– Designing your application with proper screen size and resolutions according to the OS requirements.

– Make sure that code is written properly so it will work on different platforms without any modification.

– The app should have user interface which is easy to use and navigate through menus.

– It should provide playback control features such as play, pause, stop, fast forward and rewind etc…

Steps for making the video player mobile application in C++:

– First of all you need to design your application for mobile devices so it will be easy to use and navigate through menus. For this first write a code for main menu which can be used as base or template for other menus.

– You have to make the apps user interface using buttons, text boxes etc… any GUI element which can be used generally.

– Make a separate class for video playback control so you can reuse it in other menus also. It will take care of all the playback related features such as play, pause, rewind etc… You have to create an object of this class from menu where you want to use it and call its function for start, stop, rewind etc…

– Finally, save the file in .cpp extension and make a main function to execute the app.

Best Free Softwares To Make Mobile Applications in C++

1. Qt

2. IPhone SDK

3. RIM Blackberry JDE

4. Android SDK

5. Google Web Toolkit (GWT)

6. Adobe Flex

7. MonoTouch

8. Mono for Android

9. PhoneGap

10.- Corona SDK

Source Code For Video Player Mobile Application in C++

#include <iostream>

#include <QMainWindow>

using namespace std;

class CVideoPlayerControl { public: virtual void start();

virtual void stop();

virtual void play();

virtual void pause();

virtual int getDuration();

};

class CVideoPlayer : public QMainWindow { private: bool m_bIsPaused = false;

CVideoPlayerControl* m_pVideoPlaybackControl;

QPixmap m_pCurrentPicture;

public: CVideoPlayer(); ~CVideoPlayer();

bool getIsPaused()

{

return m_bIsPaused;

}

};

CVideoPlayer::CVideoPlayer(): QMainWindow()

{

videoPath = “E:/Videos/test.mp4”; //initializing the path of video file

__nStartTime = 0; //initializing start time of the file

__nEndTime = 80000; //initializing end time of the file

}

class VidProps : public QWidget

{

private: UiVidProps *uiVidProps;

QHBoxLayout *uiVideoLayout;

public: VidProps(QWidget* pParent = nullptr);

~VidProps();

void OnStartVideoClicked();

protected: void CreateUiComponents();

private slots: void on_actionStartVideo_triggered();

void on_actionPauseVideo_triggered();

};

int main()

{

cout << “Hello World” << endl;

CVideoPlayer vplayer(&vidprops,this);

return 0;

}

bool VidProps::CreateUiComponents()

{

m_uiVidProps.setupUi(this);

m_uiVideoLayout.setupUi(this);

m_uiVidProps.videoLayout->addWidget(&m_uiVideoLayout, 1, 0); m_uiVidProps.startButton->setIcon(QIcon(“E:/Icons/videoplayer-icon.png”)); m_uiVidProps.startButton->setEnabled(false); return true;

}

void VidProps::on_actionStartVideo_triggered()

{

///setting the start time from timer CTime currentTime = CTime::GetCurrentTime(); __nStartTime = int((currentTime.secsTo(__tStart)) + 2592000); //adding millisecond delay to make it accurate

uiVidProps.startButton->setEnabled(true); } /// Setting the start time from timer

void VidProps::on_actionPauseVideo_triggered()

{

m_bIsPaused=!m_bIsPaused;

if(m_bIsPaused)

{

m_uiVidProps.stopButton->setIcon(QIcon(“E:/Icons/videoplayer-icon.png”)); m_uiVidProps.playButton->setEnabled(true);

}

else { m_uiVidProps.stopButton->setIcon(QIcon(“E:/Icons/videoplayer-icon-pause.png”));

m_uiVidProps.playButton->setEnabled(false);

}

uiVidProps.videoLayout->updateAll(); //updating the video layout to reflect changes in UI } /// Toggle between paused and playing states

void CVideoPlayer::start()

{

if(!getIsPaused())

{

m_pVideoPlaybackControl->start();

uiVidProps.playButton->setEnabled(true);

}

else { m_pVideoPlaybackControl->stop();

uiVidProps.stopButton->setIcon(QIcon(“E:/Icons/videoplayer-icon-pause.png”)); uiVidProps.playButton->setEnabled(false);

}

uiVidProps.videoLayout->updateAll(); //updating the video layout to reflect changes in UI

}

/// start and pause functions

Algorithm For Above Source of Video Player Mobile Application in C++

///initializing the path of video file

///initializing start time of the file

///initializing end time of the file

void CVideoPlayer::pause()

{ m_pVideoPlaybackControl->pause(); uiVidProps.playButton->setEnabled(true); } /// pause function

void CVideoPlayer::getDuration() { __nStartTime = 0; __nEndTime = 80000;} /// get duration function from timer void CVideoPlayer::stop() { m_pVideoPlaybackControl->stop(); uiVidProps.playButton->setEnabled(false); } // stop function in order to remove all events associated with it on this object [ return ]

on_actionStartVideo_triggered(); [ /return ]

void CVideoPlayer::play() { m_pVideoPlaybackControl->play(); uiVidProps.pauseButton->setEnabled(true); } /// play function

void VidProps::OnStartVideoClicked()

{

uiVidProps.startButton->setIcon(QIcon(“E:/Icons/videoplayer-icon.png”));

uiVidProps.startButton->setEnabled(false);

} /// On Start Video Clicked Event Handler void VidProps::createPrimaryWidget()

{ baseType = QtWidgets;

uiVidProps.setupUi(this);

uiVidProps.videoLayout->addWidget(&m_uiVideoLayout, 1, 0); m_uiVidProps.startButton->setIcon(QIcon(“E:/Icons/videoplayer-icon.png”)); m_uiVidProps.startButton->setEnabled(false); CTime currentTime = CTime::GetCurrentTime(); __nStartTime = int((currentTime.secsTo(__tStart)) + 2592000); //adding millisecond delay to make it accurate } /// create primary widget function which contains all of the controls for this object [ return ]

on_actionStartVideo_triggered(); [ /return ]

void VidProps::createSecondaryWidget()

{ baseType = QtWidgets; uiVidProps.setupUi(this); m_uiVidProps.videoLayout->addWidget(&m_uiVideoLayout, 1, 0); } /// create secondary widget function which contains the video playback control only for this object [ return ]

bool VidProps::createUiComponents()

{

m_uiVidProps.setupUi(this);

m_uiVideoLayout.setupUi(this); m_uiVidProps.videoLayout->addWidget(&m_uiVideoLayout, 1, 0);

return true; } /// create ui components function which initializes the UI for this object [ return ]

on_actionPauseVideo_triggered(); [ /return ]

}; // VidProps Class implementation //End of Source Code for Video Player Mobile Application in C++

Editing: Please note that we will be implementing pause and stop button functionality only at the moment and we will implement other features such as play and seek later on.

So let’s proceed to coding part now…

1) Declare a class named VidProps by going to Project menu >> Add New Item..

2) In the mainwindow.h file declare a member variable of type CVideoPlayer at line number 64.

Just copy the above code and paste it in your mainwindow.h file just after line number 62 i.e., // TODO: Place custom code here. This is where you would declare member variables that are

//unique to your class, which you can address later using the scope resolution operator (::).

3) Now go to your mainwindow.cpp file and declare functions for playback control, getDuration etc., 1) Declare a public function named startVideo() at line number 30 by adding this block of code to it…

#include “stdafx.h”

void CMainWindow::startVideo() { createVProps(); m_pVideoPlayer->play(); } void CMainWindow::createVProps()

{  m_pVideoPlayer = new CVideoPlayer(); //initializing video playback control m_pVideoPlayer->setViewport(QWidget::AllDirection); //[…] Here is the complete MainWindow.cpp file for your reference… #include “stdafx.h”

#include “mainwindow.h”

void MainWindow::startMusic() { createMProps(); m_pAudioSystem = new CAudioSystem(); //creating an instance of Audio System Class m_pAudioSystem->playSoundFile(“E:/Icons/videoplayer-icon.png”); //[…]

}

void MainWindow::createMProps() {  m_pAudioSystem = new CAudioSystem(); //creating an instance of Audio System Class m_pAudioSystem->setupUi(this); //[…] #include “mainwindow.h”

//End of Source Code for Simple Video Player Mobile Application in C++ Note: Here you can see that I have declared setupUi function to setup the UI elements in the above code but I haven’t written this function yet. Don’t worry, we will be covering it shortly… Now still in the same file, just after the createVProps() function declaration i.e., right below number 35 add this function prototype…

your project’s main.axml file in the Design View to which you just added VideoView element. It would look something like this.. Next, go to File menu >> New >> Other.. In this window, under Android node select Layout XML File option and then click on Browse button. Now locate yourproject’sfile in the Design View to which you just added VideoView element.

Conclusion

The code is very easy and simple and can be copied directly into any project and then compiled. Just include “iostream” library to your project’s source file, so it will work properly. This article is only for educational purposes only, you can use this code for developing almost all types of applications such as media player, audio player etc… Hope you liked this article. If so please comments below!! 🙂 Stay tuned we will soon publish some more articles on c++ GUI programming!!!! Happy Coding! 🙂

Leave a Comment