无损音乐下载器 Delphi版

无损音乐下载器 Delphi版

先上图


完整版源码下载地址:

http://download.csdn.net/detail/guoxuepeng123/9376720


searchMusic.pas 搜索界面

unit searchMusic;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, StdCtrls,msxml,ExtCtrls,IdURI,Httpapp,UrlMon,ShellAPI,
  sLabel, xmldom, XMLIntf, msxmldom, XMLDoc,StrUtils;

type
  TsearchForm = class(TForm)
    edt1: TEdit;
    lbl2: TLabel;
    lv1: TListView;
    slb1: TsLabel;
    xmldcmnt1: TXMLDocument;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    btn6: TButton;
    btn7: TButton;
    btn8: TButton;
    slb2: TsLabel;
    lbl1: TLabel;
    procedure lv1DblClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure phbMusicList();
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);
    procedure btn7Click(Sender: TObject);
    procedure btn8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }    
    HttpReq: IXMLHttpRequest;
  end;

var
  searchForm: TsearchForm;
  phbType:string;

implementation
       uses superobject,download,Constant;
{$R *.dfm}

 //文件下载  
function DownloadFile(Source, Dest: string): Boolean;  
begin   
  try   
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;   
    except   
      Result := False;   
    end;   
  end;  



procedure TsearchForm.lv1DblClick(Sender: TObject);
var
  songid:string;
begin
  try
      songid:=lv1.Items.Item[lv1.Selected.Index].Caption;
      Constant.download_songid:=songid;
      if dlForm<>nil then
        dlForm.hide;
      application.CreateForm(TdlForm, dlForm);
      dlForm.Show;
   except
      ShowMessage('请选择要下载的歌曲!');
   end;
end;
function ReversePos(SubStr, S: String): Integer;
var
  i : Integer;
begin
  i := Pos(SubStr, ReverseString(S));
  if i > 0 then i := Length(S) - i - Length(SubStr) + 2;
  Result := i;
end;

procedure TsearchForm.btn1Click(Sender: TObject);
var
  i,count,EndPos,len: Integer;
  url,content,musicName,p,m,path,param: string;
  WItem:TListItem;
  jo: ISuperObject;
  songList:TSuperArray;
begin
  if edt1.Text<>'' then
  begin
    lv1.Clear;
    slb2.Caption:='';
    musicName:=HTTPEncode(UTF8Encode(edt1.Text));
    HttpReq:=CoXMLHTTPRequest.Create;
    url := 'http://sug.music.baidu.com/info/suggestion?word='+musicName+'&format=json&version=2&from=0';
    HttpReq.open('get', Url, False, EmptyParam, EmptyParam);
    HttpReq.send(EmptyParam);//开始搜索
    content := HttpReq.responseText;
    jo := SO(content);
    songList := jo['data'].o['song'].AsArray; 
    if songList.Length=0 then
    begin
      ShowMessage('未找到歌曲【'+edt1.Text+'】');
      lv1.Clear;
    end;
    if songList.Length>0 then
    begin
        for i := 0 to songList.Length-1 do
        begin   
              WItem:=lv1.Items.Add;
              WItem.Caption:=songList[i].O['songid'].AsString;
              WItem.SubItems.Add(songList[i].O['songname'].AsString);
              WItem.SubItems.Add(songList[i].O['artistname'].AsString);
       end;
    end;
  end
  else
  begin
    ShowMessage('请输入歌曲名称!');
  end;
end;

procedure TsearchForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if dlForm<>nil then
      dlForm.Hide;
end;

procedure TsearchForm.phbMusicList();
 var
  i,count,EndPos,len: Integer;
  url,content,musicName,p,m,path,param: string;
  WItem:TListItem;
  jo: ISuperObject;
  songList:TSuperArray;
