www.pudn.com > seamp3.rar > Player.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace seamp3
{
public class Player
{
private AxWMPLib.AxWindowsMediaPlayer myPlayer;
private string[] playList;
private int numOfMusic;
private int currentPlay;
private string savepath;
public int CurrentPlay
{
get
{
return currentPlay;
}
}
public int NumOfMusic
{
get
{
return numOfMusic;
}
set
{
numOfMusic = value;
}
}
public WMPLib.WMPPlayState playstate
{
get
{
return myPlayer.playState;
}
}
public void save()//保存文件
{
StreamWriter sw = new StreamWriter(savepath);
for (int i = 1; i <= NumOfMusic; i++)
{
sw.WriteLine(playList[i]);
}
sw.Close();
}
public void load() //初始化操作
{
try
{
StreamReader sr = new StreamReader(savepath);
String line;
numOfMusic = 0;
while ((line = sr.ReadLine()) != null)
{
numOfMusic++;
playList[numOfMusic] = line;
}
sr.Close();
}
catch
{
}
}
public string PlayList(int num)
{
return playList[num];
}
public Player(AxWMPLib.AxWindowsMediaPlayer mediaPlayer)
{
FileInfo t = new FileInfo("save.msg");
savepath = t.FullName;
myPlayer = mediaPlayer;
playList = new string[1001];
numOfMusic = 0;
currentPlay = 0;
load();
}
public void AddFile(string path)
{
if (numOfMusic < 1000)
{
numOfMusic++;
playList[numOfMusic] = path;
}
}
public void DelFile(int selectNum)
{
for (int i = selectNum; i <= numOfMusic - 1; i++)
{
playList[i] = playList[i + 1];
}
numOfMusic--;
}
public void play(int selectNum)
{
MessageBox.Show(playList[selectNum].ToString());
myPlayer.URL = playList[selectNum];
currentPlay = selectNum;
}
public int NextPlay(int type)
{
/* type = 0 顺序
type = 1 重复播放全部
type = 2 重复播放一首
type = 3 随机播放
*/
switch (type)
{
case 0:
currentPlay++;
if (currentPlay > NumOfMusic) return 0;
else return currentPlay;
case 1:
currentPlay++;
if (currentPlay > numOfMusic) return 1;
else return currentPlay;
case 2:
return currentPlay;
case 3:
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
currentPlay = rdm.Next() % numOfMusic;
if (currentPlay == 0) return numOfMusic;
else return currentPlay;
default:
return 0;
}
}
}
}