【C#】音乐文件的播放功能——Windows程序设计作业2

一、功能介绍

选择歌曲: 用户可以通过文件对话框选择并添加MP3格式的音乐文件。

下一曲:用户可以点击下一曲按钮来播放下一曲歌曲。

停止播放:用户可以点击停止按钮来停止当前正在播放的音乐。

播放OGG:用户可以打开一个对话框选择并播放OGG格式的音乐文件。

播放音乐:应用程序使用Windows Media Player控件来播放选定的音乐文件。用户可以在列表中选择音乐并播放。

音量调节:允许用户通过滑动条调节音乐播放器的音量。


二、创建项目

创建新项目—>选择C#—>Windows—>桌面—>创建windows窗体应用—>下一步—>创建


三、窗体创建

选择工具箱

1.将listBox控件拖放到Winform窗体中,显示音乐播放列表。可以通过属性改变控件的颜色和字体格式(根据自己喜好调节)。例如,如图一所示,点击listbox,打开属性,找到外观中的BackColor,将颜色改为ActiveCaption,颜色如图二所示

  图一

图二

2.将Windows Media Player控件拖放到Winform窗体中。如图三所示

图三

3.将Button1控件拖放到Winform窗体中,并将名字修改为选择歌曲。

4.将Button2控件拖放到Winform窗体中,并将名字修改为停止播放。

5.将Button3控件拖放到Winform窗体中,并将名字修改为下一曲。

6.将Button4控件拖放到Winform窗体中,并将名字修改为播放ogg。

3-6步骤相同,在此只举例Button4的方法。

如图四所示,点击Button,查看属性中的Text,将Button4修改为播放ogg。

图四

3-6步骤呈现如图五所示

图五

7.openFileDialog1控件拖放到Winform窗体中,用于获取音乐文件。

8.Label1控件拖放到Winform窗体中,并将名字修改为音乐播放器于,显示当前播放音乐名称。

9.TrackBar控件拖放到Winform窗体中,可调节音量。

如图六所示,点击TrackBar控件,查看属性中Orientation将Horizontal改为Vertical,将水平方向改为竖直方向,以便于控制音量。(由图七变为图八)

图六

图七

图八

10.安装NAudio NuGet包:在Visual Studio中,通过NuGet包管理器安装NAudio。

创建完毕后如图九所示:

图九


四、代码实现

1.ListBox1代码,显示音乐播放列表

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

2.Button1代码,选择歌曲

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

                }


            }
        }

3.trackBar1代码,调节音量

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

4.Button2代码,停止播放

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

5.Button3代码,下一曲

 private void button3_Click(object sender, EventArgs e)
        {
            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;
        }

6.安装NAudio NuGet包

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.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

7.Button4代码,播放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 (var vorbisStream = new VorbisWaveReader(oggFilePath))
            {
                // 创建WaveOutEvent实例来播放音频  
                using (var outputDevice = new WaveOutEvent())
                {
                    outputDevice.Init(vorbisStream);
                    outputDevice.Play();

                    // 等待播放完成,或者你可以添加其他逻辑来处理播放过程  
                    while (outputDevice.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }
        }

五、运行代码

1.播放mp3

点击“选择歌曲”添加mp3歌曲,点击播放,能正常播放出音乐即为成功。点击“停止播放”歌曲暂停播放,点击“下一曲”,音乐播放器播放下一首歌。

2.播放ogg

点击“播放ogg”,添加ogg歌曲,能正常播放即为成功。


六、完整代码

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.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        string[] files;
        List<string> localmusiclist = new List<string> { };
        private OpenFileDialog openFileDialog1 = new OpenFileDialog();

        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 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 axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {

        }

        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 trackBar1_Scroll(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
        }

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

        private void button3_Click(object sender, EventArgs e)
        {
            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 label1_Click(object sender, EventArgs e)
        {

        }
        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 (var vorbisStream = new VorbisWaveReader(oggFilePath))
            {
                // 创建WaveOutEvent实例来播放音频  
                using (var outputDevice = new WaveOutEvent())
                {
                    outputDevice.Init(vorbisStream);
                    outputDevice.Play();

                    // 等待播放完成,或者你可以添加其他逻辑来处理播放过程  
                    while (outputDevice.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }
        }
    }
}

七、总结

1. 基本思路

确定功能需求

  • 播放.mp3和.ogg文件
  • 停止播放
  • 调整音量
  • 切换下一首
窗体设计
  • 设计一个简洁明了的用户界面,包括播放/暂停按钮、音量调节滑块、下一曲按钮、播放列表以及显示当前播放状态的标签等。
功能实现
  • 使用Windows Forms或WPF等GUI框架来构建窗体界面。
  • 使用适当的音频播放库来处理MP3和Ogg文件的播放,如NAudio库。
  • 实现播放列表的管理,包括添加、删除和更新音频文件。
  • 实现播放控制逻辑,包括播放、停止、音量调整、下一曲等。

2. 难点分析

ogg音频转换
  • NAudio库本身支持多种音频格式,但可能需要额外的编解码器来支持Ogg Vorbis格式。
  • 可以使用Naudi.Vorbis作为NAudio的扩展来支持Ogg Vorbis格式的播放。
选取多个音频文件
  • 使用OpenFileDialog的Multiselect属性来允许用户选择多个文件。
  • 将选定的文件路径添加到播放列表中,并维护一个索引来跟踪当前播放的文件。
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值