begin
    lv1.Clear;
    musicName:=HTTPEncode(UTF8Encode(edt1.Text));
    HttpReq:=CoXMLHTTPRequest.Create;
    url := 'http://tingapi.ting.baidu.com/v1/restserver/ting?size=100&type='+phbType+'&format=json&method=baidu.ting.billboard.billList';
    HttpReq.open('get', Url, False, EmptyParam, EmptyParam);
    HttpReq.send(EmptyParam);//开始搜索
    content := HttpReq.responseText;
    jo := SO(content);
    slb2.Caption:= '更新时间:'+jo['billboard'].o['update_date'].AsString;
    songList := jo['song_list'].AsArray;
    if songList.Length=0 then
    begin
      ShowMessage('未找到歌曲【'+edt1.Text+'】');
      lv1.Clear;
    end;
    if songList.Length>0 then
    begin
        for i := 0 to songList.Length-1 do
        begin
              WItem:=lv1.Items.Add;
              WItem.Caption:=songList[i].O['song_id'].AsString;
              WItem.SubItems.Add(songList[i].O['title'].AsString);
              WItem.SubItems.Add(songList[i].O['artist_name'].AsString);
       end;
    end;
end;
procedure TsearchForm.btn2Click(Sender: TObject);
begin
  phbType:='1';
  searchForm.phbMusicList();
end;

procedure TsearchForm.btn3Click(Sender: TObject);
begin
  phbType:='2';
  searchForm.phbMusicList();
end;
procedure TsearchForm.btn4Click(Sender: TObject);
begin
  phbType:='21';
  searchForm.phbMusicList();
end;

procedure TsearchForm.btn5Click(Sender: TObject);
begin
  phbType:='22';
  searchForm.phbMusicList();
end;

procedure TsearchForm.btn6Click(Sender: TObject);
begin
  phbType:='23';
  searchForm.phbMusicList();
end;

procedure TsearchForm.btn7Click(Sender: TObject);
begin
  phbType:='24';
  searchForm.phbMusicList();
end;

procedure TsearchForm.btn8Click(Sender: TObject);
begin
  phbType:='25';
  searchForm.phbMusicList();
end;

end.

download.pas 下载界面


unit download;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, StdCtrls,msxml,ExtCtrls,IdURI,Httpapp,UrlMon,ShellAPI,
  sLabel, xmldom, XMLIntf, msxmldom, XMLDoc,StrUtils, sEdit, sGroupBox,
  sButton, acImage,Jpeg;

type
  TdlForm = class(TForm)
    slb1: TsLabel;
    slb3: TsLabel;
    grp1: TsGroupBox;
    edt1: TsEdit;
    btn1: TsButton;
    grp2: TsGroupBox;
    edt2: TsEdit;
    btn2: TsButton;
    grp3: TsGroupBox;
    btn3: TsButton;
    btn4: TsButton;
    btn5: TsButton;
    img1: TsImage;
    idhtp1: TIdHTTP;
    slb2: TsLabel;
    btn6: TsButton;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure getMusicInfo();
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  dlForm: TdlForm;
  musicPath,lyPath:string;
  HttpReq: IXMLHttpRequest;
  rate:string;
  musicType:string;
implementation
   uses Constant,superobject;
{$R *.dfm}

 //文件下载  
function DownloadFile(Source, Dest: string): Boolean;  
begin   
  try
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;   
    except   
      Result := False;
    end;   
  end;
function change_length(songlength: integer): string;//将单位为秒的时间长度转化
var                                                        //为00:00:00的时间格式的字符串
s:string; //定义S为字符串变量
hours,minutes,seconds:integer;//定义hours,minutes,seconds为整形变量
begin
   songlength:=songlength div 1000;
   seconds:= songlength mod 60;
   songlength:=songlength div 60;
   minutes:=songlength mod 60;
   hours:=songlength div 60;
   if strlen(pchar(inttostr(hours)))=1 then   //若小时数为个位数
      s:='0'+inttostr(hours)                   //则在前面补0,即0X形式
   else
      s:=inttostr(hours);                     //不然,为xx形式
   s:=s+':';
   if strlen(pchar(inttostr(minutes)))=1 then
      s:=s+'0'+inttostr(minutes)
   else
      s:=s+inttostr(minutes);
   s:=s+':';
   if strlen(pchar(inttostr(seconds)))=1 then
      s:=s+'0'+inttostr(seconds)
   else
      s:=s+inttostr(seconds);
   change_length:=s;
end;
procedure TdlForm.getMusicInfo();
var
  i,count,EndPos,size: Integer;
  url,content,sizeStr,p,m,path,param,songid,downloadurl,timeStr: string;
  jo: ISuperObject;
  songList:TSuperArray;
  imagestream:TMemoryStream;
  jpg:TJpegImage;
