Play video(of all format ), mp3 with C and C++


Play video with C and C++

To play any video with C and C++ we will take advantage of the function ‘system‘ provided in the header <cstdlib>(<stdlib.h>).To this function we will pass the appropriate command and the video or mp3 file name you want it to play.

The programs work in C and C++ and also in all the compiler DEV,Code::blocks, Visual Studio but it is meant only for Windows,for Linux and iOS user it is coming soon!

Running the code

Before running the program make sure that you transfer the media file you want to play to the directory where the source code of this program(given below) is present,namely where your main.cpp or main.c is present.After transferring the media file run the program.The first one is C++ source code and the second program is C source code.A video tutorial is also given at the end.

Code example: C++ source code

#include <iostream>
#include <cstdlib>

using namespace std ;

int main( )
{
system( “start video_name.mp4” );

cin.get( );
return 0;
}

Code example: C source code

#include <stdio>
#include <stdlib.h>

int main( )
{
system( “start video_name.mp4” );

getchar( );
return 0;
}

Replace ‘video_name.mp4‘ with the name of your media file with it’s type:mp4 or avi or mp3 .

We can also play video from any directory but to do this we have to pass the directory plus the video name you want to play.For instance if your video (say video.mp4) is present in the directory “C:\video” then pass the value “C:\video\video.mp4” instead of just the video name.The ‘system()’ function call will look like this.

Code example

system( “start C:\video\video.mp4” );

If the video doesn’t play try writing the directory in this format “C:/video/video.mp4“.

Note we are not literally playing the media with C or C++,we are only invoking the file(mp3 or video file) to be executed.The file is actually played with the appropriate software that is installed in your system for instance VLC media player or the Window’s media player.

You can read more about the ‘system‘ function here.

Link :C++ stdlib system function