开发者

Finding duration of a video using directshowlib-2005

My asp.net(c#) method looks as follows:

    static public bool GetVideoLength(string fileName, out double length)
    {
        DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph();
        DirectShowLib.IGraphBuilder graphBuilder;
        DirectShowLib.IMedia开发者_如何学运维Position mediaPos;
        length = 0.0;

        try
        {
            graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
            graphBuilder.RenderFile(fileName, null);
            mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
            mediaPos.get_Duration(out length);

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            mediaPos = null;
            graphBuilder = null;
            graphFilter = null;
        }
    }

I got the duration with the above method. But my problem is i can't delete the physical file after my operation. I used

File.Delete(FilePath);

While performing this action i got an exception as follows:

"The process cannot access the file because it is being used by another process."

My Os is windows 7(IIS 7)

Any one please help me to sort this out?


I've got no experience in coding directshow apps in C#, but plenty of experience in C++.

DirectShow is based on a technology called COM - which uses reference counting to tell it when an object is in use.

It would use a COM object to represent the IGraphBuilder for example.

In C++, we would have to deconstruct the graph, by removing all its filters, then release the graph.

I understand that C# has its own garbage collection etc., but unless you explicitly release the objects you use, they'll remain in memory.

It seems from the code you've quoted, that the graph is still opened, even though playback may have finished. In that case, it'll hold a reference to the file which you've played back, which would explain why you can't delete it - e.g. there's a read lock on the file.

Hope this points you in the right direction!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