Hi,
I am using Windows Media Encoder Series 9 for encoding videos. When I try to Encode a File, sometimes the player hangs is
WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING state and the OnStateChange event doesnt get triggered after this..... the code is listed below,
///<summary>
///
///</summary>
///<param name="source"></param>
///<param name="destination"></param>
///<param name="mode"></param>
publicvoid EncodeFile(
string source, string destination,uint mode )
{
sourceFile = source;
groupCollection= encoder.SourceGroupCollection;
group = groupCollection.Add("Philips");
videoSource = (IWMEncVideoSource)group.AddSource(
WMENC_SOURCE_TYPE.WMENC_VIDEO);
videoSource.SetInput(source, "","");
// Set the bitrate for Small Size rendering to 1000000
int bitRate = (int)ProfileSettings.BitRate.Low;
if( mode == (uint)ExportConstants.CompressionMode.Normal )
{
// Set the bitrate for Normal Size rendering to 2200000
bitRate = (int)ProfileSettings.BitRate.Medium;
}
if( mode == (uint)ExportConstants.CompressionMode.HighQuality )
{
// Set the bitrate for High Quality rendering to 4400000
bitRate = (int)ProfileSettings.BitRate.High;
}
//Create a new WMEncProfile and set the properties
IWMEncProfile2 profile2 = new WMEncProfile2();
//Set validate mode to true so that profile is validated after settings
profile2.ValidateMode = true;
//Set the Profile name
profile2.ProfileName = CustomProfileName;
profile2.ProfileDescription = CustomProfileName;
//Set the content type to 16 for Video
profile2.ContentType = 16;
//WMENC_SOURCE_TYPE.WMENC_VIDEO - Video and
//WMENC_PROFILE_VBR_MODE.WMENC_PVM_NONE - Constant Bit Rate Encoding
profile2.set_VBRMode(WMENC_SOURCE_TYPE.WMENC_VIDEO , 0,
WMENC_PROFILE_VBR_MODE.WMENC_PVM_NONE);
//Create a new audience with the specified bitrate
profile2.AddAudience(bitRate);
//To store the video codec name
object vVidCodecName;
//To store the FOURCC value of the video codec
int lVid4cc = -1;
bool success = false;
for ( int i = 0; i < profile2.VideoCodecCount && !success; ++i )
{
lVid4cc = profile2.EnumVideoCodec(i, out vVidCodecName);
//Compare if the current codec is Windows Media Video 9 Screen
if ( vVidCodecName.ToString().Equals(CodecName) )
{
success = true;
}
}
//Validate the profile
profile2.Validate();
//Set the video width, height and bitrate for the audience
int numAudiences = profile2.AudienceCount;
for( int i =0; i < numAudiences; ++i )
{
IWMEncAudienceObj aud = profile2.get_Audience(i);
aud.set_VideoHeight(0,0);
aud.set_VideoWidth(0,0);
aud.set_VideoBitrate(0, bitRate);
}
group.set_Profile(profile2);
IWMEncFile output= encoder.File;
output.LocalFileName = destination;
group.PrepareToEncode(true);
encoder.OnStateChange +=new _IWMEncoderEvents_OnStateChangeEventHandler(OnStat eChanged);
encoder.OnError += new _IWMEncoderEvents_OnErrorEventHandler(
OnError);
encoder.Start();
}
///<summary>
///
///</summary>
///<param name="code"></param>
privatevoid OnError( int code )
{
try
{
encoder.OnError -= new _IWMEncoderEvents_OnErrorEventHandler(
OnError);
if( groupCollection != null )
{
group.RemoveSource(WMENC_SOURCE_TYPE.WMENC_VIDEO,
videoSource);
groupCollection.Remove(group);
File.Delete(sourceFile );
groupCollection = null;
group = null;
videoSource = null;
}
}
catch( IOException )
{
}
catch( ExternalException )
{
}
}
privatevoid OnStateChanged(WMENC_ENCODER_STATE state)
{
if( state == WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED )
{
try
{
encoder.OnStateChange -=new _IWMEncoderEvents_OnStateChangeEventHandler(OnStat eChanged);
encoder.Stop();
if( groupCollection != null )
{
group.RemoveSource(WMENC_SOURCE_TYPE.WMENC_VIDEO,
videoSource);
groupCollection.Remove(group);
File.Delete(sourceFile );
groupCollection = null;
group = null;
videoSource = null;
}
}
catch( IOException )
{
}
catch( ExternalException )
{
}
}
}
A quick response to this problem will help me a lot....
Thanks
Srihari S
I am using Windows Media Encoder Series 9 for encoding videos. When I try to Encode a File, sometimes the player hangs is
WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING state and the OnStateChange event doesnt get triggered after this..... the code is listed below,
///<summary>
///
///</summary>
///<param name="source"></param>
///<param name="destination"></param>
///<param name="mode"></param>
publicvoid EncodeFile(
string source, string destination,uint mode )
{
sourceFile = source;
groupCollection= encoder.SourceGroupCollection;
group = groupCollection.Add("Philips");
videoSource = (IWMEncVideoSource)group.AddSource(
WMENC_SOURCE_TYPE.WMENC_VIDEO);
videoSource.SetInput(source, "","");
// Set the bitrate for Small Size rendering to 1000000
int bitRate = (int)ProfileSettings.BitRate.Low;
if( mode == (uint)ExportConstants.CompressionMode.Normal )
{
// Set the bitrate for Normal Size rendering to 2200000
bitRate = (int)ProfileSettings.BitRate.Medium;
}
if( mode == (uint)ExportConstants.CompressionMode.HighQuality )
{
// Set the bitrate for High Quality rendering to 4400000
bitRate = (int)ProfileSettings.BitRate.High;
}
//Create a new WMEncProfile and set the properties
IWMEncProfile2 profile2 = new WMEncProfile2();
//Set validate mode to true so that profile is validated after settings
profile2.ValidateMode = true;
//Set the Profile name
profile2.ProfileName = CustomProfileName;
profile2.ProfileDescription = CustomProfileName;
//Set the content type to 16 for Video
profile2.ContentType = 16;
//WMENC_SOURCE_TYPE.WMENC_VIDEO - Video and
//WMENC_PROFILE_VBR_MODE.WMENC_PVM_NONE - Constant Bit Rate Encoding
profile2.set_VBRMode(WMENC_SOURCE_TYPE.WMENC_VIDEO , 0,
WMENC_PROFILE_VBR_MODE.WMENC_PVM_NONE);
//Create a new audience with the specified bitrate
profile2.AddAudience(bitRate);
//To store the video codec name
object vVidCodecName;
//To store the FOURCC value of the video codec
int lVid4cc = -1;
bool success = false;
for ( int i = 0; i < profile2.VideoCodecCount && !success; ++i )
{
lVid4cc = profile2.EnumVideoCodec(i, out vVidCodecName);
//Compare if the current codec is Windows Media Video 9 Screen
if ( vVidCodecName.ToString().Equals(CodecName) )
{
success = true;
}
}
//Validate the profile
profile2.Validate();
//Set the video width, height and bitrate for the audience
int numAudiences = profile2.AudienceCount;
for( int i =0; i < numAudiences; ++i )
{
IWMEncAudienceObj aud = profile2.get_Audience(i);
aud.set_VideoHeight(0,0);
aud.set_VideoWidth(0,0);
aud.set_VideoBitrate(0, bitRate);
}
group.set_Profile(profile2);
IWMEncFile output= encoder.File;
output.LocalFileName = destination;
group.PrepareToEncode(true);
encoder.OnStateChange +=new _IWMEncoderEvents_OnStateChangeEventHandler(OnStat eChanged);
encoder.OnError += new _IWMEncoderEvents_OnErrorEventHandler(
OnError);
encoder.Start();
}
///<summary>
///
///</summary>
///<param name="code"></param>
privatevoid OnError( int code )
{
try
{
encoder.OnError -= new _IWMEncoderEvents_OnErrorEventHandler(
OnError);
if( groupCollection != null )
{
group.RemoveSource(WMENC_SOURCE_TYPE.WMENC_VIDEO,
videoSource);
groupCollection.Remove(group);
File.Delete(sourceFile );
groupCollection = null;
group = null;
videoSource = null;
}
}
catch( IOException )
{
}
catch( ExternalException )
{
}
}
privatevoid OnStateChanged(WMENC_ENCODER_STATE state)
{
if( state == WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED )
{
try
{
encoder.OnStateChange -=new _IWMEncoderEvents_OnStateChangeEventHandler(OnStat eChanged);
encoder.Stop();
if( groupCollection != null )
{
group.RemoveSource(WMENC_SOURCE_TYPE.WMENC_VIDEO,
videoSource);
groupCollection.Remove(group);
File.Delete(sourceFile );
groupCollection = null;
group = null;
videoSource = null;
}
}
catch( IOException )
{
}
catch( ExternalException )
{
}
}
}
A quick response to this problem will help me a lot....
Thanks
Srihari S