none
使用Interop.QuartzTypeLib.dll播放wav文件怎么关闭文件流? RRS feed

  • 问题

  • 代码如下:

    /// <summary>
        /// 绑定文件 播放
        /// </summary>
        /// <param name="mediaFileName"></param>
        /// <param name="run"></param>
        public void RenderMedia(string mediaFileName, bool run)
        {
          clearUp();
          try
          {
            objMedia = new FilgraphManager();
            objMedia.RenderFile(mediaFileName);
            volMedia = objMedia as IBasicAudio;
            posMedia = objMedia as IMediaPosition;
            ctlMedia = objMedia as IMediaControl;
            eexMedia = objMedia as IMediaEventEx;
            eexMedia.SetNotifyWindow((int)this.Handle, WM_GRAPHNOTIFY, 0);
            mediaStatus = 1;
            mediaDuration = posMedia.Duration;
            volMedia.Volume = mediaVolum;
            if (run) Play();
          }
          catch
          {
            MessageBox.Show("Error loding media file!");
            clearUp();
          }
        }
    
     private void clearUp()
        {
          if (ctlMedia != null)
            ctlMedia.Stop();
    
          mediaStatus = 1;
    
          mediaDuration = 0;
          mediaCurPos = 0;
    
          if (eexMedia != null)
            eexMedia.SetNotifyWindow(0, 0, 0);
    
          if (ctlMedia != null) ctlMedia = null;
          if (volMedia != null) volMedia = null;
          if (posMedia != null) posMedia = null;
          if (eexMedia != null) eexMedia = null;
          if (objMedia != null) objMedia = null;
        }
    

    /// <summary>

            /// 停止

            /// </summary>

            public void Stop()

            {

                ctlMedia.Stop();

                posMedia.CurrentPosition = 0;

                mediaStatus = 1;

            }

    /// <summary>
        /// 播放
        /// </summary>
        public void Play()
        {
          ctlMedia.Run();
        }
     /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
          ctlMedia.Stop();
        }
    
     private void Delete(string file)
        {
          System.IO.File.Delete(file);
        }
    
    首先调用RenderMedia方法,并绑定一个音频文件并开始播放。然后Stop方法停掉。然后Delete方法,删掉该文件。但是却无法删除。我想应该是文件资源没有被释放。但是该怎么释放呢?


    郭鹏
    2010年12月9日 7:23

全部回复

  • 代码如下:

     

    /// <summary>
        /// 绑定文件 播放
        /// </summary>
        /// <param name="mediaFileName"></param>
        /// <param name="run"></param>
        public void RenderMedia(string mediaFileName, bool run)
        {
          clearUp();
          try
          {
            objMedia = new FilgraphManager();
            objMedia.RenderFile(mediaFileName);
            volMedia = objMedia as IBasicAudio;
            posMedia = objMedia as IMediaPosition;
            ctlMedia = objMedia as IMediaControl;
            eexMedia = objMedia as IMediaEventEx;
            eexMedia.SetNotifyWindow((int)this.Handle, WM_GRAPHNOTIFY, 0);
            mediaStatus = 1;
            mediaDuration = posMedia.Duration;
            volMedia.Volume = mediaVolum;
            if (run) Play();
          }
          catch
          {
            MessageBox.Show("Error loding media file!");
            clearUp();
          }
        }
    
     private void clearUp()
        {
          if (ctlMedia != null)
            ctlMedia.Stop();
    
          mediaStatus = 1;
    
          mediaDuration = 0;
          mediaCurPos = 0;
    
          if (eexMedia != null)
            eexMedia.SetNotifyWindow(0, 0, 0);
    
          if (ctlMedia != null) ctlMedia = null;
          if (volMedia != null) volMedia = null;
          if (posMedia != null) posMedia = null;
          if (eexMedia != null) eexMedia = null;
          if (objMedia != null) objMedia = null;
        }
    

    /// <summary>

            /// 停止

            /// </summary>

            public void Stop()

            {

                ctlMedia.Stop();

                posMedia.CurrentPosition = 0;

                mediaStatus = 1;

            }

    /// <summary>
        /// 播放
        /// </summary>
        public void Play()
        {
          ctlMedia.Run();
        }
     /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
          ctlMedia.Stop();
        }
    
     private void Delete(string file)
        {
          System.IO.File.Delete(file);
        }
    
    首先调用RenderMedia方法,并绑定一个音频文件并开始播放。然后Stop方法停掉。然后Delete方法,删掉该文件。但是却无法删除。我想应该是文件资源没有被释放。但是该怎么释放呢?

     

     


    郭鹏
    你好,我感觉是你的调用方式的问题,请参见: http://www.codeproject.com/Articles/2632/DirectShow-MediaPlayer-in-C
    帮助大家解决问题咯~~小站:http://www.cnblogs.com/knightluffy/
    2012年2月2日 1:04