简体   繁体   English

WMEncoder在一次迭代后抛出OutOfMemoryException

[英]WMEncoder throws OutOfMemoryException after one iteration

I'm using WMEncoder for screen recording. 我正在使用WMEncoder进行屏幕录制。
At the first time everything is working properly, 一切正常,
but at the second time Start() method throws me OutOfMemoryException : 但在第二次Start()方法抛出OutOfMemoryException

System.OutOfMemoryException was caught
  HResult=-2147024882
  Message=Not enough storage is available to complete this operation.

My code looks like this and it's on .Net4: 我的代码看起来像这样,它在.Net4上:

// Initialize encoder and set recording parameters
mEncoder = new WMEncoder();
SetRecordingParams(); // If it's relevant I can attach this function

// Set the output file.
mEncoder.EnableAutoArchive = true;
mEncoder.AutoIndex = true;
mEncoder.File.LocalFileName = tempRecFile;

// Start the encoding process.
mEncoder.PrepareToEncode(true);
mEncoder.Start();

// If currently recording, stop recording
if (mEncoder != null &&
    mEncoder.RunState != WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED)
{

    // Stop recording
    mEncoder.Stop();
}

// Releasing Com object
if (mEncoder != null)
{
    Marshal.FinalReleaseComObject(mEncoder);
    mEncoder = null;
}

Help me! 帮我!

UPDATE UPDATE

 private void SetRecordingParams()
        {
            // Create a source group collection object from the WMEncoder object.
            srcGrpColl = mEncoder.SourceGroupCollection;

            // Add a source group named SG1 to the collection.
            // Create a source object for each type of multimedia content
            // in the source group.
            srcGrp = (IWMEncSourceGroup2)srcGrpColl.Add("SG_1");
            srcVideo = (IWMEncVideoSource2)srcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
            srcVideo.SetInput("ScreenCap://ScreenCapture1", "", "");

            // Create a profile collection object from the WMEncoder object.
            mEncoder.ProfileCollection.ProfileDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            mEncoder.ProfileCollection.Refresh();
            proColl = mEncoder.ProfileCollection;

            // Create a profile object
            IEnumerator profEnum = proColl.GetEnumerator();
            while (profEnum.MoveNext())
            {
                profile = (IWMEncProfile)profEnum.Current;
                if (profile.Name == "Screen Recording")
                {
                    // Load profile
                    newProfile = new WMEncProfile2();
                    newProfile.LoadFromIWMProfile(profile);

                    audience = newProfile.get_Audience(0);

                    audience.set_VideoFPS(0, paramMaps.fpsMapping[fpsKey] * 1000);
                    audience.set_VideoKeyFrameDistance(0, keyFrameInt * 1000);
                    audience.set_VideoWidth(0, Screen.PrimaryScreen.Bounds.Width * paramMaps.imageQualityMapping[qualityRatioKey] / 100);
                    audience.set_VideoHeight(0, Screen.PrimaryScreen.Bounds.Height * paramMaps.imageQualityMapping[qualityRatioKey] / 100);

                    // Set profile language to client machine's locale.
                    // When recording is done this way, it will assume server's locale when extracted from the DB.
                    // This enables us to know which locale should be used for the file merge.
                    // We have found that when profile is set to the same language as user's locale, the recording
                    // has a "flexible" language definition.
                    int langCount = newProfile.get_LanguageCount(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0);
                    // Remove all existing language definitions from profile
                    for (int i = 0; i < langCount; i++)
                    {
                        newProfile.RemoveLanguage(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0, newProfile.get_Language(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0, i));
                    }
                    // Add current locale as profile language.
                    int lcid = Thread.CurrentThread.CurrentCulture.LCID;
                    newProfile.AddLanguage(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0, lcid);

                    // Specify this profile object as the profile to use in source group.
                    srcGrp.set_Profile(newProfile);
                }
            }
            mEncoder.VideoComplexity = WMENC_VIDEOCOMPLEXITY.WMENC_COMPLEXITY_LEVEL20;
        }

WMEncoder is no longer supported by Microsoft apparently. 显然,微软不再支持WMEncoder。 I would recommend looking into the Expression Encoder which make screen capture very easy indeed - http://www.microsoft.com/expression/products/Encoder4_Overview.aspx . 我建议查看Expression Encoder,它可以非常轻松地捕获屏幕 - http://www.microsoft.com/expression/products/Encoder4_Overview.aspx And it saves you the hassle of marshalling and dealing with COM. 它为您省去了编组和处理COM的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM