频道分类

Delphi 跨平台获取文件列表

作者:admin 来源: 日期:2020/2/11 9:11:19 人气: 标签:

 
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
  FMX.ListView, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Edit, FMX.Media,
  FMX.Ani, FMX.Objects, FMX.Layouts;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    MediaPlayer1: TMediaPlayer;
    txtBilgi: TText;
    Layout1: TLayout;
    Image1: TImage;
    FloatAnimation1: TFloatAnimation;
    procedure FormCreate(Sender: TObject);
    procedure ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
    procedure Image1Click(Sender: TObject);
  private
    function GetArsHedef: String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

Uses System.IOUtils;

procedure TForm1.FormCreate(Sender: TObject);
begin
  txtBilgi.Text := TPath.GetDirectoryName(GetArsHedef());
  FloatAnimation1.StartValue := Form1.Width;
{$IFDEF ANDROID}
  FullScreen := True;
{$ENDIF}
end;

function TForm1.GetArsHedef(): String;
const
  DosyaAdi: String = 'Gorusme.mp3';
begin
{$IFDEF ANDROID}
  Result := TPath.Combine(TPath.GetSharedMusicPath, DosyaAdi);
{$ELSE}
{$IFDEF IOS}    { /private/var/mobile/Applications/<application ID> }
  Result := TPath.GetHomePath + '/Documents/' + DosyaAdi;
{$ELSE}    { Win=C:\Users\Public\Music }
  Result := TPath.Combine(TPath.GetSharedMusicPath, DosyaAdi);
{$ENDIF}
{$ENDIF}
end;

procedure TForm1.Image1Click(Sender: TObject);
var
  Konum: string;
  AramaSecenegi: TSearchOption;
begin
  Konum := TPath.GetDirectoryName(GetArsHedef());
  if not TDirectory.Exists(Konum) then
  begin
    ShowMessage('');
    Exit;
  end;

  AramaSecenegi := TSearchOption.soAllDirectories;
  // LSearchOption := TSearchOption.soTopDirectoryOnly;
  ListView1.Items.Clear;
  for Konum in TDirectory.GetFiles(Konum, '*.mp3', AramaSecenegi) do
    With ListView1.Items.Add do
    begin
      Text := (ListView1.Items.Count).ToString + ' - ' + TPath.GetFileName(Konum);
      Detail := Konum;
    end;
end;

procedure TForm1.ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
begin
  txtBilgi.Text := ': ' + AItem.Text;
  MediaPlayer1.FileName := AItem.Detail;
  MediaPlayer1.Play;
  FloatAnimation1.StopValue := -txtBilgi.Width - Image1.Width;
  FloatAnimation1.Start;
end;

end.