C#音乐播放器、支持在线播放

本文介绍了如何使用C#编程语言开发一款音乐播放器,该播放器具备在线播放音乐的功能。通过集成相关API和服务,实现了从网络流媒体源获取并播放音乐的特性。
摘要由CSDN通过智能技术生成


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using ComponentFactory.Krypton.Ribbon;
using System.Xml;
using System.IO;
using System.Net;

namespace 音乐播放器
{
    public partial class MusicPlayer : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public MusicPlayer()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 打开本地文件
        /// </summary>
        System.Windows.Forms.OpenFileDialog ofdLightning = new OpenFileDialog();
        /// <summary>
        /// 播放器
        /// </summary>
        AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
        /// <summary>
        /// 当前播放曲目序号
        /// </summary>
        private int pos;
        /// <summary>
        /// 控制快进状态
        /// </summary>
        private bool ReturnExecute=true;
        /// <summary>
        /// 播放模式
        /// 单曲循环 - Single
        /// </summary>
        private bool Single=false;
        /// <summary>
        /// 播放模式
        /// 随机播放 - Shuffle
        /// </summary>
        private bool Shuffle = false; 
        /// <summary>
        /// 播放模式
        /// 循序播放 - Step
        /// </summary>
        private bool Step = false;
        /// <summary>
        /// 播放模式
        /// 循环播放 - Loop
        /// </summary>
        private bool Loop = false;

        //启动状态
        //private bool Start = true;

        private void kryptonListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            //if (Start == true)
            //{
            //    this.Controls.Add(wmp);
            //    Start=false;
            //    return;
            //}
            this.Controls.Add(wmp);
            if (this.kryptonListBox1.Items.Count > 0)
            {
                if (this.kryptonListBox1.SelectedItems.Count > 0)
                {

                    
                    //wmp.URL = this.kryptonListBox1.SelectedItem.ToString();
                    //wmp.Ctlcontrols.play();
                    //this.timer1.Start();

                    pos = this.kryptonListBox1.SelectedIndex;

                    wmp.URL = this.kryptonListBox1.Items[pos].ToString();
                    wmp.Ctlcontrols.play();
                    this.timer1.Start();
                    this.kryptonLabel8.Text = this.kryptonListBox1.SelectedItem.ToString();
                    int ImageInde = ReturnImage();
                    this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg";
                }
            }
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

       

        private void pictureBox4_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox4.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox4.BackColor = Color.Transparent;
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox5.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox2.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox3_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox3.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox6_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox6.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox6_MouseLeave(object sender, EventArgs e)
        {
            if (Step == true)
            {
                return;
            }
            this.pictureBox6.BackColor = Color.Transparent;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            if (Single == true)
            {
                return;
            }
            this.pictureBox3.BackColor = Color.Transparent;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            if (Shuffle == true)
            {
        
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyQQ2010 { public partial class MusicForm : Form { public MusicForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件夹选项 OpenFileDialog f = new OpenFileDialog(); //设置能够选中的多个选项 f.Multiselect = true; //弹出文件对话框 DialogResult result = f.ShowDialog(); if ( DialogResult.OK==result) { //获取选中的曲的路径 string[] music = f.FileNames; //将曲添加到ListBox中 foreach (string item in music) { this.lstMusic.Items.Add(item); } } } //双击播放曲 private void lstmusic2_DoubleClick(object sender, EventArgs e) { //获取选中的曲 string music = this.lstmusic2.SelectedItem.ToString(); //播放曲 Player.URL = music; } private void 添加到播放列表ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要选中的曲"); return; } else { //获取选中的曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的曲移动到播放列表中 this.lstmusic2.Items.Add(music); //从lstmusic中移除 this.lstMusic.Items.Remove(music); } } private void 删除音乐ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的曲"); } else { //获取选中的曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的曲删除掉 this.lstMusic.Items.Remove(music); } } private void 从列表中移除ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstmusic2.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的曲"); } else { //获取选中的曲 string music = this.lstmusic2.SelectedItem.ToString(); //把选中的曲删除 this.lstmusic2.Items.Remove(music); } } private void MusicForm_Load_1(object sender, EventArgs e) { //播放顺序默认为列表播放 this.cboPlayOrder.SelectedIndex = 0; this.trm1.Start(); } private void trm1_Tick(object sender, EventArgs e) { //判断播放的顺序 if (Player.playState == WMPLib.WMPPlayState.wmppsStopped) { if (this.cboPlayOrder.Text == "列表播放") { if (this.lstmusic2.SelectedIndex < this.lstmusic2.Items.Count - 1) { this.lstmusic2.SelectedIndex++; this.lstMusic.Items.Add(this.lstmusic2.SelectedItem.ToString()); Player.URL = this.lstmusic2.SelectedItem.ToString(); } else { this.lstmusic2.SelectedIndex = 0; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } else if (this.cboPlayOrder.Text == "随机播放") { Random r = new Random(); int index = r.Next(0, this.lstmusic2.Items.Count - 1); this.lstmusic2.SelectedIndex = index; string item = this.lstmusic2.SelectedItem.ToString(); this.lstMusic.Items.Add(item);//随机播放曲也要在lbmusic2中显示 Player.URL = item; } else { int index = this.lstmusic2.SelectedIndex; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值