begin
  songid:=Constant.download_songid;      
  HttpReq:=CoXMLHTTPRequest.Create;
  url := 'http://music.baidu.com/data/music/fmlink?songIds='+songid+'&rate='+rate+'&format=json&type='+musicType;
  HttpReq.open('get', Url, False, EmptyParam, EmptyParam);
  HttpReq.send(EmptyParam);//开始搜索
  content := HttpReq.responseText;
  jo := SO(content);
  if jo['errorCode'].AsString='22000' then
  begin
    songList := jo['data'].o['songList'].AsArray;
    if songList.Length=0 then
    begin
      ShowMessage('未找下载链接!');
    end;
    if songList.Length>0 then
    begin
        for i := 0 to songList.Length-1 do
        begin
            slb1.Caption:=songList[i].O['songName'].AsString;
            if songList[i].O['size'].AsString<>'' then
            begin                                    
              size:= strtoint(songList[i].O['size'].AsString);
              sizeStr:=formatfloat('0.00',size*100/(1024*1024)/100);
            end
            else
            begin
              sizeStr:='0';
            end;
            timeStr:='0';
            if songList[i].O['time'].AsString<>'' then
            begin
              timeStr:=change_length(strtoint(songList[i].O['time'].AsString)*1000);
            end;
            slb2.Caption:=' 时长:'+timeStr;
            slb3.Caption:=songList[i].O['artistName'].AsString+'  '+sizeStr+'MB / '+songList[i].O['format'].AsString+' / '+songList[i].O['rate'].AsString+'kbps';
            m:=songList[i].O['songName'].AsString+'-'+songList[i].O['artistName'].AsString+'.'+songList[i].O['format'].AsString;
            path:=songList[i].O['songName'].AsString+'-'+songList[i].O['artistName'].AsString+'.lrc';
            musicPath:=ExtractFilePath(Application.Exename)+'Songs\'+m;
            lyPath:=ExtractFilePath(Application.Exename)+'Lyrics\'+path;
            edt1.Text:= songList[i].O['songLink'].AsString;
            edt2.Text:= 'http://music.baidu.com'+songList[i].O['lrcLink'].AsString;
            btn1.Enabled:=true;
            btn2.Enabled:=true;
            if songList[i].O['songLink'].AsString='' then
            begin
              edt1.Text:= '未找到歌曲下载链接,请尝试其他音质。';
              btn1.Enabled:=false;
            end;
            if songList[i].O['lrcLink'].AsString='' then
            begin
              edt2.Text:= '未找到歌词下载链接,请尝试其他音质。';
              btn2.Enabled:=false;
            end;      
            //显示歌曲图片
            if songList[i].O['songPicBig'].AsString<>'' then
            begin
              imagestream := TMemoryStream.Create();
              jpg:=TJpegImage.Create;
              idhtp1.Get(songList[i].O['songPicBig'].AsString,imagestream);
              imagestream.Position:=0;
              jpg.LoadFromStream(imagestream);
              img1.Picture.Assign(jpg);
            end;
        end;
    end;
  end
  else
  begin
    ShowMessage('下载失败!');
  end;
end;
procedure TdlForm.FormCreate(Sender: TObject);
begin
    rate:='320';
    musicType:='flac';
    dlForm.getMusicInfo();
end;

procedure TdlForm.btn1Click(Sender: TObject);
begin
      ShellExecute(Handle, 'open', 'IExplore.exe', PAnsiChar(edt1.Text), nil, SW_SHOWNORMAL);
end;

procedure TdlForm.btn2Click(Sender: TObject);
begin
     ShellExecute(Handle, 'open', 'IExplore.exe', PAnsiChar(edt2.Text), nil, SW_SHOWNORMAL);
end;

procedure TdlForm.btn3Click(Sender: TObject);
begin
    rate:='128';         
    musicType:='mp3';
    dlForm.getMusicInfo();
end;

procedure TdlForm.btn4Click(Sender: TObject);
begin
    rate:='192';             
    musicType:='mp3';
    dlForm.getMusicInfo();
end;

procedure TdlForm.btn5Click(Sender: TObject);
begin
    rate:='320';
    musicType:='mp3';
    dlForm.getMusicInfo();
end;

procedure TdlForm.btn6Click(Sender: TObject);
begin  
    rate:='320';
    musicType:='flac';
    dlForm.getMusicInfo();
end;

end.


调用插件superobject.pas、superxmlparser.pas

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值