|
发表于 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[i].SubItems.Strings[0] + Form1.ListView1.items[i].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[0] + 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;
复制代码
|
|