Skip to main content
GameDev.net gamedev.net
🔒 Locked

Playing .avi (c++)

Started by drummer1549 May 7, 2005 at 10:34 PM 2 replies 2.5k views
Original Post
drummer1549
drummer1549
hey this might be a dumb question but o well... im not a c++ programmer, i know the basics and thats about it but i thought what im doing would be best done in c++ if its not to complicated. i just want to make an .exe that plays a short .avi video ive made. Since im not a c++ programmer, please be specific. if its not to long or complicated, can someone please give me the code, or at least a reference to a tutorial if its more complicated, to play my video (logo.avi is the name of the file). Thanks for any help!
Going to Iraq without France is like going deer hunting without your accordion.
Prototype
Prototype
Here's a brief example of an application using DirectShow to play a movie. For simplicity it runs as a console application, if you want to get rid of the console window you should transform it into a Windows application (WinMain, message pump etc.) But I'll leave that up to you as an exercise :)
Good luck.

#pragma comment(lib, "strmiids.lib")#pragma comment(lib, "quartz.lib")#include <string>#include <dshow.h>int PlayVideo(const std::string& pFile){    IGraphBuilder* pGraph = NULL;    IVideoWindow* pWin = NULL;    IMediaControl* pControl = NULL;    IMediaEvent* pEvent = NULL;    // Initialize the COM library.    HRESULT hr = CoInitialize(NULL);    if (FAILED(hr)) return 1;    // Create the filter graph manager and query for interfaces.    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);    if (FAILED(hr)) return 1;	// Get interfaces    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);    hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWin);    // Build the graph (convert unicode filename)    size_t _size = mbstowcs(NULL, pFile.c_str(), 0) + 2; // Add 2 for ZT    wchar_t* _wfile = new wchar_t[_size+2];    _size = mbstowcs(_wfile, pFile.c_str(), pFile.length()+1); // Add 1 for ZT    hr = pGraph->RenderFile(_wfile, NULL);    delete[] _wfile;    // Uncomment next line for borderless window display    //pWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);    // For fullscreen play, get Windows screen parameters and replace    pWin->SetWindowPosition(0,0,800,600);    if (SUCCEEDED(hr))    {        hr = pControl->Run();        if (SUCCEEDED(hr))        {            long evCode;            pEvent->WaitForCompletion(INFINITE, &evCode);        }    }    pControl->Release();    pEvent->Release();    pWin->Release();    pGraph->Release();    CoUninitialize();    return 0;}int main(){    PlayVideo("test.avi"); // play any format Windows can handle, avi/mpg etc.    return 0;}


edit: fixed spacing
edit: forgot to mention you will need some DirectX SDK, or at least headers & libaries for DirectShow [pig]
So if you really need it simple you could also launch Windows Media Player:

#include <windows.h>int main(){    ShellExecute(NULL, "open", "mplayer2.exe", "\"test.avi\" /play /close /fullscreen", NULL, 0);    return 0;}


Well this got messy after all.

[Edited by - Prototype on May 8, 2005 12:38:00 AM]
drummer1549
drummer1549
thanks a lot! this helped so much. looks like my problems are solved. :)
Going to Iraq without France is like going deer hunting without your accordion.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.