C#程序实现音乐文件的播放功能

2 篇文章 0 订阅

目录

要求

要求1

要求2

前提

环境

添加WindowsMediaPlayer控件

添加NAudio和NAudio.Vorbis控件

界面

具体代码

结果

小结


要求

要求1

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

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

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

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

要求2

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

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

3. 程序应具有良好

的用户界面,方便用户进行操作。

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

前提

环境

  • Visual Studio 2022
  • .NET Framework 4.8

添加WindowsMediaPlayer控件

下载位置

在COM组件最下面

添加NAudio和NAudio.Vorbis控件

下载位置

添加NAudio控件

添加NAudio.Vorbis控件

界面

其中,各控件实现功能如下:
    Label:展示当前播放的歌曲。
    ListBox:展示选择的歌曲列表。
    AxWindowsMediaPlayer:用于播放常规格式音乐(ogg无法用此播放)。
    OpenFileDialog:用于选择音乐文件。
    Button:有4个,分别用于选择歌曲文件、播放ogg、停止播放、下一曲的功能实现。
    TrackBarl:用于音量控制。

具体代码

using NAudio.Vorbis;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    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 if (extension == ".mp3")
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
            else
            {
                Console.WriteLine("无法识别的文件格式");
            }
        }
        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]);
            }
        }
        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)
            {
                listBox1.Items.Clear();
                localmusiclist.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)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "打开音频|*.ogg";
            string oggFilePath = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                oggFilePath = openFileDialog.FileName;
            }
            using (var reader = new VorbisWaveReader(oggFilePath))
            {
                using (var outputDevice = new WaveOutEvent())
                {
                    outputDevice.Init(reader);
                    outputDevice.Play();
                    while (outputDevice.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }
        private void button4_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;
            }
        }
    }
}

结果

小结

学习了如何使用C#编写一个简单的程序来实现音乐文件的播放功能。使用了WindowsMediaPlayer控件和Naudi.Vorbis控件来处理不同格式的音乐文件,并确保程序具备处理异常、良好的用户界面和兼容性的特点,以确保程序能够在不同版本的C#中正常运行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值