新人求问:
用c#编写媒体播放器(调用windows media player),怎样获取当前文件播放时间以及文件播放总时间,还有设置怎样使播放器播完后自动退出。可以给出代码吗

解决方案 »

  1.   

    http://topic.csdn.net/u/20101010/00/89E7D09B-7C59-4D74-A5E8-2248EA4C649D.html
      

  2.   

    如果你是用System.Diagnostics.Process.Start("foo.mp3");的方法播放的,无法获取这些信息。如果是用media player控件,则:axWindowsMediaPlayer1.URL = @"foo.mp3";
    axWindowsMediaPlayer1.PlayStateChange += onPlayStateChange;private void onPlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {double duration = axWindowsMediaPlayer1.currentMedia.duration;//文件长度,获取一次就够了double currentPosition = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;//当前文件位置,如果要在文件播放过程中检测,可将这句放到一个timer控件的事件里if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
    {//媒体播放完
      ...
    }
    }注意,如果是在64位windows 7系统下,需编译成x86,不能用缺省的anyCpu,不然这个事件不会被触发。如果是用winmm.dll,看这个链接:
    http://blog.csdn.net/baihe_591/article/details/2594041
      

  3.   

    播放完成:处理PlayStateChange 事件 8 MediaEnded应该是你想要的状态,然后退出程序即可。// Add a delegate for the PlayStateChange event.
    player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        // Test the current state of the player and display a message for each state.
        switch (e.newState)
        {
            case 0:    // Undefined
                currentStateLabel.Text = "Undefined";
                break;        case 1:    // Stopped
                currentStateLabel.Text = "Stopped";
                break;        case 2:    // Paused
                currentStateLabel.Text = "Paused";
                break;        case 3:    // Playing
                currentStateLabel.Text = "Playing";
                break;        case 4:    // ScanForward
                currentStateLabel.Text = "ScanForward";
                break;        case 5:    // ScanReverse
                currentStateLabel.Text = "ScanReverse";
                break;        case 6:    // Buffering
                currentStateLabel.Text = "Buffering";
                break;        case 7:    // Waiting
                currentStateLabel.Text = "Waiting";
                break;        case 8:    // MediaEnded
                currentStateLabel.Text = "MediaEnded";
                break;        case 9:    // Transitioning
                currentStateLabel.Text = "Transitioning";
                break;        case 10:   // Ready
                currentStateLabel.Text = "Ready";
                break;        case 11:   // Reconnecting
                currentStateLabel.Text = "Reconnecting";
                break;        case 12:   // Last
                currentStateLabel.Text = "Last";
                break;        default:
                currentStateLabel.Text = ("Unknown State: " + e.newState.ToString());
                break;
        }
    }
      

  4.   

    axWindowsMediaPlayer1.Ctlcontrols.currentPosition  当前position
    The currentPosition property gets or sets the current position in the media item in seconds from the beginning.
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd564740(v=VS.85).aspx
    axWindowsMediaPlayer1.currentMedia.duration  总长度
    A System.Double that is the duration in seconds.
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd564790(v=VS.85).aspx
      

  5.   

    我写了
    if (MediaPlayer.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
    Application.Exit();为什么文件播放完后,软件不能自动退出
      

  6.   

    player_PlayStateChange中设置个断点,完成后函数会被调用吗?如果调用了,看看playState 是什么