woawn 发表于 2012-7-7 14:42:41

dephi怎样把多个音乐添加到WindowsMediaPlayer控件中的播放列表中

dephi怎样把多个音乐添加到WindowsMediaPlayer控件中的播放列表中
俺最近想做一个音乐播放器,现在就是想把一个文件夹里的音乐全部添加到WindowsMediaPlayer控件中的播放列表中并且把它们都显示在一个可视化的空间列表中(比如listbox),希望大家给点意见。

aminghanhua 发表于 2012-7-7 14:42:42

不好意思 这几天忙没上网 不知道楼主问题解决了没 刚才自己做了个样本 主要代码是procedure TForm1.DoSearchPathFile(qPath:   string);
var
SR:TSearchRec;
FileAttr:Integer;
begin
FileAttr:=faDirectory;
FindFirst(qPath + '\*.*',FileAttr,SR);
While   FindNext(SR)   =   0   do
begin
if   (SR.Name <> '.')   and   (SR.Name <> '..')   then
begin
    if   DirectoryExists(qPath   +   '\'   +   SR.Name)   then
    begin
      ListView1.Items.Add.Caption:=qPath + '\' + SR.Name;
      DoSearchPathFile(qPath + '\' + SR.Name);
    end;
end;
end;

FileAttr:=faAnyFile;
FindFirst(qPath + '\*.*', FileAttr,SR);
while FindNext(SR)=0 do
begin
if   (SR.Name   <>   '.')   and   (Sr.Name   <>   '..')   then
begin
   if   not(DirectoryExists(qPath   +   '\'   +   SR.Name))   then
   begin
      ListView1.Items.Add.Caption:=qPath + '\' + SR.Name;
   end;
end;
end;
end;procedure TForm1.OpenfExecute(Sender: TObject);//打开文件夹所有文件到列表
var
s:string;
begin
if SelectDirectory('请选择媒体文件目录','',s) then
DoSearchPathFile(s);
end;我把源码文件上传了可以看看这个帖子
http://bbs.fishc.com/forum.php?mod=viewthread&tid=19757&extra=

aminghanhua 发表于 2012-7-7 19:55:17

本帖最后由 aminghanhua 于 2012-7-7 21:55 编辑

楼主的意思不太清楚
1、如果用三个控件DriveComboBox1    DirectoryListBox1   FileListBox1 和一个MediaPlayer1
DriveComboBox1的 dirlist事件选择DirectoryListBox1,DirectoryListBox1的filelist事件选择FileListBox1,FileListBox1的mask属性为 *.mp3这样就全都为mp3了再在filelistbox1的onclick事件加入

mediaplayer1.FileName:=filelistbox1.FileName;
mediaplayer1.Open;等代码。

2、点击按钮打开文件夹,所有mp3文件自动添加到列表中
(1)用SelectDirectory打开文件夹要 uses   FileCtrl;加入个全局变量list1: TListItem;
(2)放入一个button1控件用来打开文件夹,一个button2按钮用来播放 一个listview1控件用来显示歌曲 和一个MediaPlayer1
(3)listview的viewstyle改为vsreport   双击listview1控件 点击add new 加入两个 caption分别为 文件名 和 路径
(4)具体代码
function AddFormDir(F: string): integer; //查找MP3文件的函数
var
MP3: integer;
S: TSearchRec;
i: Integer;
a: Boolean;
begin
mp3 := FindFirst(F, faAnyFile, s); //查找*.mp3
while mp3 = 0 do
begin
Application.ProcessMessages;
if (S.Attr and faDirectory) = 0 then
begin
a := False;
for i := 0 to Form1.ListView1.Items.Count - 1 do
begin
if GetCurrentDir + '\' + s.Name =Form1.ListView1.Items.SubItems.Strings + Form1.ListView1.items.Caption then
a:= True;
end;
if a = false then
begin
list1:=Form1.ListView1.Items.Add;
list1.Caption := s.name;
list1.SubItems.add(GetCurrentDir + '\');
form1.Button1.Enabled := true;
end;
end;
MP3 := FindNext(s);
end;
result := 1;
end;

procedure TForm1.Button1Click(Sender: TObject);//button1的onclick事件加入代码 //添加文件夹中的MP3文件
var
dir: string;
S: TSearchRec;
begin
if selectdirectory('请选择目录', '', dir) then
begin
ChDir(Dir); //设置当前路径为搜索目录
AddFormDir('*.mp3');
end;
FindClose(s);
end;


procedure TForm1.Button2Click(Sender: TObject); //播放
begin
if ListView1.ItemIndex <> -1 then //首先判断列表框中是否有内容
begin
mediaplayer1.FileName := listview1.Selected.SubItems.Strings + listview1.Selected.Caption;
begin
try
mediaplayer1.Open;
mediaplayer1.Play; //播放列表框中选择的文件
mediaplayer1.Notify := true;
except
on EMCIDeviceError do
MessageBox(Handle, '无法播放,请检查路径或者文件名是否正确!', '错误',
MB_OK + MB_ICONSTOP);
end;
end;
end;
end;

procedure TForm1.ListView1DblClick(Sender: TObject);//双击listview播放
begin
Button2Click(sender);
end;





aminghanhua 发表于 2012-7-7 22:52:24

本帖最后由 aminghanhua 于 2012-7-7 22:59 编辑

也可在Button1Click事件中 加入AddFormDir('*.wav');    AddFormDir('*.wma');等等其它音频文件
两种方法都行 测试通过 给分{:5_109:}

woawn 发表于 2012-7-8 13:04:32

aminghanhua 发表于 2012-7-7 22:52 static/image/common/back.gif
也可在Button1Click事件中 加入AddFormDir('*.wav');    AddFormDir('*.wma');等等其它音频文件
两种方法都 ...

神马意思不懂啊能不能说的具体点

woawn 发表于 2012-7-8 13:06:25

aminghanhua 发表于 2012-7-7 19:55 static/image/common/back.gif
楼主的意思不太清楚
1、如果用三个控件DriveComboBox1    DirectoryListBox1   FileListBox1 和一个MediaP ...

这位老兄首先十分的感谢你,但是你貌似没看清我想表达的意思就是我的控件是windowsMediaPlayer,而不是MediaPlayer这两个是有区别的喔

aminghanhua 发表于 2012-7-8 21:36:33

Sorry没弄明白 你说的是WindowsMediaPlayer这个ActiveX控件吧
明天跟帖吧 要睡了

woawn 发表于 2012-7-10 16:54:01

aminghanhua 发表于 2012-7-8 21:36 static/image/common/back.gif
Sorry没弄明白 你说的是WindowsMediaPlayer这个ActiveX控件吧
明天跟帖吧 要睡了

恩是啊{:5_109:}
页: [1]
查看完整版本: dephi怎样把多个音乐添加到WindowsMediaPlayer控件中的播放列表中