代码之家  ›  专栏  ›  技术社区  ›  caw

Delphi中音乐播放器的播放列表/多个数据项列表

  •  1
  • caw  · 技术社区  · 16 年前

    我想用Delphi/Pascal为我自己的音乐播放器制作一个播放列表。

    我认为最好的解决方案是用一个带有MP3文件路径的TStringList和一个带有歌曲名称的TListBox。两个列表中的匹配字符串必须位于同一位置。因此,如果用户选择TListBox中的项目5,我可以选择TStringList中位置5处的路径。

    这个很好用。

    4 回复  |  直到 16 年前
        1
  •  1
  •   K.Sandell    16 年前

    随着时间的推移,我已经做了一些这样的“列表”,最后我总是发现使类变得相当容易,但至少可以说,存储,尤其是从磁盘读取列表被证明是“具有挑战性的”。

    对于普遍接受的播放列表格式(M3U),请查看 http://schworak.com/programming/music/playlist_m3u.asp .

    Torry's的“PlayList v.0.5.1”中提供了一个VCL组件,该组件具有读取多种格式的源代码。 http://www.torry.net/quicksearchd.php?String=PlayList+v.0.5.1&Title=Yes

        2
  •  4
  •   Francesca    16 年前

    我将创建一个至少包含艺术家和标题属性的TSong类,以及一个TSongList,该TSongList使用适当的排序字段提供1个或多个排序方法(可以是泛型的)。
    当然不需要维护两个单独的字符串列表,您必须管理它们,在排序时保持同步和重新排列。。。


    当前行将直接提供这两个信息。

        3
  •  2
  •   Vivian Mills    16 年前

    一个简单的解决方案是将歌曲列表/歌曲信息实现为一个TCollection。

    通过使用集合,您可以让VCL处理加载和保存到磁盘的操作。

    例如:

    请注意,这在功能上是不完整的,我将留给你,因为我是从我的头顶写这篇文章的,所以我可能把一些事情搞砸了。这只是一个让你开始的例子。

    {...}
    interface
    
    Type
      TSongCollectionItem = class(TCollectionItem)
      public
        constructor create(Owner:TCollection); override;
        procedure assign(source : TPersistent); override;
      published
        property FileName : String read fFileName Write fFileName;
        property Artist : string read fArtist write fArtist;
        property Title : string read fTitle write fTitle;
        {...}
        property Album : string read fAlbum write fAlbum;
      end;
    
      TSongCollection = class(TOwnedCollection)
      private
        function GetItem(Index: Integer): TSongCollectionItem;
        procedure SetItem(Index: Integer; Value: TSongCollectionItem);
      public
        constructor Create(AOwner: TPersistent);
        function Add: TSongCollectionItem;
        property Songs[Index: Integer]: TSongCollectionItem read GetItem write SetItem; default;
      end;
    
      procedure SaveSongList(Songs : TSongCollection; FileName:string; Binary:boolean);
      procedure LoadSongList(Songs : TSongCollection; FileName:string; Binary:boolean);
    
    {...}
    
    implementation
    
    {...}
    
    type  
      TSongComponent = class(TComponent)
      published
        property SongList : TSongCollection read fsonglist write SetSongList;
      end;
    
      procedure SaveSongList(Songs : TSongCollection; FileName:string; Binary:boolean);
      var
        wFile : TFileStream;
        wConvert : TMemoryStream;
        wSongList : TSongComponent;
      begin
        RegisterClass(TSongComponent);
        Try
          wConvert := TMemoryStream.Create;
          wFile := TFileStream.Create(filename, fmcreate);
          wSongList := TSongComponent.create(nil);
          try
            wSongList.SongList.Assign(Songs);
            if not Binary then
            begin
              wConvert.WriteComponent(wSongList);
              wConvert.Position := 0;
              ObjectBinaryToText(wConvert, wFile);
            end
            else
              wFile.WriteComponent(wSongList);
          finally
            wConvert.Free;
            wFile.Free;
            wSongList.free;
          end;
        finally
          Unregisterclass(TSongComponent);
        end;
      end;
    
      procedure LoadSongList(Songs : TSongCollection; FileName:string; Binary:boolean);
      var
        wFile : TFileStream;
        wConvert : TMemoryStream;
        wSongList : TSongComponent;
      begin
        RegisterClass(TSongComponent);
        Try
          wConvert := TMemoryStream.Create;
          wFile := TFileStream.Create(filename, fmOpenRead);
          try
            if not Binary then
            begin
              ObjectTextToBinary(wFile, wConvert);
              wConvert.Position := 0;
              wSongList := TSongComponent(wConvert.ReadComponent(Nil));
            end
            else
              wSongList := TSongComponent(wFile.ReadComponent(Nil));
    
            if assigned(Songs) and assigned(wSongList) then
              Songs.Assign(wSongList.Songs);
    
            if assigned(wSongList) then
              wSongList.free; 
          finally
            wConvert.Free;
            wFile.Free;
          end;
        finally
          Unregisterclass(TSongComponent);
        end;
      end;
    
        4
  •  1
  •   se.    16 年前

    您可以轻松添加图标等。。。。。

    你有正确的事件来触发。

    推荐文章