利用C#程序,实现音乐文件的播放功能(文末附GitHub源代码地址)

目录

一、功能要求

读取MP3文件要求

读取ogg文件要求

二、前期准备步骤

三、代码实现步骤

1、实现“添加音乐”

2、实现“停止音乐”

3、实现”上一曲“

4、实现“下一曲”

5、实现“播放ogg"

6、listbox1中代码

7、填写showmusic函数

四、全部代码

五、测试

六、总结

1、功能实现

2、心得体会

七、GitHub源代码地址



一、功能要求

读取MP3文件要求

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

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

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

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

提示:本文此功能使用WindowsMediaPlayer控件

读取ogg文件要求

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

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

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

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

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

二、前期准备步骤

步骤一:打开C#项目在Visual Studio 2022中,创建新项目,选择Windows窗体应用(.NET Framework)。

 步骤二:左键点击常规(在工具箱中),右键点击指针“选择项” -> “com组件”->“windows media player",将其拖到窗口。

步骤三:右键点击项目(在解决方案资源管理器中),选择管理nuGet资源->”浏览“->”NAUdio.Vorbis“安装。

步骤四:拖工具箱当中的button、listbox、label控件到Form1.cs[设计]当中,添加按键。

三、代码实现步骤

1、实现“添加音乐”

这段代码的目的是让用户从文件系统中选择音频文件,并将这些文件的路径显示在ListBox控件中,同时将它们存储在一个列表中供后续使用。

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

        if(openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            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);
            }
        }

2、实现“停止音乐”

axWindowsMediaPlayer1.Ctlcontrols.stop(); 

3、实现”上一曲“

此代码实现播放上一曲的功能,如果播放到第一一曲,则会从最后一曲开始播放。

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

     axWindowsMediaPlayer1.URL = localmusiclist[index];
     showmusic(axWindowsMediaPlayer1.URL);
     label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);

     listBox1.SelectedIndex = index;
 }

4、实现“下一曲”

此代码实现播放下一曲的功能,如果播放到最后一曲,则会从第一曲开始播放。

         if (localmusiclist.Count > 0)
         {
             if (listBox1.SelectedIndex + 1 > localmusiclist.Count)
             {
                 axWindowsMediaPlayer1.URL = localmusiclist[0];
             }
             axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex+1];
             musicplay(axWindowsMediaPlayer1.URL);
             label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
         }

5、实现“播放ogg"

            string oggFilePath = "path_to_your_ogg_file.ogg"; // 替换为您的OGG文件路径  
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "播放音频|*.ogg";

            if (openFileDialog.ShowDialog()== DialogResult.OK)
            {
                oggFilePath = openFileDialog.FileName;
            }
            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);
                   }
               }
            }

6、listbox1中代码

     if (localmusiclist.Count > 0)
     {
         axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];
         musicplay(axWindowsMediaPlayer1.URL);
         label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
     }

7、填写showmusic函数

    private void showmusic(string filename)
    {
        string extension = Path.GetExtension(filename);
         if (extension == ".ogg")
        {
            Console.WriteLine("this is ogg file.");
        }
         else
        {
            Console.WriteLine("this is not ogg file.");
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
    }

四、全部代码

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

namespace WindowsMusic1
{
    public partial class Form1 : Form
    {
        string[] files;
        List<string> localmusiclist = new List<string> { };
        public Form1()
        {
            InitializeComponent();
        }

        private void showmusic(string filename)
        {
            string extension = Path.GetExtension(filename);
             if (extension == ".ogg")
            {
                Console.WriteLine("this is ogg file.");
            }
             else
            {
                Console.WriteLine("this is not ogg file.");
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "选择音频|*mp3;*.wav;*.fllac";
            openFileDialog1.Multiselect = true;

            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                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 listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (localmusiclist.Count > 0)
            {
                axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];
                showmusic(axWindowsMediaPlayer1.URL);
                label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
            label2.Text = trackBar1.Value + "%";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop(); 
        }

        private void button3_Click(object sender, EventArgs e)
        {
                if (localmusiclist.Count > 0)
                {

                    int index = listBox1.SelectedIndex + 1;

                    if (index >= localmusiclist.Count()) { index = 0; }

                    axWindowsMediaPlayer1.URL = localmusiclist[index];

                    showmusic(axWindowsMediaPlayer1.URL);
                    label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);

                    listBox1.SelectedIndex = index;


                }
            

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string oggFilePath = "path_to_your_ogg_file.ogg"; // 替换为您的OGG文件路径  
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "播放音频|*.ogg";

            if (openFileDialog.ShowDialog()== DialogResult.OK)
            {
                oggFilePath = openFileDialog.FileName;
            }
            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);
                   }
               }
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
         
                if (localmusiclist.Count > 0)
                {
                    int index = listBox1.SelectedIndex;

                    // 如果当前没有选中任何项,或者已经是第一项,可以选择播放最后一项或者保持当前项不变  
                    if (index < 0)
                    {
                        index = 0; // 或者你可以选择播放列表中的最后一项:index = localmusiclist.Count - 1;  
                    }
                    else
                    {
                        index--; // 否则播放上一项  

                        // 如果现在索引是-1(即原本是第一项),可以选择播放列表中的最后一项或者保持当前项不变  
                        if (index < 0)
                        {
                            index = localmusiclist.Count - 1; // 循环到最后一项  
                        }
                    }

                    axWindowsMediaPlayer1.URL = localmusiclist[index];
                    showmusic(axWindowsMediaPlayer1.URL);
                    label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);

                    listBox1.SelectedIndex = index;
                }
            
        }
    }
 }

五、测试

六、总结

1、功能实现

音乐播放器已经具备了基本的播放控制功能,包括播放播放音乐(通过的button1_Click事件处理)、停止音乐(通过的button2_Click事件处理)、上一曲(通过button5_Click事件处理)、播放下一曲(通过的button3_Click事件处理)和播放ogg(通过的button4_Click事件处理)。

播放器能够从一个包含音乐文件路径的列表中加载并播放音乐。

播放器的当前播放曲目会在一个列表框(listBox1)中显示,并且当播放曲目变化时,列表框的选中项也会相应更新。

播放器还包含了一个标签(label1)用于显示当前播放曲目的文件名。

播放器还包含音量的调节(label2)。

2、心得体会

在开发过程中,我接触并学习了许多新的技术和工具,如Windows Media Player控件、ActiveX控件的使用、文件路径处理(如Path.GetFileNameWithoutExtension)等。这些新技术和工具不仅帮助我解决了开发中的难题,也拓宽了我的技术视野。

七、GitHub源代码地址

https://github.com/chenwq2004/windowshomework.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值