C# 面向对象例题 -- Mp3功能实现
【摘要】
C# 面向对象例题 -- Mp3功能实现
请利用面向对象分析实现Mp3功能,Mp3具有播放歌曲,载入本地歌曲 ,上一首,下一首,添加歌曲,退出播放器功能。
实现结果:
using System;using System.Collections.Generic;using Sys...
实现结果:


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)
{
PlaySong ps = new PlaySong();
ps.Inof();
Console.ReadLine();
}
}
//歌曲信息列表
class Song
{
public Song() { }
public Song(String title, string singer, int length)
{
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)
{
TimeSpan ts = new TimeSpan(0, 0, time);
Console.WriteLine(" " + (ts.Minutes).ToString().PadLeft(2, '0') + ":" + (ts.Seconds).ToString().PadLeft(2, '0'));
}
}
}
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/76390356
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)