namespace MyKTVClient
{
public partial class FrmOrderBySinger : Form
{
private string singerSex = "男"; // 当前选择的歌手性别
private int singerTypeId = 0; // 当前选择的歌手类型的编号
public FrmOrderBySinger()
{
InitializeComponent();
}
// 点击后,显示歌手类别
private void lvOrder_Click(object sender, EventArgs e)
{
if (lvOrder.SelectedItems[0] != null)
{
// 隐藏歌手性别,显示歌手类别
pnlSingerSex.Visible = false;
pnlSingerType.Location = pnlSingerSex.Location;
pnlSingerType.Dock = DockStyle.Fill;
pnlSingerType.Visible = true;
this.singerSex = Convert.ToString(lvOrder.SelectedItems[0].Tag); // 记录选择的性别
}
// 读取歌手类别
DBHelper dbHelper = new DBHelper();
string sql = "select * from singer_type";
try
{
// 查询数据库
SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
dbHelper.OpenConnection();//相当于con.open
SqlDataReader reader = command.ExecuteReader();
// 循环将类别读取出来添加到ListView中
lvSingerType.Items.Clear();
int i = 0;
while (reader.Read())
{
ListViewItem item = new ListViewItem();
item.Text = Convert.ToString(reader["singertype_name"]);
item.Tag = Convert.ToInt32(reader["singertype_id"]);
item.ImageIndex = i;
lvSingerType.Items.Add(item);
i++;
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("系统错误,请联系服务人员!");
}
finally
{
dbHelper.CloseConnection();
}
}
// 点击类别后,显示对应类别下的歌手列表
private void lvSingerType_Click(object sender, EventArgs e)
{
// 隐藏歌手类别,显示歌手列表
pnlSingerType.Visible = false;
pnlSingerList.Location = pnlSingerSex.Location;
pnlSingerList.Dock = DockStyle.Fill;
pnlSingerList.Visible = true;
this.singerTypeId = Convert.ToInt32(lvSingerType.SelectedItems[0].Tag); // 保存选中的类别编号
// 读取数据库,读出歌手信息
DBHelper dbHelper = new DBHelper();
StringBuilder sql = new StringBuilder();
sql.AppendFormat("select singer_id,singer_name,singer_photo_url from singer_info where singertype_id={0} and singer_gender=‘{1}‘",
this.singerTypeId,this.singerSex);
try
{
SqlCommand command = new SqlCommand(sql.ToString(),dbHelper.Connection);
dbHelper.OpenConnection();
SqlDataReader reader = command.ExecuteReader();
int imageIndex = 0; // 代表歌手头像的索引
ilSinger.Images.Clear();
// 循环读出歌手信息添加到窗体中显示
lvSinger.Items.Clear();
while (reader.Read())
{
// 将歌手头像放在ImageList控件中
string photoURL = KTVUtil.singerPhotoPath + "\\" + Convert.ToString(reader["singer_photo_url"]);
ilSinger.Images.Add(Image.FromFile(photoURL));
// 将歌手添加到ListView中
ListViewItem item = new ListViewItem();
item.Text = Convert.ToString(reader["singer_name"]);
item.Tag = Convert.ToString(reader["singer_id"]);
item.ImageIndex = imageIndex;
lvSinger.Items.Add(item);
imageIndex++;
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("系统错误,请联系服务人员!");
}
finally
{
dbHelper.CloseConnection();
}
}
// 点击歌手姓名,打开歌曲列表窗口
private void lvSinger_Click(object sender, EventArgs e)
{
// 读取数据库,读出该歌手的所有歌曲
DBHelper dbHelper = new DBHelper();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("select song_id,song_name, singer_name=‘{0}‘,song_url from song_info where singer_id={1}",
lvSinger.SelectedItems[0].Text, Convert.ToInt32(lvSinger.SelectedItems[0].Tag));
FrmSongList songList = new FrmSongList();
songList.Sql = sb.ToString();
songList.Previous = PrevioisForm.Singer; // 指定返回的窗体是按歌手点歌
songList.Show();
this.Close();
}
// 关闭当前窗体,显示主界面
private void tsbtnHome_Click(object sender, EventArgs e)
{
this.Close();
}
// 重新播放当前歌曲
private void tsbtnAgain_Click(object sender, EventArgs e)
{
PlayList.PlayAgain();
}
// 切歌
private void tsbtnCut_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要切歌吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
PlayList.CutSong(-1);
}
}
// 打开已点歌曲窗体
private void tsbtnOrdered_Click(object sender, EventArgs e)
{
FrmOrderedSongList orderedForm = new FrmOrderedSongList();
orderedForm.Show();
}
// 呼叫服务
private void tsbtnService_Click(object sender, EventArgs e)
{
MessageBox.Show("您发出了呼叫服务!");
}
// 处理返回按钮的事件
private void tsbtnExit_Click(object sender, EventArgs e)
{
if (this.pnlSingerList.Visible)
{
this.pnlSingerType.Visible = true;
this.pnlSingerList.Visible = false;
}
else if (this.pnlSingerType.Visible)
{
this.pnlSingerSex.Visible = true;
this.pnlSingerType.Visible = false;
}
else if (this.pnlSingerSex.Visible)
{
this.Close();
}
}
private void lvSinger_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
辅助类:
song类:
namespace MyKTVClient
{
enum SongPlayState
{
unplayed,played,again,cut
}
/// <summary>
/// 歌曲类
/// </summary>
class Song
{
/// <summary>
/// 歌曲名称
/// </summary>
public string SongName
{
get { return songName; }
set { songName = value; }
}
/// <summary>
/// 歌曲存放路径
/// </summary>
public string SongURL
{
get { return songURL; }
set { songURL = value; }
}
/// <summary>
/// 歌曲播放状态
/// </summary>
internal SongPlayState PlayState
{
get { return playState; }
set { playState = value; }
}
private string songName;
private string songURL;
private SongPlayState playState = SongPlayState.unplayed; // 歌曲播放状态
/// <summary>
/// 将歌曲状态改为已播放
/// </summary>
public void SetSongPlayed()
{
this.playState = SongPlayState.played;
}
/// <summary>
/// 将歌曲状态改为再拨放一次
/// </summary>
public void SetPlayAgain()
{
this.playState = SongPlayState.again;
}
/// <summary>
/// 将歌曲状态改为切歌
/// </summary>
public void SetSongCut()
{
this.playState = SongPlayState.cut;
}
}
}
PlayList类:
namespace MyKTVClient
{
/// <summary>
/// 播放列表管理
/// </summary>
class PlayList
{
private static Song[] songList = new Song[50]; // 歌曲播放列表数组
private static int songIndex = 0; // 当前播放的歌曲在数组中的索引
/// <summary>
/// 播放列表数组
/// </summary>
public static Song[] SongList
{
get { return PlayList.songList; }
}
/// <summary>
/// 当前播放歌曲的索引
/// </summary>
public static int SongIndex
{
get { return PlayList.songIndex; }
}
/// <summary>
/// 当前播放的歌曲名称
/// </summary>
/// <returns>歌曲名称</returns>
public static string PlayingSongName()
{
string songName = ""; // 歌曲名称
if (SongList[SongIndex] != null)
{
songName = SongList[SongIndex].SongName;
}
return songName;
}
/// <summary>
/// 获取当前播放的歌曲
/// </summary>
/// <returns>当前要播放的歌曲</returns>
public static Song GetPlayingSong()
{
if (SongList[songIndex] != null)
{
return SongList[songIndex];
}
else
{
return null;
}
}
/// <summary>
/// 下一首要播放的歌曲名称
/// </summary>
/// <returns>歌曲名称</returns>
public static string NextSongName()
{
string songName = ""; // 歌曲名称
if (SongList[SongIndex+1] != null)
{
songName = SongList[SongIndex+1].SongName;
}
return songName;
}
/// <summary>
/// 点播一首歌曲
/// </summary>
/// <param name="song">新点播的歌曲</param>
public static bool AddSong(Song song)
{
bool success = false;
for (int i = 0; i < SongList.Length; i++)
{
if (SongList[i] == null)
{
SongList[i] = song;
Console.WriteLine(song.SongName);
success = true;
break;
}
}
return success;
}
/// <summary>
/// 切歌
/// </summary>
/// <param name="index">要切歌曲的编号,如果是切当前播放的歌曲传入-1</param>
public static void CutSong(int index)
{
int i; // 循环变量,代表切歌的位置
if (index == -1)
{
i = SongIndex;
}
else
{
i = index; // 从切歌的位置开始,将歌曲逐个向前移一个位置
}
SongList[i].SetSongCut();
while (SongList[i] != null)
{
SongList[i] = SongList[i + 1];
i++;
// 如果到达数组最后一个元素,就将最后一个元素指向空
if (i == SongList.Length)
{
SongList[i] = null;
}
}
}
/// <summary>
/// 重放当前歌曲
/// </summary>
public static void PlayAgain()
{
if (SongList[songIndex] != null)
{
SongList[songIndex].SetPlayAgain();
}
}
/// <summary>
/// 播放下一首
/// </summary>
public static void MoveOn()
{
if (SongList[songIndex] != null && SongList[songIndex].PlayState == SongPlayState.again)
{
SongList[songIndex].SetSongPlayed();
}
else
{
songIndex++;
}
}
}
}