010继续完善播放器功能,实现自动播放下一首等比较简单功能

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 System.IO;
namespace XCGAMVPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> listSong = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "请选择要播放的文件";
            open.InitialDirectory = @"E:\";
            open.Multiselect = true;
            open.Filter = "所有文件|*.*|音乐文件|*.mp3|音乐文件|*.wav";
            open.ShowDialog();
            string[] path = open.FileNames;
            for (int i = 0; i < path.Length; i++)
            {
                listBox1.Items.Add(Path.GetFileName(path[i]));
                listSong.Add(path[i]);
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL=listSong[listBox1.SelectedIndex];
             
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            index--;
            if (index < 0)
            {
                index = listBox1.Items.Count - 1;
            }
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            index++;
            if (index == listBox1.Items.Count)
            {
                index = 0;
            }
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
        }

       
         
        private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
        {
            //利用播放器状态实现自动播放下一首存在BUG,留待日后灵光一现
            //int index =listBox1.SelectedIndex;

            //if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
            //{


            //    //index=index+1;
            //    // listBox1.SelectedIndex = index;
            //    //axWindowsMediaPlayer1.URL = listSong[index];
            //}
             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Timer timer1 = new Timer();
            timer1.Tick +=new EventHandler( Timer1_Tick);
            timer1.Start();
            timer1.Interval = 10;
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;

            if ((int)axWindowsMediaPlayer1.playState == 1)
            {
                if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
                {
                    listBox1.SelectedIndex = index + 1; axWindowsMediaPlayer1.URL = listSong[index + 1];
                }
                else
                if (listBox1.Items.Count > 0)
                {
                    index = 0;
                    listBox1.SelectedIndex = 0; axWindowsMediaPlayer1.URL = listSong[index];
                    listBox1.SelectedIndex = index;
                    axWindowsMediaPlayer1.URL = listSong[index];
                }
               

                
            }


        }
        //设定一个布尔类型,用来分辨歌曲播放后点击暂停再次点击播放时可以继续播放 而不是从头播放
        bool b = true;
        private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnPlay.Text == "播放")
                {
                    if (b)
                    {
                        //获得选中的歌曲 并从头播放
                        axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex];
                    }
                    axWindowsMediaPlayer1.Ctlcontrols.play();
                    btnPlay.Text = "暂停";
                }
                else if (btnPlay.Text == "暂停")
                {
                    axWindowsMediaPlayer1.Ctlcontrols.pause();
                    btnPlay.Text = "播放";
                    b = false;
                }
            }
            catch
            {
                MessageBox.Show("没有任何内容可以播放,施主请不要淘气");
            }
        

        }
        /// <summary>
        /// 实现当切换下标后,更改btnplay的text为播放,点击后,播放当前选中歌曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnPlay.Text = "播放";
            if (btnPlay.Text == "暂停")
            {
                axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex];
            }

        }
    }
}

利用timer来实现自动播放下一首歌曲,同时实现播放、暂停的切换显示及对应的功能。应该还是有BUG的,另外使用Install发布后在别的电脑安装运行后有问题,目前还不知道到底是因为什么.......

转载于:https://www.cnblogs.com/xcyg/p/6646437.html

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值