Use DirectShow to play audio and video in WinCE

Source: Internet
Author: User

// ================================================ ========================================
// Title:
// Use DirectShow to play audio and video in WinCE
// Author:
// Norains
// Date:
// Monday 14-may-2007
// Environment:
// Wince 5.0
// ================================================ ========================================

Although there is not much information on how to use DirectShow to play multimedia files under wince on the internet, WinCE still belongs to Windows after all, while the DirectShow example of the desktop system comes from the Internet, in addition, the functional methods of DirectShow are not much different from those of wince, and I have no confidence that they are more gorgeous than their wheels. Therefore, this article will go straight into the topic, I will not introduce the structure function of DirectShow. Let's take a look at how to use it. (In fact, I am still a little lazy for some reasons, well, but this has little to do with the subject of this article :-)).

To facilitate code transplantation, I encapsulated the DirectShow operation into a cmedia class. As long as this class is called directly, DirectShow can be easily called to play multimedia files.

Okay. Now let's take a look at the specific code: // get the cmedia instance.
Cmedia * m_pmedia = cmedia: getinstance ();

// Set the playback window
M_pmedia-> setvideowindow (hwnd );

// Open a media file
M_pmedia-> open (text ("A. Avi "));

// Play
M_pmedia-> play ();

...

// Call close to release the resource after the playback ends.
M_pmedia-> open ();
That's right. It's just six lines of code. It's so easy to play media files smoothly. here, we need to call the setvideowindow () function to set the playing window because we play a video and need to display it in a window. the video playing window can be a normal window or a picture control. of course, if you play an audio file, you can ignore this function.

In addition, when open () is successfully called, you must call close () to release the resource before opening another media file. otherwise, unreleased resources may cause many inexplicable consequences.

Wait, the Code seems not perfect. For example, if I want to play another file after the file is played, how can I know when the file has been played? At this time, we need to ask for setpolicywindow ().

This function is used to set a message receiving window. When DirectShow has an event change, the specified message is sent to the specified window. The prototype is as follows:

Setpolicywindow (hwnd, uint wmsg, long linstancedata)

Hwnd: the handle to receive messages.

Wmsg: the specified custom message.

Linstancedata: the parameter of the message.


Now, the following code snippet is used to receive a Video Playback End event: // customize a message.
# Define wm_graphnotify (wm_user + 13)

// Set the receiving message window and message
M_pmedia-> setvideowindow (hwnd, wm_graphnotify, null );

...

// This is a message loop function.
Lresult cmainwnd: wndproc (hwnd, uint wmsg, wparam, lparam)
...{
Switch (wmsg)
...{

...

Case wm_graphpolicy:
...{
Long evcode, evparam1, evparam2;

// Obtain the DirectShow event at this time
If (m_pmedia-> getevent (& evcode, & evparam1, & evparam2) = true)
...{
If (evcode = ec_complete)
...{
MessageBox (null, text ("played"), text (""), mb_ OK );
}
}
Return 0;
}
}

...

}

Okay, it's that simple to know that the playback is complete. Well, it's still complicated ..? Oh, I think it's quite easy.

At the end of the article, let's take a look at several other useful functions of cmedia:

Checkvisibility ()
Description: determines the file type.
When the return value is true, it is a video file; otherwise, it is only an audio file.


Setvolume (long lvolume, long lbalance)
Description: sets the volume.
Lvolume: Set the volume in the range of-10,000 to 0.
Lbalance: set the balance between the left and right volume. The range is-10,000 to 10,000. The default value is 0.


Setdisplaymode (displaymode Mode)
Description: sets the playback mode.
Disp_fit: scales out to the screen window.
Disp_stretch: Do not scale to the screen window.
Disp_native: if the original video size is smaller than the screen size, it will be played as the original video file size. Otherwise, it will be the same as disp_fit
Disp_fullscreen: Full Screen

/**///////////////////////////////////// /////////////////////////////////
// Media. h: interface for the cmedia class.
//
// Version:
// 1.2.0
// Date:
// 2007.05.08
/**///////////////////////////////////// //////////////////////////////////
# Ifndef media_h
# Define media_h

# Include <mmsystem. h>
# Include <streams. h>

//--------------------------------------------------------------------
// Macro define

// The volume value
# Define max_volume 0
# Define min_volume-10000

// The balance value
# Define max_balance 10000
# Define min_balance-10000

//--------------------------------------------------------------------
// Enum Value

Enum displaymode
...{
// Fit to the play window size. How wide (height) the window is, how
// Is the move. Keep aspect ratio.
Disp_fit,

// Stretch to the play window size. Don't keep the aspect ratio.
Disp_stretch,

// Full screen play.
Disp_fullscreen,

// When the size of video is smaller than the play window, it displayes
// As the video size. If it's bigger, it just like the disp_fit mode.
Disp_native
};
//--------------------------------------------------------------------

