设计如下界面
编写代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 播放器接口
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
IPlayer ip;
MP3 m;
//MP3歌曲
private void button1_Click(object sender, EventArgs e)
{
m = new MP3();
ip = (IPlayer)m;
}
//上一首
private void button3_Click(object sender, EventArgs e)
{
if ( m is IPlayer)
{
label1.Text = "\n" + ip.Pre();
}
}
//停止
private void button4_Click(object sender, EventArgs e)
{
if (ip is IPlayer )
{
label1.Text = "\n" + ip.Stop();
}
}
//播放
private void button5_Click(object sender, EventArgs e)
{
if ( m is IPlayer)
{
label1.Text = "\n" + ip.Play();
}
}
//暂停
private void button6_Click(object sender, EventArgs e)
{
if (m is IPlayer)
{
label1.Text = "\n" + ip.Pause();
}
}
//下一首
private void button7_Click(object sender, EventArgs e)
{
if ( m is IPlayer)
{
label1.Text = "\n" + ip.Next();
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
}
}
interface IPlayer
{
string Play();
string Stop();
string Pause();
string Pre();
string Next();
}
public class MP3 : IPlayer
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
public string Play()
{
player.SoundLocation = @"f:\hello.wav";
player.Load();
player.PlayLooping();
return "正在播放MP3歌曲!";
}
public string Stop()
{
player.Stop();
return "停止播放MP3歌曲";
}
public string Pause()
{
player.Stop();
return "暂停播放MP3歌曲";
}
public string Pre()
{
player.SoundLocation = @"f:\hello2.wav";
player.Load();
player.PlayLooping();
return "播放上一首MP3歌曲";
}
public string Next()
{
player.SoundLocation = @"f:\hello.wav";
player.Load();
player.PlayLooping();
return "播放下一首MP3歌曲";
}
}
}
运行结果