C#作业—Windows窗体应用—制作一个音乐播放器

目录

一、需求分析

二、窗体设计

三、代码实现

3.1选择ogg文件并播放

3.2导入音乐文件

3.3音量调节

3.4停止播放

3.5下一曲

四、完整代码

五、心得体会


一、需求分析

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

程序应能够读取MP3文件,并播放其中的音频。
程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
程序应具有良好的用户界面,方便用户进行操作。
程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
要求2:

程序应能够播放ogg文件。
程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
程序应具有良好的用户界面,方便用户进行操作。
程序应具有良好的兼容性,能在不同版本的C#中正常运行。
​​​​


二、窗体设计



控件:

播放ogg为选择ogg文件并播放
listBox1展示将要播放的歌曲列表

选择为选择音乐文件并添加入播放列表中

WindowsMediaPlayer播放音乐
label为正在播放的歌曲
trackBar是音量控制条

停止和下一首分别是停止音乐和播放下一首音乐
openFileDialog用于打开文件


三、代码实现


3.1选择ogg文件并播放

需要添加Naudi.Vorbis控件并在程序开头using NAudio;

private void button3_Click_1(object sender, EventArgs e)//ogg播放
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "打开ogg文件|*.ogg";
    string oggFilePath = "";
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        oggFilePath = openFileDialog.FileName;

        using (var vorbis = new VorbisWaveReader(oggFilePath))
        {
            using (var waveOut = new WaveOutEvent())
            {
                waveOut.Init(vorbis);
                waveOut.Play();

                while (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    Thread.Sleep(1000);
                }


            }
        }

    }
}

3.2导入音乐文件

选择一些符合格式的歌曲,并导入到播放列表中

 private void button1_Click(object sender, EventArgs e)//选择歌曲
 {
     
     openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";//选择格式
     openFileDialog1.Multiselect = true;//多选


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

     }
 }


3.3音量调节


结合trackBar与Windows多媒体播放器中的音量条。

 axWindowsMediaPlayer1.settings.volume = trackBar1.Value;


3.4停止播放


调用stop函数

axWindowsMediaPlayer1.Ctlcontrols.stop();


3.5下一曲

private void button2_Click_1(object sender, EventArgs e)//下一首
{
    
    if (localmusic.Count > 0)
    {
        int index = listBox1.SelectedIndex + 1;//下一曲

        if (index >= localmusic.Count()) index = 0;//循环播放

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

        listBox1.SelectedIndex = index;
    }

}


四、完整代码
 

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

namespace Windows
{
    public partial class Form1 : Form
    {
        string[] files;

        List<string> localmusic = new List<string> { };
        public Form1()
        {
            InitializeComponent();
        }

        private void musicplay(string filename)//播放
        {
            axWindowsMediaPlayer1.URL = filename;
            string extension = Path.GetExtension(filename);//传递文件
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)//歌曲列表显示区
        {
            if (localmusic.Count > 0)
            {
                axWindowsMediaPlayer1.URL = localmusic[listBox1.SelectedIndex];
                musicplay(axWindowsMediaPlayer1.URL);
                label1.Text = Path.GetFileNameWithoutExtension(axWindowsMediaPlayer1.URL);
            }
        }

        private void button1_Click(object sender, EventArgs e)//选择歌曲
        {
            
            openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";//选择格式
            openFileDialog1.Multiselect = true;//多选


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

            }
        }

        private void label1_Click_1(object sender, EventArgs e)//当前歌曲
        {
            
        }

        private void trackBar1_Scroll_1(object sender, EventArgs e) //调节音量
        {
           
            axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
        }

        private void axWindowsMediaPlayer1_Enter_1(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)//下一首
        {
            
            if (localmusic.Count > 0)
            {
                int index = listBox1.SelectedIndex + 1;//下一曲

                if (index >= localmusic.Count()) index = 0;//循环播放

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

                listBox1.SelectedIndex = index;
            }

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void button3_Click_1(object sender, EventArgs e)//ogg播放
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "打开ogg文件|*.ogg";
            string oggFilePath = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                oggFilePath = openFileDialog.FileName;

                using (var vorbis = new VorbisWaveReader(oggFilePath))
                {
                    using (var waveOut = new WaveOutEvent())
                    {
                        waveOut.Init(vorbis);
                        waveOut.Play();

                        while (waveOut.PlaybackState == PlaybackState.Playing)
                        {
                            Thread.Sleep(1000);
                        }


                    }
                }

            }
        }

        private void button4_Click_1(object sender, EventArgs e)//停止播放
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }
    }
}

五、心得体会

学习并理解了如何用windows程序设计一个音乐播放器,在对于控件的使用和编程上我理解了各个操作之间的关联性,明白了如何合理利用各种控件、代码、程序包等来实现一个窗体的作用。在这次作业完成中,有不知道如何进行一些操作或者遇到一些奇怪的报错时,我还学到了如何分析问题、解决问题,这些都让我的编程思维和操作得到锻炼。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值