Bien installer un API [Résolu]

Also know as 269 Messages postés samedi 13 décembre 2003Date d'inscription 22 novembre 2010 Dernière intervention - 19 avril 2007 à 17:59 - Dernière réponse : f0xi 4304 Messages postés samedi 16 octobre 2004Date d'inscription 9 mars 2018 Dernière intervention
- 19 avril 2007 à 18:59
Bonjour a tous,


Bon alors apres une longue surune certaine erreur enfin resolu :-), on m'a conseille d'utiliser un nouvel API.

Je souhaiterais utiliser apparemment "Winamp API".

La seule chose que j'ai pu trouver pour delphi c'est cela :

"unit WinampApi;


    interface

    uses Windows,Messages,Classes,SysUtils;

    type

    TWinampApi = class

    private

    WinampHnd : THandle;

    function ConvertTime(n: integer;m : integer): string;

    function ConvertTimeTool(n: integer): string;

    function split(seperator: Char; text: String; var list: TStringList): Integer;

    public

    WinampPath : String;

    SongLengthParseTime : Boolean;

    SongPosParseTime : Boolean;

    constructor Create(WPath : String);

    function getWhatsPlaying():String;

    function getSongState():String;

    function getSongPosition():String;

    function getSongLength():String;

    function getSongSampleRate():String;

    function getSongBitRate():String;

    function getSongChannels():String;

    function getPlayListPosition():String;

    function getPlayListLength():String;

    procedure getPlayList(var PlayList:TStringList;var FileList:TStringList);

    procedure PlaySong(Mp3Name:string);

    procedure AddToPlayList(Mp3Name:string);

    procedure Play();

    procedure Stop();

    procedure Pause();

    procedure NextTrack();

    procedure PreviousTrack();

    procedure Forward5Seconds();

    procedure Back5Seconds();

    procedure StartOfPlayList();

    procedure VolumeUp();

    procedure VolumeDown();

    procedure FadeOutStop();

    end;

    implementation

    { ----- PUBLIC VOID/NULL RETURN METHODS ---- }

    constructor TWinampApi.Create(WPath : String);

    begin

    SongLengthParseTime := False;

    SongPosParseTime := False;

    WinampPath := WPath;

    WinampHnd := FindWindow(\'Winamp v1.x\', nil);

    end;

    procedure TWinampApi.PlaySong(Mp3Name:String);

    begin

    WinExec(PChar(\'"\'+WinampPath+\'\\winamp.exe" "\'+Mp3Name+\'"\'),SW_SHOW);

    end;

    procedure TWinampApi.AddToPlayList(Mp3Name:String);

    begin

    WinExec(PChar(\'"\'+WinampPath+\'\\winamp.exe" /ADD "\'+Mp3Name+\'"\'),SW_SHOW);

    end;

    procedure TWinampApi.Play();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40045, 0);

    end;

    procedure TWinampApi.Stop();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40047, 0);

    end;

    procedure TWinampApi.Pause();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40046, 0);

    end;

    procedure TWinampApi.NextTrack();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40048, 0);

    end;

    procedure TWinampApi.PreviousTrack();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40044, 0);

    end;

    procedure TWinampApi.Forward5Seconds();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40148, 0);

    end;

    procedure TWinampApi.Back5Seconds();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40144, 0);

    end;

    procedure TWinampApi.StartOfPlayList();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40154, 0);

    end;

    procedure TWinampApi.VolumeUp();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40058, 0);

    end;

    procedure TWinampApi.VolumeDown();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40059, 0);

    end;

    procedure TWinampApi.FadeOutStop();

    begin

    SendMessage(WinampHnd, WM_COMMAND, 40147, 0);

    end;

    function TWinampApi.getWhatsPlaying():String;

    var

    TitleLen: integer;

    TempInt : Integer;

    TempStr: String;

    begin

    TempStr := \'Winamp isn\'\'t running\';

    if WinampHnd <> 0 then

    begin

    // Get wHnd text

    TitleLen := GetWindowTextLength(WinampHnd)+2;

    SetLength(TempStr,TitleLen);

    GetWindowText(WinampHnd,Pchar(TempStr),TitleLen);

    SetLength(TempStr,Length(TempStr));

    // Remove \'- Winamp\' part

    TempInt := Pos(\'- Winamp\',TempStr);

    TempStr := Copy(TempStr,0,TempInt -2); // Knock of space and -

    end;


    result := TempStr;

    end;

    procedure TWinampApi.getPlayList(var PlayList:TStringList;var FileList:TStringList);

    var

    Buffer: string;

    Stream: TFileStream;

    FileStr : String;

    TempList : TStringList;

    TempListLen : Integer;

    TempStr,TempStr2 : String;

    TempPos,TempPos2 : Integer;

    i : Integer;

    begin

    SendMessage(WinampHnd,WM_USER,0,120);

    Stream := TFileStream.Create(WinampPath+\'\\winamp.m3u\', fmShareDenyNone);

    try

    SetLength(buffer, Stream.Size);

    Stream.Read(Buffer[1], Stream.Size);

    FileStr := Buffer;

    finally

    Stream.Free;

    end;

    TempList := TStringList.Create;

    TempListLen := Split(\'#\',FileStr,TempList);

    // Start from 1 to cut out \'#EXTM3U\'. Grab Name and Filename

    for i:=2 to TempListLen -1 do

    begin

    TempPos := Pos(\',\',TempList.Strings[i]);

    TempPos2 := Pos(#13,TempList.Strings[i]);

    TempStr := Copy(TempList.Strings[i],TempPos +1,(TempPos2 -1) - TempPos);

    TempStr2 := Copy(TempList.Strings[i],TempPos2 +2,(Length(TempList.Strings[i]) -1) - (TempPos2 +2));

    PlayList.Add(TempStr);

    FileList.Add(TempStr2);

    end;

    TempList.Free;

    end;

    { ----- PUBLIC RETURN METHODS ---- }

    function TWinampApi.getSongState():String;

    var

    SongState : Word;

    SongStateStr : String;

    begin

    SongState := SendMessage(WinampHnd,WM_USER,0,104);

    case SongState of

    1: SongStateStr:= \'playing\';

    3: SongStateStr:= \'paused\';

    0: SongStateStr:= \'stopped\'

    else

    SongStateStr := \'unknown\';

    end;

    Result := SongStateStr;

    end;

    function TWinampApi.getSongPosition():String;

    var

    TempInt : Integer;

    begin

    TempInt := SendMessage(WinampHnd,WM_USER,0,105);


    if SongPosParseTime then

    Result := ConvertTime(TempInt,1000)

    else

    Result := IntToStr(TempInt);

    end;

    function TWinampApi.getSongLength():String;

    var

    TempInt : Integer;

    begin

    TempInt := SendMessage(WinampHnd,WM_USER,1,105);


    if SongLengthParseTime then

    Result := ConvertTime(TempInt,1)

    else

    Result := IntToStr(TempInt);

    end;

    function TWinampApi.getSongSampleRate():String;

    begin

    Result := IntToStr(SendMessage(WinampHnd,WM_USER,0,126));

    end;

    function TWinampApi.getSongBitRate():String;

    begin

    Result := IntToStr(SendMessage(WinampHnd,WM_USER,1,126));

    end;

    function TWinampApi.getSongChannels():String;

    begin

    Result := IntToStr(SendMessage(WinampHnd,WM_USER,2,126));

    end;

    function TWinampApi.getPlayListPosition():String;

    var

    TempInt : Integer;

    begin

    TempInt := SendMessage(WinampHnd,WM_USER,0,125);

    if StrToInt(getPlayListLength()) <1 then

    Result := IntToStr(TempInt)

    else

    Result := IntToStr(TempInt +1);

    end;

    function TWinampApi.getPlayListLength():String;

    begin

    Result := IntToStr(SendMessage(WinampHnd,WM_USER,0,124));

    end;

    { ----- PRIVATE METHODS ----- }

    function TWinampApi.ConvertTime(n: integer;m : integer): string;

    begin

    n := n div m;

    result := ConvertTimeTool(n div 60) + \':\' + ConvertTimeTool(n mod 60);

    end;

    function TWinampApi.ConvertTimeTool(n: integer): string;

    begin

    if n < 10 then

    result := \'0\' + inttostr(n)

    else

    result := inttostr(n);

    end;

    function TWinampApi.split(seperator: Char; text: String; var list: TStringList): Integer;

    var

    mypos, number: Integer;

    begin

    number:=0;

    if Length(text) > 0 then

    begin

    if text[Length(text)] <> seperator then

    text:=text+seperator;

    while(Pos(String(seperator),text))>0 do

    begin

    mypos:=Pos(String(seperator),text);

    list.Add(Copy(text,1,mypos-1));

    text:=Copy(text,mypos+1,Length(text)-mypos);

    Inc(number);

    end;

    end;

    Result:=number;

    end;

    end."

----------------

N'y connaissan strictement rien en "installation" d'API, pourriez-vous
m'indiquer les demarches a suivre pour pouvoir utiliser autre chose que
ce fameux TMediaPlayer

Les composats API les plus utilises seraient apparemment ceux-la : Bass, FMod, WinAmp API, DSEngine..


@+
Afficher la suite 

Votre réponse

1 réponse

Meilleure réponse
f0xi 4304 Messages postés samedi 16 octobre 2004Date d'inscription 9 mars 2018 Dernière intervention - 19 avril 2007 à 18:59
3
Merci
je te conseil plutot de te focaliser sur Bass qui est plutot pas mal. le probleme avec ce code c'est que tu control WinAmp ... :(

j'avais commencer un player avec bass, voici le code, il se peu qu'il y ai encore des erreurs etc... :

unit UBassPlayer;

interface

uses Windows, SysUtils, Classes, ExtCtrls, Controls, BassAPIv22;

const
  PE_NO_ERROR             = $0000;
  PE_API_NOT_ACTIVATED    = $0001;
  PE_CREATE_STREAM_ERROR  = $0002;
  PE_INDEX_OVERLIMITS     = $0003;
  PE_PLAYLIST_IS_EMPTY    = $0004;

type
  TTimeCode = record
     M,S : word;
  end;

  TBassPlayerState  = (psPlay, psStop, psPause, psSoundMute);
  TBassPlayerStates = set of TBassPlayerState;

  TPlayingEvent = procedure (Sender : TObject;
                      const MusicPos, MusicLength : TTimeCode;
                      const LeftLevel,RightLevel : integer) of object;

  TPlayerErrorEvent   = procedure (Sender : TObject; const ErrorCode : integer) of object;

  TBassPlayer = class(TComponent)
  private
     fBassActive  : Boolean;

     fTimer       : TTimer;

     fChannel     : HSTREAM;

     fMusicLength : TTimeCode;
     fMusicPos    : TTimeCode;

     fMuteVolume  : cardinal;

     fFiles       : TStringList;
     fTrackIndex  : integer;

     fPlayerStates: TBassPlayerStates;

     fHandle      : THandle;

     fOnPlaying   : TPlayingEvent;
     fOnStop      : TNotifyEvent;
     fOnPause     : TNotifyEvent;
     fOnSoundMute : TNotifyEvent;
     fOnError     : TPlayerErrorEvent;

     procedure DoTimer(Sender : TObject);
     procedure fSetFiles(Val : TStringList);
     procedure fSetTrackIndex(Val : integer);
     function fGetTrackIndex : integer;
     function fGetTrackName  : string;

  protected
     procedure PlayQuery; virtual;
     procedure StopQuery; virtual;
     procedure PauseQuery; virtual;
     procedure SoundMuteQuery; virtual;
     procedure ErrorQuery(const ErrorCode : integer); virtual;

  published
     property Files        : TStringList read fFiles         write fSetFiles;
     property TrackIndex   : integer     read fGetTrackIndex write fSetTrackIndex;
     property TrackName    : string      read fGetTrackName;

     property MusicPos     : TTimeCode read fMusicPos;
     property MusicLength  : TTimeCode read fMusicLength;

     property OnPlaying   : TPlayingEvent     read fOnPlaying   write fOnPlaying;
     property OnStop      : TNotifyEvent      read fOnStop      write fOnStop;
     property OnPause     : TNotifyEvent      read fOnPause     write fOnPause;
     property OnSoundMute : TNotifyEvent      read fOnSoundMute write fOnSoundMute;
     property OnError     : TPlayerErrorEvent read fOnError     write fOnError;

  public
     constructor Create(AOwner : TComponent); override;
     destructor Destroy; override;

     procedure Play;
     procedure Stop;
     procedure Pause;
     procedure SoundMute;

     procedure NextTrack;
     procedure PreviousTrack;
     procedure JumpToTrack(const Index : integer);

     procedure SetVolume(const Vol : cardinal);

     procedure SetMusicPos(const TC : TTimeCode); overload;
     procedure SetMusicPos(const TC : integer); overload;

     function IsPlaying    : boolean;
     function IsStopped    : boolean;
     function IsPaused     : boolean;
     function IsSoundMuted : boolean;
     function IsCurrentTrackExists : boolean;

  end;

function TimeCode(const M,S : word) : TTimeCode; overload;
function TimeCode(const I : Integer) : TTimeCode; overload;
function TimeCodeToInt(const TC : TTimeCode) : integer;
function TimeCodeToStr(TC : TTimeCode) : string;

implementation

{ ---- }

function Clamp(const V,AMin,AMax : integer) : integer; overload;
begin
  if V < AMin then
     result := AMin
  else
  if V > AMax then
     result := AMax
  else
     result := V;
end;

function TimeCode(const M,S : word) : TTimeCode;
begin
  result.M := M;
  result.S := S;
end;

function TimeCode(const I : Integer) : TTimeCode;
begin
  result.M := I div 60;
  result.S := I mod 60;
end;

function TimeCodeToInt(const TC : TTimeCode) : integer;
begin
  result := (TC.M*60) + TC.S;
end;

function TimeCodeToStr(TC : TTimeCode) : string;
begin
  result := format('%.2d:%.2d',[TC.M,TC.S]);
end;

{ ---- }

type
  EBASSBADVERSION = class(Exception);
  EBASSINITERROR  = class(Exception);

{ ---- }

constructor TBassPlayer.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);

  fPlayerStates := [psStop];

  fTimer          := TTimer.Create(Self);
  fTimer.Interval := 45; {~25 CPS}
  fTimer.Enabled  := false;
  fTimer.OnTimer  := DoTimer;

  fFiles          := TStringList.Create;

  fMusicPos       := TimeCode(0,0);
  fMusicLength    := TimeCode(0,0);

  fTrackIndex     := -1;

  fBassActive     := not (BASS_GetVersion <> DWORD(MAKELONG(2,2)));
  if not fBassActive then
  begin
    Raise EBASSBADVERSION.Create('Ce composant a besoin de l''API BASS v2.2.');
  end else
  begin
    fBassActive  := BASS_Init(-1, 48000, BASS_DEVICE_LEAVEVOL, (AOwner as TWinControl).Handle, nil);
    if not fBassActive then
       Raise EBASSINITERROR.Create('Erreur d''initialisation de l''API BASS.');
  end;
end;

destructor TBassPlayer.Destroy;
begin
  Stop;
  fTimer.Enabled := false;
  BASS_StreamFree(fChannel);
  BASS_Free;
  fFiles.Free;
  fTimer.Free;
  inherited Destroy;
end;

{ ------ }

procedure TBassPlayer.DoTimer(Sender : TObject);
begin
  PlayQuery;

  if TimeCodeToInt(fMusicPos) >= TimeCodeToInt(fMusicLength) then
     Stop;
end;

procedure TBassPlayer.fSetFiles(Val : TStringList);
begin
  fFiles.Assign(val);
end;

procedure TBassPlayer.fSetTrackIndex(Val : integer);
begin
  if (Val < 0) or (Val > fFiles.Count-1) then
  begin
    ErrorQuery(PE_INDEX_OVERLIMITS);
    exit;
  end;

  if (fFiles.Count = 0) then
  begin
    fTrackIndex := -1;
    ErrorQuery(PE_PLAYLIST_IS_EMPTY);
    exit;
  end;

  if (fTrackIndex <> Val) then
  begin
    fTrackIndex := Val;
  end;
end;

function TBassPlayer.fGetTrackIndex : integer;
begin
  if fFiles.Count = 0 then
  begin
    fTrackIndex := -1;
  end;
  result := fTrackIndex;
end;

function TBassPlayer.fGetTrackName : string;
begin
  result := '';
  if TrackIndex <> -1 then
    result := Files[TrackIndex];
end;

{ ------ }

const
    VRatio = 100/32768;

procedure TBassPlayer.PlayQuery;
var VL : DWord;
    LL,LR : integer;
begin
  if Assigned(fOnPlaying) then
  begin
    VL  := BASS_ChannelGetLevel(fChannel);

    LL  := clamp(trunc(VRatio * LOWORD(VL)),0,100);
    LR  := clamp(trunc(VRatio * HIWORD(VL)),0,100);

    fMusicPos := TimeCode(trunc(BASS_ChannelBytes2Seconds(fChannel, BASS_ChannelGetPosition(fChannel))));
    fOnPlaying(Self, fMusicPos, fMusicLength, LL, LR);
  end;
end;

procedure TBassPlayer.StopQuery;
begin
  if Assigned(fOnStop) then
     fOnStop(Self);
end;

procedure TBassPlayer.PauseQuery;
begin
  if Assigned(fOnPause) then
     fOnPause(Self);
end;

procedure TBassPlayer.SoundMuteQuery;
begin
  if Assigned(fOnSoundMute) then
     fOnSoundMute(Self);
end;

procedure TBassPlayer.ErrorQuery(const ErrorCode : integer);
begin
  if Assigned(fOnError) then
     fOnError(Self,ErrorCode);
end;

{ ------ }

procedure TBassPlayer.Play;
var FN : string;
    TC : integer;
begin
  if (not fBassActive) or (TrackIndex = -1) then
  begin
    ErrorQuery(PE_API_NOT_ACTIVATED);
    Exit;
  end;

  Stop;

  if (not IsPlaying) and IsCurrentTrackExists then
  begin
    FN := fGetTrackName;
    fChannel := BASS_StreamCreateFile(False, pchar(FN), 0, 0, BASS_STREAM_PRESCAN);
    if fChannel = 0 then
    begin
      ErrorQuery(PE_CREATE_STREAM_ERROR);
    end else
    begin
      fPlayerStates  := fPlayerStates - [psStop,psPause] + [psPlay];
      fTimer.Enabled := true;
      BASS_ChannelPlay(fChannel,FALSE);
      fMusicLength := TimeCode(trunc(BASS_ChannelBytes2Seconds(fChannel,BASS_ChannelGetLength(fChannel))));
    end;
  end;
end;

procedure TBassPlayer.Stop;
begin
  if not fBassActive then
  begin
    ErrorQuery(PE_API_NOT_ACTIVATED);
    Exit;
  end;

  if not IsStopped then
  begin
    fPlayerStates := fPlayerStates - [psPlay,psPause] + [psStop];
    BASS_ChannelStop(fChannel);
    fTimer.Enabled := false;
    BASS_StreamFree(fChannel);
    fMusicPos    := TimeCode(0,0);
    fMusicLength := TimeCode(0,0);
    if Assigned(fOnPlaying) then
       fOnPlaying(Self, fMusicPos, fMusicLength, 0, 0);
    StopQuery;
  end;
end;

procedure TBassPlayer.Pause;
begin
  if not fBassActive then
  begin
    ErrorQuery(PE_API_NOT_ACTIVATED);
    Exit;
  end;

  if (not IsPaused) and IsPlaying then
  begin
    fPlayerStates := fPlayerStates + [psPause];
    fTimer.Enabled := false;
    BASS_ChannelPause(fChannel);
    if Assigned(fOnPlaying) then
       fOnPlaying(Self, fMusicPos, fMusicLength, 0, 0);
    PauseQuery;
  end else
  if IsPaused and IsPlaying then
  begin
    fPlayerStates := fPlayerStates - [psPause];
    BASS_ChannelPlay(fChannel,false);
    fTimer.Enabled := true;
  end;
end;

procedure TBassPlayer.SoundMute;
begin
  if not fBassActive then
  begin
    ErrorQuery(PE_API_NOT_ACTIVATED);
    Exit;
  end;

  if IsSoundMuted then
  begin
    fPlayerStates := fPlayerStates - [psSoundMute];
    { sound mute }
    fMuteVolume := BASSAPIV22.BASS_GetVolume;
    BASS_SetVolume(0);
    SoundMuteQuery;
  end else
  begin
    fPlayerStates := fPlayerStates + [psSoundMute];
    BASS_SetVolume(fMuteVolume);
  end;
end;

{ ------ }

procedure TBassPlayer.SetVolume(const Vol : cardinal);
begin
  if IsSoundMuted then
    fMuteVolume := Vol
  else
    BASS_SetVolume(Vol);
end;

{ ------ }

procedure TBassPlayer.SetMusicPos(const TC : TTimeCode);
begin
  SetMusicPos(TimeCodeToInt(TC));
end;

procedure TBassPlayer.SetMusicPos(const TC : integer);
begin
  BASS_ChannelSetPosition(fChannel,TC);
end;

{ ------ }

procedure TBassPlayer.NextTrack;
begin
  Stop;
  TrackIndex := TrackIndex + 1;
  if TrackIndex <> -1 then Play;
end;

procedure TBassPlayer.PreviousTrack;
begin
  Stop;
  TrackIndex := TrackIndex - 1;
  if TrackIndex <> -1 then Play;
end;

procedure TBassPlayer.JumpToTrack(const Index : integer);
begin
  Stop;
  TrackIndex := Index;
  if TrackIndex <> -1 then Play;
end;

{ ------ }

function TBassPlayer.IsPlaying : boolean;
begin
  result := psPlay in fPlayerStates;
end;

function TBassPlayer.IsStopped : boolean;
begin
  result := psStop in fPlayerStates;
end;

function TBassPlayer.IsPaused : boolean;
begin
  result := psPause in fPlayerStates;
end;

function TBassPlayer.IsSoundMuted : boolean;
begin
  result := psSoundMute in fPlayerStates;
end;

function TBassPlayer.IsCurrentTrackExists : boolean;
begin
  result := FileExists(Files[TrackIndex]) and (TrackIndex <> -1);
end;

end.
 

Merci f0xi 3

Avec quelques mots c'est encore mieux Ajouter un commentaire

Codes Sources a aidé 72 internautes ce mois-ci

Commenter la réponse de f0xi

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.