Buscar

Abrindo conteúdo de playlist em um ListView

Código

{é muito simples fazer um procedimento para ler os 4 tipos mais usados de playlists: *.m3u, *.pls, *.rmp e *.wpl. Eu fiz este procedimento observando a estrutura destes arquivos. Estes procedimentos sao um tanto quanto arcairos e podem ser melhorados, porem funcionam com estes 4 tipos de play list. Testei todos.
Recomendações:

No Form:

inclua um TEdit --> edt1
inclua 1 TButton --> btnImportar
inclua 1 TButton --> btnAbrir
inclua um TMemo --> mmo1
propriedades:
             visible := false
             enabled := false

inclua um TListView --> lv1
propriedades:
             viewstyle := vsreport
             crie um columns com qq nome

inclua um OpenDIalog --> dlgOpen1
propriedades:
filter := 'Playlists|*.m3u;*.pls;*.rmp;*.wpl'

No Unit:
var temp: integer;

Pra se ganhar memoria, e bom trocar todas as variaveis integer para smallint. Se colocar byte, vai ter problemas de leitura para listas grandes.
Se voce manter os nome dos componentes, é só declarar as funções e proceds como privadas ou publicas, criar os eventos para os botoes e copiar os procedimentos abaixo. Boa sorte!
}


// ***************************************************************************************
// ************** PROCEDIMENTOS DE APOIO *************************************************
// ***************************************************************************************

function TForm1.Separador(s, ss: string): Boolean; // retorna verdadeiro quando encontra o separador em qq lugar da linha
var i: integer;
begin
 for i:=1 to Length(s) do
  begin
    if (copy(s, i, Length(ss)) = ss) then
      begin
        temp := i; // resgata a posicao
        Result := TRUE;
        Break;
      end
        else Result := FALSE;
  end;
end;

// ***************************************************************************************
function TForm1.LerLinha(ss1, s, ss2: string): string; // retorna o string da linha sem os separadores inicial e final
var i, j:Integer;
begin
for i:=1 to Length(s) do
   if (copy(s, i, Length(ss1)) = ss1) then break;

for j:=1 to Length(s) do
   if (copy(s, j, Length(ss2)) = ss2) then break;

if (Length(ss2) = 3) then
Result := Copy(s, i + Length(ss1), (2 * j) - i - Length(s) - 10);
if (Length(ss2) <> 3) then
Result := Copy(s, i + Length(ss1), (2 * j) - i - Length(s));
end;
// ***************************************************************************************
// ************** PROCEDIMENTOS PARA LEITURA *********************************************
// ***************************************************************************************

procedure TForm1.LerM3U(Arquivo: string; LV: TListView);
var i: integer;
    stemp, path: string;
    Lista: TListItem;
begin
 LV.Clear;
 path := ExtractFilePath(Arquivo);

 for i:=0 to (mmo1.Lines.Count - 1)do
 begin
  stemp:=mmo1.Lines.Strings[i];
  if stemp[1] <> '#' then // somente as linhas q comecam com # sao validas
  begin
  lista:=lv.Items.Add;
  lista.Caption :=(path + stemp);
 end;
end;
end;

// ***************************************************************************************

procedure TForm1.LerPLS(Arquivo: string; LV: TListView);
function LerLInha(s: string): string;
var i:integer;
begin
  for i:=0 to Length(s) do
    if s[i] = '=' then Break; // o nome ds arquivos estao depois do "="
 Result := Copy(s, i+1, Length(s));
end;
// **
var i: integer;
    stemp, path: string;
    Lista: TListItem;
begin
 LV.Clear;
 path := ExtractFilePath(Arquivo);

 for i:=0 to (mmo1.Lines.Count - 1)do
 begin
   stemp:=mmo1.Lines.Strings[i];
    if stemp[1] = 'F' then // somente as linhas que comecam com F de File sao validas
      begin
       lista:=lv.Items.Add;
       lista.Caption := (path + LerLinha(stemp));
      end;
 end; // for j
end;

// ***************************************************************************************

procedure TForm1.LerWPL(Arquivo: string; LV: TListView);
var i, j: integer;
    Lista: TListItem;
    path: string;
begin // WPL
 LV.Clear;
 path := ExtractFilePath(Arquivo);

 for j:=0 to (mmo1.Lines.Count - 1) do // conta as linhas para encontrar onde tem o tag <seq>
  begin
    if Separador(mmo1.Lines.Strings[j], '<seq>') then // quando encontrar esta tag:

    for i:=(temp - 1) to (mmo1.Lines.Count - 1) do // comeca a pegar as linhas a partir da encontrada
      begin
       if Separador(mmo1.Lines.Strings[i], '</seq>') then Break; // quando o tag for </seq>, termina
        lista:=lv.Items.Add;
        lista.Caption := path + LerLinha('<media src="', mmo1.Lines.Strings[i], '"/>');
      end;
  end;
end;

// ***************************************************************************************

procedure TForm1.LerRMP(Arquivo: string; LV: TListView);
var j: Integer;
    Lista: TListItem;

begin
 LV.Clear;
 for j:=0 to (mmo1.Lines.Count - 1) do
  begin
    if Separador(mmo1.Lines.Strings[j], '<FILENAME>') then // quando encontrar esta tag:
      begin
        lista:=lv.Items.Add;
        lista.Caption := lerlinha('<FILENAME>', mmo1.Lines.Strings[j],'</FILENAME>'); // recebe o nome do arquivo sem os tags
      end;
  end;
end;

// ***************************************************************************************
// ************** PROCEDIMENTOS DOS COMPONENTES ******************************************
// ***************************************************************************************

procedure TForm1.btnAbrirClick(Sender: TObject);
begin
  mmo1.Clear;
if dlgOpen1.Execute then
   mmo1.Lines.LoadFromFile(dlgOpen1.FileName);

// *.m3u - OK
// *.pls - OK
// *.rmp - OK
// *.wpl - OK

end;

// ***************************************************************************************

procedure TForm1.btnImportarClick(Sender: TObject);
begin

if ExtractFileExt(dlgOpen1.FileName) = '.m3u' then LerM3U(dlgOpen1.FileName, LV1);
if ExtractFileExt(dlgOpen1.FileName) = '.pls' then LerPLS(dlgOpen1.FileName, LV1);
if ExtractFileExt(dlgOpen1.FileName) = '.rmp' then LerRMP(dlgOpen1.FileName, LV1);
if ExtractFileExt(dlgOpen1.FileName) = '.wpl' then LerWPL(dlgOpen1.FileName, LV1);
// coloque uma exceção para tipos nao conhecidos
end;

// ***************************************************************************************

procedure TForm1.lv1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean); // aqui serve para mostrar o item selecionado no listview
begin
if lv1.Selected <> nil then
   edt1.Text:=lv1.Selected.Caption;
end;

// ***************************************************************************************

end.

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Arquivos
Adicionada dia: 27/09/09
Por: Paulo Pinto
Visualizada: 4129 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009