// The media file Property
Typedef struct
...{
// The volume range is-10,000 to 0.
// Divide by 100 to get equivalent decibel value (for example-10,000 =-100 dB ).
Long lvolume;

// The value from-10,000 to 10,000 indicating the stereo balance
// As with the volume property, units correspond to. 01 decibels (multiplied by-1 when plbalance is a positive value ).
// For example, a value of 1000 indicates-10 dB on the right channel and-90 db on the left channel.
Long lbalance;

// Width of the video
Long lwidth;

// Height of the video
Long lheight;

// Approximate Bit Rate
Long lbitrate;

} Mediaproperty, * pmediaproperty;
//--------------------------------------------------------------------
Class cmedia
...{
Public:
Bool getevent (long * plevcode, long * plparam1, long * plparam2 );
Bool setnotifywindow (hwnd, uint wmsg, long linstancedata );
Bool setvolume (long lvolume, long lbalance = 0 );
Bool setdisplaymode (displaymode mode );
Bool getmediaproperty (pmediaproperty poutproperty );
Static cmedia * getinstance ();
Void close ();
Bool checkvisibility ();
Void setvideowindow (hwnd hwndvideo );
Bool open (tchar * pszfilename );
Bool stop ();
Bool pause ();
Bool play ();
Virtual ~ Cmedia ();

Protected:

Cmedia ();

// Collection of interfaces
Igraphbuilder * m_pgb;
Imediacontrol * m_pmc;
Imediaeventex * m_pme;
Ivideowindow * m_pvw;
Ibasicaudio * m_pba;
Ibasicvideo * m_pbv;
Imediaseeking * m_pms;

Tchar m_szfilename [max_path];
Hwnd m_hwndvideo; // The Window play video
Hwnd m_hwnd1_y; // The window between Y
Bool m_bexitthrd;
Bool m_bthrdrunning;
Static cmedia * m_pinstance;
Displaymode m_dispmode;

};

# Endif // # ifndef media_h

 

 

/**///////////////////////////////////// //////////////////////////////////
// Media. cpp: Implementation of the cmedia class.
//
/**///////////////////////////////////// //////////////////////////////////

# Include "stdafx. H"
# Include "media. H"

// Configure //----------------------------------------------------------------------------------------------
// Macro define

// Default play mode
# Define default_display_mode disp_native

//----------------------------------------------------------------------
// Initialize
Cmedia * cmedia: m_pinstance = NULL;
//------------------------------------------------------------------------

/**///////////////////////////////////// //////////////////////////////////
// Construction/destruction
/**///////////////////////////////////// //////////////////////////////////

Cmedia: cmedia ():
M_pgb (null ),
M_pmc (null ),
M_pme (null ),
M_pvw (null ),
M_pba (null ),
M_pbv (null ),
M_pms (null ),
M_hwndvideo (null ),
M_bexitthrd (true ),
M_bthrdrunning (false ),
M_dispmode (default_display_mode ),
M_hwnd1_y (null)
...{
Memset (m_szfilename, 0, sizeof (m_szfilename ));
}

Cmedia ::~ Cmedia ()
...{
If (m_pinstance! = NULL)
...{
Delete m_pinstance;
M_pinstance = NULL;
}

}

 

//------------------------------------------------------------
// Description:
// Play the media file
// When you call the function, you should call open () before.
//
//-------------------------------------------------------------
Bool cmedia: Play ()
...{
// Run the graph to play the media file

If (m_pmc = NULL)
...{
Return false;
}

M_pmc-> Run ();

Return true;
}

 

//------------------------------------------------------------
// Description:
// Pause.
// When you call the function, you should call open () before.
//
//-------------------------------------------------------------
Bool cmedia: Pause ()
...{

If (m_pmc = NULL)
...{
Return false;
}

M_pmc-> pause ();

Return true;
}

 

//------------------------------------------------------------
// Description:
// Stop.
// When you call the function, you should call open () before.
//
//-------------------------------------------------------------
Bool cmedia: Stop ()
...{

If (m_pmc = NULL | m_pms = NULL)
...{
Return false;
}


M_pmc-> stop ();
M_pms-> setpositions (0, am_seeking_absolutepositioning, null, am_seeking_nopositioning );

Return true;
}

 

//--------------------------------------------------------------------------
// Description:
// Open the media file. When succeed in calling the function,
// You shocould call the close () to release the resource
//
//-------------------------------------------------------------------------
Bool cmedia: open (tchar * pszfilename)
...{
Bool bresult = false;

If (_ tcslen (pszfilename)> = max_path)
...{
Goto end;
}
Else
...{
_ Tcscpy (m_szfilename, pszfilename );

// Check the file existing
Handle hdfile = createfile (m_szfilename, generic_read, file_assist_read, null, open_existing, null, null );
If (hdfile = invalid_handle_value)
...{
// The file doesn' t exist
Goto end;
}
Else
...{
Closehandle (hdfile );
}
}

 

// Initialize com
If (coinitializeex (null, coinit_multithreaded )! = S_ OK)
...{
Goto end;
}

// Get the interface for DirectShow's graphbuilder
If (cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server, iid_igraphbuilder, (void **) & m_pgb )! = S_ OK)
...{
Goto end;
}

// Have the Graph Construct its the appropriate graph automatically
If (m_pgb-> renderfile (m_szfilename, null )! = Noerror)
...{
Goto end;
}

// QueryInterface for DirectShow Interfaces
If (m_pgb-> QueryInterface (iid_imediacontrol, (void **) & m_pmc )! = Noerror)
...{
Goto end;
}
If (m_pgb-> QueryInterface (iid_imediaeventex, (void **) & m_pme )! = Noerror)
...{
Goto end;
}
If (m_pgb-> QueryInterface (iid_imediaseeking, (void **) & m_pms )! = Noerror)
...{
Goto end;
}

// Query for video interfaces, which may not be relevant for audio files
If (m_pgb-> QueryInterface (iid_ivideowindow, (void **) & m_pvw )! = Noerror)

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/norains/archive/2007/05/14/1609118.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.