WindowsMediaPlayer:无法设置最大值

时间:2017-11-14 11:02:57

标签: c# windows-media-player

我尝试使用WindowsMediaPlayer当前媒体中的整数来设置地铁主题轨道栏的最大值,但它不断抛出以下错误:

  

指定的参数超出了有效值的范围   参数名称:最大值小于最小值

这意味着根本没有设置最大值,我不确定原因。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;
using VideoLibrary;
using System.IO;
using System.Threading;
using System.Diagnostics;
using WMPLib;
using UITimer = System.Windows.Forms.Timer;

namespace Composer
{
    public partial class Form1 : MetroForm
    {
        private string _pDirectory;
        private string[] _songs;

        private int _sIndex;

        private WindowsMediaPlayer wmp;
        private UITimer _timer;

        public Form1()
        {
            InitializeComponent();
        }

        private void metroTrackBar1_Scroll(object sender, ScrollEventArgs e)
        {
            wmp.settings.volume = metroTrackBar1.Value;
        }

        private void metroTile3_Click(object sender, EventArgs e)
        {
            playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
        }

        private void playAudio(string path)
        {
            wmp.URL = path;
            wmp.controls.play();
            _timer.Start();

            displayHeader(path);

            metroTrackBar2.Maximum = (int)wmp.currentMedia.duration;
        }

        private void t_Tick(object sender, EventArgs e)
        {
            metroTrackBar2.Value = (int)wmp.controls.currentPosition;
        }

        private void displayHeader(string song)
        {
            MethodInvoker invoke = new MethodInvoker(delegate
            {
                metroTile1.Text = Path.GetFileNameWithoutExtension(_songs[_sIndex]);
            });

            this.Invoke(invoke);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string bin;

            _sIndex = 0;
            _pDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Composer\");
            bin = Path.Combine(_pDirectory, "bin");

            _timer = new UITimer();
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(t_Tick);

            if (!Directory.Exists(_pDirectory))
            {
                Directory.CreateDirectory(_pDirectory);
                if(!Directory.Exists(bin))
                {
                    Directory.CreateDirectory(bin);
                }
            } else
            {
                _songs = Directory.GetFiles(bin);
            }

            wmp = new WindowsMediaPlayer();
            //MessageBox.Show(_pDirectory);
        }

        private void metroTile4_Click(object sender, EventArgs e)
        {
            if(_sIndex == (_songs.Length - 1))
            {
                _sIndex = 0;
            } else
            {
                _sIndex++;
            }

            playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
        }
    }
}

0 个答案:

没有答案