zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C# 面向对象(Mp3案例完善)

c#案例 面向对象 完善 Mp3
2023-09-11 14:20:51 时间


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 面向对象7._13_Mp3完善
{
    class Program
    {
       static void Main(string[] args)
       {

           PlaySongps = new PlaySong();
          ps.Inof();

          Console.ReadLine();
       }
    }
    //歌曲信息列表
    class Song
    {
       public Song() { }
       public Song(String title, string singer, intlength)
       {
          this.titile = title;
          this.singer = singer;
          this.length = length;

       }
       ///
       /// 歌曲名
       ///
       public String titile{set;get;}
       public String singer { set; get; }
       public int length { set; get; }


    }

    class PlaySong
    {
       List list = new List();
       ///
       /// 载入本地歌曲
       ///
       
       private int listLenght; //获取到列表长度(有几首歌)
       public void bianli() {

           listLenght= 0;
           foreach(Song item in list)
           {
              Console.Write("   "+(listLenght+1)+"."+item.titile+" - "+item.singer+" ");
              time(item.length);
              listLenght++;
           }
       }
     
       public void Inof()
       {

          Console.WriteLine("您的当前播放列表是:");
          Console.WriteLine();
          Console.WriteLine("++ ——————————— ++");
          Console.WriteLine();
          bianli();
          Console.WriteLine();
          Console.WriteLine("++ ——————————— ++");
          Console.WriteLine();
          Console.WriteLine(" 请您输入功能序号 :");
          Console.WriteLine();
          Console.WriteLine("1.播放歌曲  2.载入本地歌曲 3.上一首  4.下一首 5.暂停  6.添加歌曲 7.退出播放器");
           int n =int.Parse(Console.ReadLine());
           switch(n)
           {
              case 1:
                 play();
                 break;
              case 2:
                 load();
                 break;
              case 3:
                 upSong();
                 break;
              case 4:
                 downSong();
                 break;
              case 5:
                 stop();
                 break;
              case 6:
                 downLoad();
                 break;
              case 7:
                 return;
              default :
                 break;

           }
       }
       public void load()
       {
           Song s1 =new Song("灰色头像", "许嵩", 356);
           Song s2 =new Song("坏孩子", "许嵩", 303);
           Song s3 =new Song("城府", "许嵩", 287);
           Song s4 =new Song("因为了解", "汪苏泷", 196);
           Song s5 =new Song("送你的读白", "许嵩", 314);
          list.Add(s1);
          list.Add(s2);
          list.Add(s3);
          list.Add(s4);
          list.Add(s5);
          Console.WriteLine("++ ——————————— ++");
          Console.WriteLine();
          bianli();
          Console.WriteLine();
          Console.WriteLine("++ ——————————— ++");
          Inof();
       }
       public void downLoad()
       {
           Song s =new Song();

          Console.Write("请输入您要下载的歌曲名称:");
           s.titile =Console.ReadLine();
          Console.Write("请输入您要下载的歌手名称:");
           s.singer =Console.ReadLine();
          Console.Write("请输入您要下载歌曲的时长:");
           s.length=int.Parse( Console.ReadLine());

          list.Add(s);
          listLenght++;
          Console.WriteLine("您的歌曲下载成功!");
          Console.WriteLine();
          Inof();
       }
      private int index = 0; //记录播放的歌曲
       public void play()
       {
          Console.WriteLine();
           if (index< 0)
           {
             Console.WriteLine("当前列表没有歌曲!");
             
           }
           if (index< listLenght)
           {
              Console.WriteLine("正在播放:" +list[index].titile);
           }
          Console.WriteLine();
          Inof();
       }

      
       public void upSong()
       {
           if (index< 0) {
             Console.WriteLine("当前列表没有歌曲");
           }
          
           index =index - 1 < 0 ?  0: index - 1;
           if (index- 1 < 0)
           {
             Console.WriteLine("当前播放已经是第一首音乐:" + list[0].titile);
           }
           else
           {
              Console.WriteLine("正在播放:" +list[index].titile);
           }
          Console.WriteLine();
          Inof();
       }

       public void downSong()
       {
           if (index< 0)
           {
             Console.WriteLine("当前列表没有歌曲");
           }
           index =index + 1 > listLenght ? listLenght : index+1;
           if (index+1 > listLenght)
           {
              
             Console.WriteLine("当前播放已经是最后一首音乐:");
           }
           else
           {
              Console.WriteLine("正在播放:" +list[index].titile);
           }
              Console.WriteLine();
          Inof();
       }

       public void stop()
       {
          Console.WriteLine("暂停播放:"+list[index].titile);
          Console.WriteLine();
          Inof();
       }

       ///
       /// 转换时间格式 00:00:00 
       ///
       ///
       public void time(int time)
       {
           TimeSpants = new TimeSpan(0, 0, time);
          Console.WriteLine("  " +(ts.Minutes).ToString().PadLeft(2, '0') + ":" +(ts.Seconds).ToString().PadLeft(2, '0'));
       }

    }

}