音乐播放器控件

 一、题目与代码实现 

请编写一个C#程序,实现音乐文件的播放功能。

1.要求1:

1. 程序应能够读取MP3文件,并播放其中的音频。

2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。

3. 程序应具有良好的用户界面,方便用户进行操作。

4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。

提示:此功能可以使用WindowsMediaPlayer控件

2.要求2:

1. 程序应能够播放ogg文件。

2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。

3. 程序应具有良好的用户界面,方便用户进行操作。

4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。

提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件

using AxWMPLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;

namespace _4._23lianxi1
{
    public partial class Form1 : Form
    {
        string[] files;  //数组型

        List<string> localmusiclist = new List<string> { };  //列表
        public Form1()
        {
            InitializeComponent();
        }
        private void musicplay(string filename)
        {
            axWindowsMediaPlayer1.URL = filename;
            string extension = Path.GetExtension(filename);

            if (extension == ".ogg") { Console.WriteLine("这是ogg文件"); }
            else
            {
                try
                {
                    axWindowsMediaPlayer1.Ctlcontrols.play(); // 注意这里可能需要检查属性/方法的名称是否正确  
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show("文件不存在:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (IOException ex)
                {
                    MessageBox.Show("文件读取错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    // 捕获其他未指定的异常  
                    MessageBox.Show("发生未知错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
            openFileDialog1.Multiselect = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                localmusiclist.Clear();
                listBox1.Items.Clear();

                if (files != null)
                {
                    Array.Clear(files, 0, files.Length);
                }

                files = openFileDialog1.FileNames;
                string[] array = files;
                foreach (string x in array)
                {
                    listBox1.Items.Add(x);  //存放歌曲列表
                    localmusiclist.Add(x);
                }
            }
        }
        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (localmusiclist.Count > 0)
            {
                axWindowsMediaPlayer1.URL = localmusiclist[0];  //代表从第一个开始读
                axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];//被击中的
                //axWindowsMediaPlayer1.Ctlcontrols.play();
                musicplay(axWindowsMediaPlayer1.URL);
                label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
            }
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (localmusiclist.Count > 0)
            {
                int index = listBox1.SelectedIndex + 1;
                if (index >= localmusiclist.Count()) { index = 0; }
                else { }

                axWindowsMediaPlayer1.URL = localmusiclist[index];//被击中的(
                musicplay(axWindowsMediaPlayer1.URL);
                label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
                listBox1.SelectedIndex = index;
            }
        }
        //使用ogg文件
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog = new OpenFileDialog();
            openFileDialog1.Filter = "打开音频|*.ogg";
            string oggFilePath = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                oggFilePath = openFileDialog1.FileName;
                try
                {
                    using (var vorbisReader = new VorbisWaveReader(oggFilePath))
                    {
                        using (var outputDevice = new WaveOutEvent())
                        {
                            outputDevice.Init(vorbisReader);
                            outputDevice.Play();
                            // 等待播放完成,或者您可以根据需要添加其他逻辑
                            while (outputDevice.PlaybackState == PlaybackState.Playing)
                            {
                                System.Threading.Thread.Sleep(1000);
                            }
                        }
                    }
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show("文件不存在:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (IOException ex)
                {
                    MessageBox.Show("文件读取错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    // 捕获其他未指定的异常  
                    MessageBox.Show("发生未知错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
 {
     // 指定图片文件的路径(这里假设图片位于项目目录下)  
     string imagePath = @"D:\我的作业\windows\头像.jpg"; // 替换为你的图片路径  

     // 使用Image.FromFile方法加载图片  
     Image image = Image.FromFile(imagePath);

     // 将图片设置到PictureBox中  
     pictureBox1.Image = image;

     // (可选)设置SizeMode以适应图片或窗体大小  
     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
 }
    }
}

二、MP3文件

1.选择歌曲

 先利用筛选器来选择特定后缀即音乐类型的文件,多文件选择时Multiselect为true,

openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
openFileDialog1.Multiselect = true;

 然后定义localmusiclist来存放已经选择的音乐的路径,如果用户点击了文件,即openFileDialog1.ShowDialog() == DialogResult.OK,就会进行路径列表和歌曲列表的清除,

List<string> localmusiclist = new List<string>

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    localmusiclist.Clear();
    listBox1.Items.Clear();

 如果文件不为空,会让定义的数组类型的files存放歌曲的名字,然后把files的内容复制给array,然后通过遍历让现在点击的歌曲的路径和名称列表进入,存放歌曲

string[] files;  //数组型

if (files != null)
{
    Array.Clear(files, 0, files.Length);
}
files = openFileDialog1.FileNames;
string[] array = files;
foreach (string x in array)
{
    listBox1.Items.Add(x);  //存放歌曲列表
    localmusiclist.Add(x);
}

 然后用WindowsMediaPlayer的URL去获取歌曲名,用extension获取路径,然后用Ctlcontrol属性的play方法,让歌曲播放。

axWindowsMediaPlayer1.URL = filename;
string extension = Path.GetExtension(filename);

axWindowsMediaPlayer1.Ctlcontrols.play();

 用户页面如下

开始播放了 

2.暂停播放

暂停用WindowsMediaPlayer的Ctlcontrol属性的stop方法,让歌曲暂停

 axWindowsMediaPlayer1.Ctlcontrols.stop();
3.下一曲

如果播放列表中还有歌曲,就将ListBox 控件中当前选中项的索引,即为listBox1.SelectedIndex,加1,再存储到index中,如果index>=目录里面的数量,即为出错误了,将index置0。否则,index为你点击的歌曲的次序,然后修改路径为你点击的歌曲路径,再使用musicplay方法播放歌曲。然后标签label的内容是播放歌曲的名字,再将index的值赋给listBox1.SelectedIndex,是选中项的索引随歌曲的变化而变化。

if (localmusiclist.Count > 0)
{
    int index = listBox1.SelectedIndex + 1;
    if (index >= localmusiclist.Count()) { index = 0; }
    else { }

    axWindowsMediaPlayer1.URL = localmusiclist[index];//被击中的(
    musicplay(axWindowsMediaPlayer1.URL);
    label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
    listBox1.SelectedIndex = index;
}
4.音量控制

trackBar1控件中获取当前滑块的值设置为axWindowsMediaPlayer1控件的音量,就可控制音量。

axWindowsMediaPlayer1.settings.volume = trackBar1.Value;

 

5.播放目录

用WindowsMediaPlayer的URL去获取localmusiclist列表中的被选择的歌曲名,然后使用musicplay方法播放歌曲,标签label的内容是播放歌曲的名字

if (localmusiclist.Count > 0)
{
    axWindowsMediaPlayer1.URL = localmusiclist[0];  //代表从第一个开始读
    axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];//被击中的
    //axWindowsMediaPlayer1.Ctlcontrols.play();
    musicplay(axWindowsMediaPlayer1.URL);
    label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
}
6.处理异常

在musicplay方法中处理异常,用try-catch结构,处理3种异常。

catch (FileNotFoundException ex)
{
    MessageBox.Show("文件不存在:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (IOException ex)
{
    MessageBox.Show("文件读取错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
    // 捕获其他未指定的异常  
    MessageBox.Show("发生未知错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

三、ogg文件

1.播放歌曲

先利用筛选器来选择特定后缀即ogg类型的文件,然后openFileDialog1.FileName 属性就会获取用户选择的文件的完整路径和文件名,让背后赋值给oggFilePath 变量

OpenFileDialog OpenFileDialog = new OpenFileDialog();
openFileDialog1.Filter = "打开音频|*.ogg";
string oggFilePath = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    oggFilePath = openFileDialog1.FileName;

 使用Naudi.Vorbis控件,vorbisReader 装了ogg歌曲内容,初始化 outputDevice 对象,配置 outputDevice 以使用 vorbisReader 提供的音频流,然后用outputDevice 的play方法,播放歌曲,后面Sleep语句它会使线程暂停1000毫秒,即1秒。

using (var vorbisReader = new VorbisWaveReader(oggFilePath))
{
    using (var outputDevice = new WaveOutEvent())
    {
        outputDevice.Init(vorbisReader);
        outputDevice.Play();
        // 等待播放完成,或者您可以根据需要添加其他逻辑
        while (outputDevice.PlaybackState == PlaybackState.Playing)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }
}
2.处理异常

用try-catch结构,处理3种异常

catch (FileNotFoundException ex)
{
    MessageBox.Show("文件不存在:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (IOException ex)
{
    MessageBox.Show("文件读取错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
    // 捕获其他未指定的异常  
    MessageBox.Show("发生未知错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

四、小头像


     // 指定图片文件的路径(这里假设图片位于项目目录下)  
     string imagePath = @"D:\我的作业\windows\头像.jpg"; // 替换为你的图片路径  

     // 使用Image.FromFile方法加载图片  
     Image image = Image.FromFile(imagePath);

     // 将图片设置到PictureBox中  
     pictureBox1.Image = image;

     // (可选)设置SizeMode以适应图片或窗体大小  
     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

五、仓库

音乐播放器 · Issue #I9LNUS · 马彤彤/音乐播放器 - Gitee.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值