Einzelnen Beitrag anzeigen

DSFreak

Registriert seit: 24. Apr 2008
9 Beiträge
 
#14

Re: iTunes steuern mit iTunes Type Library

  Alt 28. Jun 2009, 23:07
Da hast du aber Glück... genau so etwas habe ich heute erst für meine mIRC-DLL geschrieben
Ich habe jetzt nicht so viel Zeit, das ganze auf "normales" Format umzuschreiben, aber entferne einfach die ganzen unnötigen Parameter; das Ergebnis landet hier in data.

Ich weiss, da ist viel Copy&Paste drin, aber man kann ja nicht alles haben.

Delphi-Quellcode:
unit Query;

interface

uses
  ComObj,
  ComTool,
  SysUtils,
  Variants,
  Windows,
  iTunesLib_TLB;

implementation

function queryFile(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  ret : AnsiString;
  track : IITTrack;
  tkind : ITTrackKind;
  ftrack : IITFileOrCDTrack;
  utrack : IITURLTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          utrack := IDispatch(iTunes.CurrentTrack) as IITURLTrack;
          ret := AnsiString(utrack.URL);
        end
        else begin
          ftrack := track as IITFileOrCDTrack;
          ret := AnsiString(ftrack.Location);
        end;
        if (ret <> '') then begin
          StrCopy(data, PAnsiChar(ret));
          Exit(3);
        end
      end;
    end;
  except
  end;
  StrCopy(data, 'n/a');
  Result := 3;
end;

function queryType(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  name, uret : AnsiString;
  fret : PAnsiChar;
  track : IITTrack;
  tkind : ITTrackKind;
  ftrack : IITFileOrCDTrack;
  utrack : IITURLTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          utrack := IDispatch(iTunes.CurrentTrack) as IITURLTrack;
          name := AnsiString(utrack.URL);
          uret := Copy(name, 1, Pos(AnsiString('://'), name) - 1);
          StrCopy(data, PAnsiChar(uret));
          Exit(3);
        end
        else if (tkind = ITTrackKindFile) or (tkind = ITTrackKindCD) then begin
          ftrack := track as IITFileOrCDTrack;
          name := AnsiString(ftrack.Location);
          fret := AnsiStrRScan(PAnsiChar(name), '.') + 1;
          StrCopy(data, fret);
          Exit(3);
        end;
      end;
    end;
  except
  end;
  StrCopy(data, 'n/a');
  Result := 3;
end;

function queryTitle(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  name, interp : AnsiString;
  track : IITTrack;
  tkind : ITTrackKind;
  ftrack : IITFileOrCDTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          name := AnsiString(VarToStr(iTunes.CurrentStreamTitle));
          StrCopy(data, PAnsiChar(name));
          Exit(3);
        end
        else if (tkind = ITTrackKindFile) or (tkind = ITTrackKindCD) then begin
          ftrack := track as IITFileOrCDTrack;
          interp := AnsiString(ftrack.Artist);
          name := AnsiString(ftrack.name);
          StrCopy(data, PAnsiChar(interp));
          StrCat(data, ' - ');
          StrCat(data, PAnsiChar(name));
          Exit(3);
        end;
      end;
    end;
  except
  end;
  StrCopy(data, 'n/a');
  Result := 3;
end;

function queryBitrate(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  track : IITTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        StrCopy(data, PAnsiChar(AnsiString(IntToStr(track.BitRate))));
        Exit(3);
      end;
    end;
  except
  end;
  StrCopy(data, '-1');
  Result := 3;
end;

function queryLength(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  track : IITTrack;
  tkind : ITTrackKind;
  ftrack : IITFileOrCDTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          StrCopy(data, '-1');
          Exit(3);
        end
        else if (tkind = ITTrackKindFile) or (tkind = ITTrackKindCD) then begin
          ftrack := track as IITFileOrCDTrack;
          StrCopy(data, PAnsiChar(AnsiString(IntToStr(ftrack.Duration))));
          Exit(3);
        end;
      end;
    end;
  except
  end;
  StrCopy(data, '-2');
  Result := 3;
end;

function queryLengthF(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  track : IITTrack;
  tkind : ITTrackKind;
  ftrack : IITFileOrCDTrack;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          StrCopy(data, 'Stream');
          Exit(3);
        end
        else if (tkind = ITTrackKindFile) or (tkind = ITTrackKindCD) then begin
          ftrack := track as IITFileOrCDTrack;
          StrCopy(data, PAnsiChar(AnsiString(ftrack.Time)));
          Exit(3);
        end;
      end;
    end;
  except
  end;
  StrCopy(data, 'n/a');
  Result := 3;
end;

function queryPosition(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        StrCopy(data, PAnsiChar(AnsiString(IntToStr(iTunes.PlayerPosition))));
        Exit(3);
      end;
    end;
  except
  end;
  StrCopy(data, '-2');
  Result := 3;
end;

function queryPositionF(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing, b, m, s : integer;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        b := iTunes.PlayerPosition;
        s := b mod 60;
        m := b div 60;
        StrCopy(data, PAnsiChar(AnsiString(Format('%d:%02d', [m, s]))));
        Exit(3);
      end;
    end;
  except
  end;
  StrCopy(data, 'n/a');
  Result := 3;
end;

function queryStatus(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      StrCopy(data, PAnsiChar(AnsiString(IntToStr(iTunes.PlayerState))));
      Exit(3);
    end;
  except
  end;
  StrCopy(data, '-1');
  Result := 3;
end;

function queryStatusF(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  state : ITPlayerState;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      state := iTunes.PlayerState;
      case state of
        ITPlayerStateStopped: StrCopy(data, 'Gestoppt');
        ITPlayerStatePlaying: StrCopy(data, 'Laufend');
        ITPlayerStateFastForward: StrCopy(data, 'Vorspulen');
        ITPlayerStateRewind: StrCopy(data, 'Zurückspulen');
        else StrCopy(data, 'Fehler');
      end;
      Exit(3);
    end;
  except
  end;
  StrCopy(data, '-1');
  Result := 3;
end;

function queryNum(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  playlist : IITPlaylist;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        playlist := IDispatch(iTunes.CurrentPlaylist) as IITPlaylist;
        StrCopy(data, PAnsiChar(AnsiString(IntToStr(playlist.Tracks.Count))));
        Exit(3);
      end;
    end;
  except
  end;
  StrCopy(data, '-2');
  Result := 3;
end;

function queryInfo(mWnd, aWnd: HWND; data, parms: PAnsiChar; show, nopause:boolean) : integer; stdcall;
var
  iTunes : OleVariant;
  playing : integer;
  track : IITTrack;
  tkind : ITTrackKind;
begin
  try
    iTunes := CreateOleObject('iTunes.Application');
    if not VarIsNull(iTunes) then begin
      playing := iTunes.PlayerState;
      if playing > 0 then begin
        track := IDispatch(iTunes.CurrentTrack) as IITTrack;
        tkind := track.Kind;
        if tkind = ITTrackKindURL then begin
          StrCopy(data, '2');
          Exit(3);
        end
        else if (tkind = ITTrackKindFile) or (tkind = ITTrackKindCD) then begin
          StrCopy(data, '1');
          Exit(3);
        end
        else begin
          StrCopy(data, '3');
          Exit(3);
        end;
      end;
    end
    else begin
      StrCopy(data, '0');
      Exit(3);
    end;
  except
  end;
  StrCopy(data, '-1');
  Result := 3;
end;

exports
  queryFile,
  queryType,
  queryTitle,
  queryBitrate,
  queryLength,
  queryLengthF,
  queryPosition,
  queryPositionF,
  queryStatus,
  queryStatusF,
  queryNum,
  queryInfo;

end.
  • queryFile: Dateinamen inkl. Pfad bzw. Stream-URL
  • queryType: Dateiendung bzw. tream-Protokoll
  • queryTitle: aktuell gespielter Titel inkl. Interpret im Format "Interpret - Titel"
  • queryBitrate: Bitrate des aktuell gespielten Titel in kbit/s
  • queryLength: Länge des aktuell gespielten Titels in Sekunden
  • queryLengthF: Länge des aktuell gespielten Titels im Format mm:ss
  • queryPosition(F): das selbe nochmal für den Anteil des Songs, der schon gespielt wurde
  • queryStatus(F):
    Zitat:
    Enumeration values:
    ITPlayerStateStopped (0) Player is stopped.
    ITPlayerStatePlaying (1) Player is playing.
    ITPlayerStateFastForward (2) Player is fast forwarding.
    ITPlayerStateRewind (3) Player is rewinding.
  • queryNum: Anzahl der Titel in der aktuell gespielten Playlist
  • queryInfo: -1 = Fehler, 0 = iTunes nicht gefunden, 1 = Datei läuft, 2 = Stream läuft, 3 = etwas Unbekanntes läuft

//EDIT: falls, nötig, hab ich auch noch Funktionen für Play, Pause etc...
  Mit Zitat antworten Zitat