c#中用DirectShow实现媒体播放器的核心(0) 完成后的代码

c#可以开发多媒体软件吗?当然能。复杂吗?当然不复杂。资源占用严重吗?当然不严重。和普通cpp + com开发有什么不同吗?没什么不同。一定要用DirectShow开发的吗?随你高兴爱用不用。为什么不选cpp而是选c#进行媒体开发呢?为什么(why)的问题请去询问哲学家,我只回答你怎么样(how)的问题。能稍微解释下代码的意思吗?没看到标题index==0嘛,当然要做做前戏啦,哪有第0节就开始正题的,东方人是很矜持滴。
 

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Diagnostics;
  5using DirectShowLib;
  6
  7namespace SoundPlayer
  8{
  9    public class SoundPlayer
 10    {
 11        IFilterGraph2 graphBuilder;
 12        IMediaSeeking mediaSeeking;
 13        IMediaControl mediaControl;
 14        IBasicAudio audioControl;
 15        IVideoWindow window;
 16        Stack<int> errorStack = new Stack<int>();
 17        bool isValidate = false;
 18        public bool Validate
 19        {
 20            get return isValidate; }
 21        }

 22
 23        string fileName = string.Empty;
 24        public SoundPlayer( string file)
 25        {
 26            graphBuilder = (new FilterGraph()) as IFilterGraph2;
 27            if (graphBuilder == nullreturn;
 28            mediaControl = graphBuilder as IMediaControl;
 29            mediaSeeking = graphBuilder as IMediaSeeking;
 30            audioControl = graphBuilder as IBasicAudio;
 31            if (mediaControl == null || mediaSeeking == null || audioControl == nullreturn;
 32            int hr = mediaControl.RenderFile(file);
 33            fileName = file;
 34            if (hr != 0) errorStack.Push(hr);
 35            if (hr != 0return;
 36            mediaSeeking.SetTimeFormat(TimeFormat.MediaTime);
 37            window  = graphBuilder as IVideoWindow;
 38            
 39            isValidate = true;
 40        }

 41        public bool Play()
 42        {
 43            Debug.Assert(isValidate);
 44            int hr = mediaControl.Run();
 45            if (hr != 0) errorStack.Push(hr);
 46            return (hr == 0);
 47        }

 48        public bool Stop()
 49        {
 50            Debug.Assert(isValidate);
 51            int hr = mediaControl.Stop();
 52            this.CurrentPosition = 0;
 53            if (hr != 0) errorStack.Push(hr);
 54            window.put_Visible(OABool.False);
 55            return (hr == 0);
 56        }

 57        public bool Pause()
 58        {
 59            Debug.Assert(isValidate);
 60            int hr = mediaControl.Pause();
 61            if (hr != 0) errorStack.Push(hr);
 62            return (hr == 0);
 63        }

 64
 65        public long CurrentPosition
 66        {
 67            get
 68            {
 69                Debug.Assert(isValidate);
 70                long cur = -1;
 71                int hr = mediaSeeking.GetCurrentPosition(out cur);
 72                if (hr != 0) errorStack.Push(hr);
 73                return cur / (10000);
 74            }

 75            set
 76            {
 77                Debug.Assert(isValidate);
 78                long stopPosition = -1;
 79                int hr = mediaSeeking.GetStopPosition(out stopPosition);
 80                if (hr != 0) errorStack.Push(hr);
 81                hr = mediaSeeking.SetPositions(value * 10000, AMSeekingSeekingFlags.AbsolutePositioning,
 82                    stopPosition, AMSeekingSeekingFlags.AbsolutePositioning);
 83                if (hr != 0) errorStack.Push(hr);
 84            }

 85        }

 86
 87        public void SetOwner(IntPtr ptr)
 88        {
 89            if (window != null)
 90            {
 91                window.put_Owner(ptr);
 92
 93            }

 94        }

 95
 96        public string CurrentFile
 97        {
 98            get
 99            {
100                return fileName;
101            }

102        }

103
104        public long Duration
105        {
106            get
107            {
108                Debug.Assert(isValidate);
109                long dur = -1;
110                int hr = mediaSeeking.GetDuration(out dur);
111                if (hr != 0) errorStack.Push(hr);
112                return dur / 10000;
113            }

114        }

115
116        public double Rate
117        {
118            get
119            {
120                Debug.Assert(isValidate);
121                double rate = 0.0;
122                int hr = mediaSeeking.GetRate(out rate);
123                if (hr != 0) errorStack.Push(hr);
124                return rate;
125            }

126            set
127            {
128                Debug.Assert(isValidate);
129                int hr = mediaSeeking.SetRate(value);
130                if (hr != 0) errorStack.Push(hr);
131            }

132        }

133
134        public int Volume
135        {
136            get
137            {
138                Debug.Assert(isValidate);
139                int volume = 0;
140                int hr = audioControl.get_Volume(out volume);
141                if (hr != 0) errorStack.Push(hr);
142                return volume;
143            }

144            set
145            {
146                Debug.Assert(isValidate);
147                int hr = audioControl.put_Volume(value);
148                if (hr != 0) errorStack.Push(hr);
149            }

150        }

151
152        public int GetLastErrorCode()
153        {
154            if (errorStack.Count != 0return errorStack.Pop();
155            else return 0;
156        }

157
158    }

159}

160


 

posted @ 2007-05-10 19:58 gussing 阅读(3255) 评论(15)  编辑 收藏 网摘

  回复  引用    
#1楼2007-05-11 11:01 | lollipop[未注册用户]
期待後續文章,PS,討厭驗證碼
  回复  引用    
#2楼2007-09-11 09:34 | 脚印儿[未注册用户]
你好,我是directshow初学者,打算用c#开发,请问你是用哪个版本的sdk实现的?现在新出SDK的好象都没有directshow
  回复  引用  查看    
#3楼[楼主]2007-09-17 14:11 | gussing      
@脚印儿
你好,托管代码版的DirectShow应该是不存在的,至少现在没有
我用的是开源的lib : DirectShowLib,实际上它只是一个壳子,把重要的数据结构和接口用c#重新定义一遍,指定好各GUID,这些事情要自己做也很容易,就是很麻烦,DirectShowLib帮我们做掉了那实在很好,重复发明轮子的工作就不需要了。

  回复  引用  查看    
#4楼2008-06-10 15:45 | 杨欣      
好东西,恐怕整个因特网只有你这篇有关这方面知识的文章了!!!非常值得顶!!!
  回复  引用    
#5楼2008-11-21 13:44 | zhfj[未注册用户]
你好,我也是用DirectShowLib开发,但我做的是视频采集,我要实现视频显示到窗口,保存到文件以及采集图片功能,但有的摄像头不支持Still Pin,程序就不能采集图片,不知道你是否遇到过这个问题
  回复  引用  查看    
#6楼[楼主]2008-11-22 15:07 | gussing      
@zhfj
不支持still pin的话,试试看得先播放视频,然后用DirectShowLib.IVMRWindowlessControl9.GetCurrentImage方法获取静态图。vidow render用vmr9

  回复  引用  查看    
#7楼2008-12-03 20:22 | pl_1069      
怎么没有新的内容来更新呢?实在想学,实想知道
  回复  引用  查看    
#8楼[楼主]2008-12-04 20:38 | gussing      
对不住啊,一直在忙,等忙过这阵子,一定多写
  回复  引用    
#9楼2008-12-24 17:20 | JeffreyXu[未注册用户]
用DirectShowLib-2005开发摄像头采集拍摄软件。可是有些无驱的摄像头在初始化时报错“未找到可用于建立连接的介质筛选器组合”。
修改MediaSubType的值(24RGB改为ARGB32)可以启动,但显示的画面几秒钟才跳一帧,这是什么问题啊

  回复  引用  查看    
#10楼[楼主]2008-12-31 23:45 | gussing      
@JeffreyXu
你用的是什么demux,微软自带的吗?

  回复  引用    
#11楼2009-02-06 09:42 | peter123[未注册用户]
楼主加油呀
  回复  引用    
#12楼2009-02-22 10:28 | 阿[未注册用户]
顶起 不咬让他到了
  回复  引用    
#13楼2009-05-04 17:01 | 木.木
期待你 关于这方面的 后续更新...
  回复  引用    
#14楼2009-07-01 15:40 | 木.木
Volume



声音好象控制不了。...

  回复  引用    
#15楼2009-07-01 15:41 | 木.木
网上有一篇 DirectShow截图的播放器,,但是声音输出不了,,,如果有时间可不可以帮我看一下。... 我QQ406548218 ..我把程序发给你..谢谢。..



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 742061




相关文章:

相关链接: