谁知道怎样播放MP3音频文件?有没有相关的控件或例子?问题解决立即结贴,谢谢!

解决方案 »

  1.   

    unit Main;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, CheckLst, ImgList, ComCtrls, ToolWin, Menus, MPlayer,
      ExtCtrls,shellAPI,Inifiles;
    const
      ICON_ID=1;
      MI_ICONEVENT=WM_USER+1;
      MY_MESSAGE=WM_USER+100;
      FPS=25;
    type
      TFrmMain = class(TForm)
        MainMenu1: TMainMenu;
        MP3Fix1: TMenuItem;
        P1: TMenuItem;
        L1: TMenuItem;
        H1: TMenuItem;
        ToolBar1: TToolBar;
        TBtnStop: TToolButton;
        TBtnPause: TToolButton;
        TBtnPlay: TToolButton;
        TBtnUp: TToolButton;
        TbtnNext: TToolButton;
        TBtnOpen: TToolButton;
        ImageList1: TImageList;
        OpenDialog1: TOpenDialog;
        CLBox1: TCheckListBox;
        ToolButton1: TToolButton;
        ComboBox1: TComboBox;
        ToolButton2: TToolButton;
        Panel_XS: TPanel;
        PopupMenu1: TPopupMenu;
        S1: TMenuItem;
        P2: TMenuItem;
        P3: TMenuItem;
        R1: TMenuItem;
        N1: TMenuItem;
        N2: TMenuItem;
        STB: TStatusBar;
        N3: TMenuItem;
        N4: TMenuItem;
        N5: TMenuItem;
        N6: TMenuItem;
        LPM: TPopupMenu;
        N7: TMenuItem;
        N8: TMenuItem;
        N9: TMenuItem;
        E1: TMenuItem;
        N10: TMenuItem;
        Timer1: TTimer;
        M1: TMenuItem;
        C1: TMenuItem;
        A1: TMenuItem;
        procedure TBtnOpenClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure TBtnPlayClick(Sender: TObject);
        procedure TBtnPauseClick(Sender: TObject);
        procedure TBtnStopClick(Sender: TObject);
        procedure TbtnNextClick(Sender: TObject);
        procedure TBtnUpClick(Sender: TObject);
        procedure DragDropFiles(var msg:TMessage);message WM_DROPFILES;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure FormDestroy(Sender: TObject);
        procedure N7Click(Sender: TObject);
        procedure N8Click(Sender: TObject);
        procedure N9Click(Sender: TObject);
        procedure E1Click(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure A1Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }
        procedure Play_Stats(File_Name:string);
        procedure SetUpIcon;
        procedure FreeIcon;
        procedure IconOnClick(var msg:TMessage);message MI_ICONEVENT;
      public
        { Public declarations }
      end;
      function GetStringFromFrame(frames:dWord):string;
    var
      FrmMain: TFrmMain;
      Play_Stop:Boolean;
      Roundom_Play:Boolean;
      MyIcon:TIcon;
      Apath:string;
      nt:TNOTIFYICONDATA;
    implementation
    uses About;
    {$R *.dfm}
    function pvInitMp3(hInstance,hWnd,DisplayW,DisplayH,bands:integer):integer;
    stdcall;external 'pvmp3.dll';
    procedure pvFreeMp3;stdcall;external 'pvmp3.dll';
    procedure pvGetMp3Info(pFileName:PChar;pFreq,pLen:pointer);stdcall;external 'pvmp3.dll';
    function pvPlayMp3(pFileName:PChar):integer;stdcall;external 'pvmp3.dll';
    procedure pvStopMp3;stdcall;external 'pvmp3.dll';
    procedure pvPauseMp3;stdcall;external 'pvmp3.dll';
    procedure pvResumeMp3;stdcall;external 'pvmp3.dll';
    function GetStringFromFrame(frames:dWord):string;
    var
      ttt:dWord;
      frameStr,hourStr,minuteStr,secondStr:string;
    begin
      ttt:=frames mod FPS;
      frameStr:=IntToStr(ttt);
      if Length(frameStr)<2 then frameStr:='0'+frameStr;  ttt:=(frames div FPS) mod 60;
      secondStr:=IntToStr(ttt);
      if Length(secondStr)<2 then secondStr:='0'+secondStr;  ttt:=((frames div FPS) div 60) mod 60;
      minuteStr:=IntToStr(ttt);
      if Length(minuteStr)<2 then minuteStr:='0'+minuteStr;  ttt:=(frames div FPS) div 3600;
      hourStr:=IntToStr(ttt);
      if Length(hourStr)<2 then hourStr:='0'+hourStr;  Result:=hourStr+':'+minuteStr+':'+secondStr+'.'+frameStr;
    end;procedure TFrmMain.TBtnOpenClick(Sender: TObject);
    begin
      if Opendialog1.Execute then
        CLBox1.Items.Add(Opendialog1.FileName);
    end;procedure TFrmMain.FormCreate(Sender: TObject);
    var
      ini:TInifile;
      i:integer;
    begin
        pvInitMp3(hInstance,Panel_XS.Handle,Panel_XS.Width,Panel_XS.Height,32);
        DragAcceptFiles(handle,true);
        Combobox1.Text:='默认';
        Apath:=ExtractFilePath(Paramstr(0));
        setupIcon;
        timer1.Enabled:=false;
        ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'bflist.ini');
        try
        if not FileExists(ExtractFilePath(Paramstr(0))+'bflist.ini') then
          begin
          ini.WriteInteger('Form','top',self.Top);
          ini.WriteInteger('Form','left',self.Left);
          ini.WriteInteger('Form','width',self.Width);
          ini.WriteInteger('Form','height',self.Height);
          end
          else
          begin
          self.Top:=ini.ReadInteger('Form','top',0);
          self.Left:=ini.ReadInteger('Form','left',0);
          self.Width:=ini.ReadInteger('Form','width',460);
          self.Height:=ini.ReadInteger('Form','height',384);
          for i:=0 to ini.ReadInteger('list','count',0)-1 do
            CLBox1.Items.Add(ini.ReadString('list',inttostr(i),''));
          end;
        finally
          ini.Free;
        end;
    end;
    procedure TFrmMain.TBtnPlayClick(Sender: TObject);
    begin
      Play_stats(CLBox1.Items.Strings[CLBox1.itemIndex]);
    end;procedure TFrmMain.TBtnPauseClick(Sender: TObject);
    begin
      try
        if TBtnPause.Tag=0 then
        begin
          TBtnPause.Tag:=1;
          Play_Stop:=false;
          pvPauseMp3;
        end
        else
          begin
            TBtnPause.Tag:=0;
            Play_Stop:=true;
            pvResumeMp3;
          end;
      except
        exit;
      end;
    end;procedure TFrmMain.TBtnStopClick(Sender: TObject);
    begin
      pvStopMp3;
      timer1.Enabled:=false;
    end;procedure TFrmMain.TbtnNextClick(Sender: TObject);
    var
      Play_Index:Integer;
    begin
      try
        if Roundom_Play=true then
          begin
            Play_Index:=Round(Random(CLBox1.Items.Count));
            CLBox1.ItemIndex:=Play_Index;
            Play_stats(CLBox1.Items.Strings[CLBox1.ItemIndex]);
          end;
        if Roundom_Play=false then
          begin
            if CLBox1.ItemIndex<CLBox1.Items.Count-1 then
              Play_Index:=CLBox1.ItemIndex+1
            else
              Play_Index:=0;
              CLBox1.ItemIndex:=Play_Index;
              Play_stats(CLBox1.Items.Strings[CLBox1.ItemIndex]);
          end;
      except
        exit;
      end;
    end;procedure TFrmMain.TBtnUpClick(Sender: TObject);
    var
      Play_Index:Integer;
    begin
      try
        if Roundom_Play=true then
        begin
          Play_Index:=Round(Random(CLBox1.Items.Count));
          CLBox1.ItemIndex:=Play_Index;
          Play_Stats(CLBox1.Items.Strings[CLBox1.ItemIndex]);
        end;
        if Roundom_Play=false then
        begin
          if CLBox1.ItemIndex>=1 then
            Play_Index:=CLBox1.ItemIndex-1
          else
            Play_Index:=0;
            CLBox1.ItemIndex:=Play_Index;
            Play_stats(CLBox1.Items.Strings[CLBox1.ItemIndex]);
        end;
      except
        exit;
      end;
    end;procedure TFrmMain.DragDropFiles(var msg:TMessage);
    var
      char1:array[0..128]of char;//缓冲区
      char2:pchar;
      i,Sum:Integer;
    begin
      try
        //获得拖拉的文件数目,该功能由第二个参数决定
        Sum:=DragQueryFile(msg.WParam,$FFFFFFFF,nil,0);
        char2:=@char1;
        for i:=0 to Sum-1 do
        begin
          //获取第i个拖拉文件的名字,并保存在缓冲区char2中
          DragQueryFile(msg.WParam,i,char2,128);
          CLBox1.Items.Add(char2);
        end;
        DragFinish(msg.WParam);
      except
        exit;
      end;
    end;procedure TFrmMain.SetUpIcon;
    var
      IconData:TNotifyIconData;
    begin
      MyIcon:=TIcon.Create;
      MyIcon.LoadFromFile(Apath+'123.ico');
      IconData.cbSize:=Sizeof(IconData);
      IconData.Wnd:=Handle;
      IconData.uID:=ICON_ID;
      IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
      IconData.uCallbackMessage:=MI_ICONEVENT;
      IconData.hIcon:=MyIcon.Handle;
      IconData.szTip:='Mp3Fix v1.0';
      Shell_NotifyIcon(NIM_ADD,@IconData);
    end;
      

  2.   


    procedure TFrmMain.FreeIcon;
    var
      IconData:TNotifyIconData;
    begin
      IconData.cbSize:=Sizeof(IconData);
      IconData.Wnd:=Handle;
      IconData.uID:=ICON_ID;
      Shell_NotifyIcon(NIM_DELETE,@IconData);
    end;procedure TFrmMain.IconOnClick(var msg:TMessage);
    var
      pt:TPoint;
    begin
      if GetAsyncKeyState(VK_LButton)<>0 then
        begin
          Shell_NotifyIcon(NIM_DELETE,@nt);
          showWindow(Application.Handle,SW_SHOW);
          Application.Restore;
          self.N9.Caption:='缩小为图标';
        end;
      if GetAsyncKeyState(VK_RButton)<>0 then
        begin
          GetCursorPos(pt);
          PopupMenu1.Popup(pt.X,pt.Y);
        end;
    end;procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
    var
      i:integer;
      ini:TInifile;
    begin
      FreeIcon;
      MyIcon.Free;
      ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'bflist.ini');
      ini.WriteInteger('Form','top',self.Top);
      ini.WriteInteger('Form','left',self.Left);
      ini.WriteInteger('Form','width',self.Width);
      ini.WriteInteger('Form','height',self.Height);
      for i:=0 to ini.ReadInteger('list','count',0) do
        ini.DeleteKey('list',inttostr(i));
        ini.WriteInteger('list','count',CLBox1.Items.Count);
      for i:=0 to CLBox1.Items.Count-1 do
        ini.WriteString('list',inttostr(i),CLBox1.Items.Strings[i]);
    end;
    procedure TFrmMain.FormDestroy(Sender: TObject);
    begin
      pvFreeMp3;
    end;
    procedure TFrmMain.N7Click(Sender: TObject);
    begin
      CLBox1.Items.Delete(CLBox1.ItemIndex);
    end;procedure TFrmMain.N8Click(Sender: TObject);
    begin
      CLBox1.Items.Clear;
    end;procedure TFrmMain.N9Click(Sender: TObject);
    begin
      if pos('缩小为图标',self.N9.Caption)>0 then
      begin
      with nt do
        begin
          cbSize:=Sizeof(nt);
          Wnd:=Handle;
          UID:=0;
          uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
          uCallBackMessage:=MY_MESSAGE;
          hIcon:=Icon.Handle;
          szTip:='Mp3Fix v1.0';
          hIcon:=Application.Icon.Handle;
        end;
      Application.Minimize;
      ShowWindow(Application.Handle,SW_HIDE);
      Shell_NotifyIcon(NIM_ADD,@nt);
      self.N9.caption:='正常显示';
      end
      else
      begin
        Shell_NotifyIcon(NIM_DELETE,@nt);
        ShowWindow(Application.Handle,SW_SHOW);
        Application.Restore;
        self.N9.caption:='缩小为图标';
      end;
    end;
    procedure TFrmMain.E1Click(Sender: TObject);
    begin
      close;
    end;procedure TFrmMain.Play_Stats(File_Name:string);
    var
      freq,len:integer;
      s:string;
    begin
      pvStopMp3;
      pvGetMp3Info(PChar(File_Name),@freq,@len);
      STB.Panels[0].Text:='采样率为'+inttostr(freq div 1000)+'KHz';
      s:=GetStringFromFrame(len*25);
      SetLength(s,Length(s)-3);
      Timer1.Interval:=len*1000;
      Timer1.Enabled:=true;
      STB.Panels[1].Text:='播放时间:'+s;
      STB.Panels[2].Text:=ExtractFileName(ClBox1.Items.Strings[CLBox1.ItemIndex]);  TBtnPlay.Enabled:=true;
      TBtnPause.Enabled:=true;
      TBtnStop.Enabled:=true;
      TBtnPause.Tag:=0;  pvPlayMp3(PChar(File_Name));
      TBtnPause.Tag:=0;
      Play_Stop:=True;
      Timer1.Enabled:=true;
    end;procedure TFrmMain.Timer1Timer(Sender: TObject);
    begin
      if (ComboBox1.Text='默认') or (Combobox1.Text='循环') then
      TBtnNext.Click
      else if Combobox1.Text='随机' then
      randomize;
      CLBox1.ItemIndex:=random(CLBox1.Items.Count-1);
      Play_Stats(CLBox1.Items.Strings[CLBox1.ItemIndex]);
    end;procedure TFrmMain.A1Click(Sender: TObject);
    begin
      About.AboutBox.Show;
    end;procedure TFrmMain.FormShow(Sender: TObject);
    begin
      About.AboutBox.Close;
    end;end.
      

  3.   

    我有一个DMEO,要的话发给你 [email protected]
      

  4.   

    1.很简单的,如果用到控件的话,你就用system面版上的mediaplayer,设置好filename属性为要播放的文件的全路径,然后就可以play了.
    2.利用API函数ShellExecute可以,具体为 ShellExecute('play '+xxxx.mp3),注意play后有个空格
    3.用API函数sndsoundplay,具体你可以查一下怎么使用,该函数使用的时候要先添加一个mmsystem单元
      

  5.   

    错了,上面最后一个函数是sndPlaySound^_^
      

  6.   

    To:wdswcy(一无所有)
    你提供的代码是Mp3Fix这个程序的源代码,这个程序需要pvmp3.dll这个库文件,我想要的是不需要链接库的代码,不过还是谢谢你!
      

  7.   

    To: must0001(飞鸟) 
    谢谢你,
      

  8.   

    大家都说用TMediaPlayer可以播放,我也确实播放成功了,但是声音质量非常的差,有没有人知道应该怎么优化一下程序,至少让他的声音不要停顿和噼啪作响呀
      

  9.   

    一般TMediaPlayer就可以了,效果差可能是音频文件本身的问题