自制音乐播放器:编写c#程序,播放ogg,mp3文件

目录

一、内容

二、操作

三、运行结果

四、全部代码

五、小结

一、内容

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

要求1:

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

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

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

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

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

要求2:

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

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

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

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

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

二、操作

1、使用axWindowsMediaPlaver的COM组件
(1)加载COM组件:工具->选择工具箱->COM Components->Windows Media Player如下图:

(2)把Windows Media Plaver控件拖放到Winform窗体中,把axWindowsMediaPlaver1中URL
属性设置为MP3或是AVI的文件路径,并运行。

2、添加Nuget程序包并添加Naudi.Vorbis控件和Naudi.Wave

(1)安装 N audio, 工具->NuGet包管理器->程序包管理器控制台,如图
(2)输入Install-Package NAudios,执行
(3)输入using NAudio.Wave,执行
(4) 工具->NuGet包管理器->管理解决方案的NuGet程序包,如图
(5)下载 N audi.Vorbis,如图
(6)在代码顶端添加头文件
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;

3、添加musicplay函数

 private void musicplay(string filename)
 {
     axWindowsMediaPlayer1.URL = filename;
     string extension = Path.GetExtension(filename);


     if (extension == ".ogg") { Console.WriteLine("这是ogg文件。"); }
     else
     {
         axWindowsMediaPlayer1.Ctlcontrols.play();

     }

 }

4、把Listbox控件拖放到Winform窗体中(可将颜色改为MenuHighlight,字体改为微软雅黑五号)。
把Button1控件拖放到Winform窗体中,名称改为“选择歌曲”。进入编写相应代码。

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (localmusiclist.Count > 0)
    {
        axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];
        musicplay(axWindowsMediaPlayer1.URL);
        label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
    }
}

5、把Label控件拖放到Winform窗体中,可将空间颜色改为红色。

6、添加调整声音大小控件拖放到Winform窗体中。编写相应代码。

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

7、把Button1控件拖放到Winform窗体中,名称改为“选择歌曲”。进入编写相应代码。

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);
        }
    }
}

8、把Button2控件拖放到Winform窗体中,名称改为“停止播放”。进入编写相应代码。

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

9、把Button3控件拖放到Winform窗体中,名称改为“下一首”。进入编写相应代码。

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];

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

        listBox1.SelectedIndex = index;
    }
}

10、把Button4控件拖放到Winform窗体中,名称改为播放ogg。进入编写相应代码以处理ogg播放

private void button4_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "打开音频|*.ogg";

    string oggFilePath = "";

    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);
            }
        }
    }
}

三、运行结果

四、全部代码

using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
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 WindowsFormsApp5
{
    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
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();

            }

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

        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 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];

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

                listBox1.SelectedIndex = index;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "打开音频|*.ogg";

            string oggFilePath = "";

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

五、小结

本次任务是开发一个能够播放MP3和OGG格式音乐文件的C#Windows Forms应用程序。经过一系列的设计、编码和测试过程。首先,用户界面设计:成功创建了一个Windows Forms应用程序,并添加了必要的UI控件,如播放/暂停/停止按钮、音量滑块、文件选择对话框等。界面设计简洁直观,用户能够方便地操作和管理音乐文件。第二,音频播放功能:应用程序能够支持MP3和OGG两种音频格式的播放。对于MP3文件,使用了Windows Media Player控件;对于OGG文件,则集成了NAudio库进行音频解码和播放。第三,文件管理:实现了文件选择对话框,允许用户浏览并选择要播放的音乐文件。同时,还设计了一个播放列表,用于管理用户选择的音乐文件。最后,NAudio的学习与使用:NAudio库虽然功能强大,但使用它需要一定的学习和理解时间。在开发过程中,需要熟悉NAudio的API和音频处理的相关知识。在开发过程中,对第三方了解和熟悉程度对项目的进展有着重要影响。

  • 23
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值