Delphi ... Skype API
2:48 AM | Author: devista
//  Tested with Delphi 5, Skype4Com v.1.0.27, Skype v.3.5.0.95
//
// To use the code in this example:
// 1. Create new application. A form named Form1 should be created auttomatically.
// 2. Drag and drop a standard Memo onto Form1.
// 3. Copy & paste the code in this exaple into Form1's code editor.
// 4. Go to Form view and link following events:
// 4.1 Form1.OnCreate -> FormCreate;
// 4.2 Form1.OnClose -> FormClose.


Unit Unit1;

Interface

Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, OleCtrls, SKYPE4COMLib_TLB, OleServer, CmAdmCtl;

Const PortNr = 37216;

Type
TForm1 = class(TForm)
Memo1: TMemo;
Procedure SkypeCallStatus(Sender: TObject; const pCall: ICall; Status: TOleEnum);
Procedure SkypeAttachmentStatus(Sender: TObject; Status: TOleEnum);
Procedure OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
Procedure OnClientConnect (Sender: TObject; Socket: TCustomWinSocket);
Procedure OnClientDisConnect (Sender: TObject; Socket: TCustomWinSocket);
Procedure FormCreate(Sender: TObject);
Procedure FormClose(Sender: TObject; var Action: TCloseAction);
Private
Server : TServerSocket;
Skype : TSkype;
End;

Var
Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.FormCreate(Sender: TObject);

Begin
// Creating and activating TCP server socket
Server := TServerSocket.Create(Self);
Server.Port := PortNr;
Server.Active := True;

// Assigning our own event handlers to Server object's event properties
Server.OnClientRead := OnClientRead;
Server.OnClientConnect := OnClientConnect;
Server.OnClientDisconnect := OnClientDisConnect;

// Creating and attaching Skype object
Skype := TSkype.Create(self);
Skype.Attach(7, false);



// Assigning our own event handlers to Skype object's properties
Skype.OnCallStatus := SkypeCallStatus;
Skype.OnAttachmentStatus := SkypeAttachmentStatus;

Memo1.Lines.Clear;
Memo1.Align := alClient;
Caption := 'Voice to port redirection demo';
End;


//------------------------------------------------------------------------------
// Fired when a client gets connected to our TCP server socket.
// In case of this example, this happens when pCall.OutputDevice property
// gets assigned a value in SkypeCallStatus procedure (incoming call detected).

Procedure TForm1.OnClientConnect (Sender: TObject; Socket: TCustomWinSocket);

Begin
Memo1.Lines.Add('Skype connected to port: ' + IntToSTr(PortNr));
End;


//------------------------------------------------------------------------------
// Fired when a client gets disconnected from our TCP server socket.

Procedure TForm1.OnClientDisConnect (Sender: TObject; Socket: TCustomWinSocket);

Begin
Memo1.Lines.Add('Skype disconnected from port: ' + IntToSTr(PortNr));
End;


//------------------------------------------------------------------------------
// Simple byte to hex conversion

Function ByteToHex (B : Byte) : String;

Const
Hex = '0123456789ABCDEF';

Var S : String[2];

Begin
S := '00';
S[1] := Hex[(B mod 16) + 1];
S[2] := Hex[(B div 16) + 1];
ByteToHex := S;
End;


//------------------------------------------------------------------------------
// OnClientRead is fired when a data packet is received.
// This procedure will read the data packet from the stream and
// hex dump it in Memo1.
//
// Note that while we declare the Buf variable with size 10 Kb, the actual
// size of the data packet can differ between systems. Usually the actual
// packet size is below 2 Kb. If your application excepts out in this procedure
// you may want to increase the buffer size to 20 Kb.
//
// We declare the Buf variable outside OnClientRead procedure to avoid
// having to allocate the buffer each time the procedure is executed.


Var Buf : Array[0..10*1024] of Byte;

Procedure TForm1.OnClientRead(Sender: TObject; Socket: TCustomWinSocket);

Var
Len, I, J : Integer;
OutStr : String;

Begin
Len := Socket.ReceiveLength;
Socket.ReceiveBuf(Buf, Len);

For I := 0 to (Len div 16) - 1 do
Begin
OutStr := '';
For J := 0 to 15 do
Begin
OutStr := OutStr + ByteToHex(Buf[(I * 16) + J]) + ' ';
End;
Memo1.Lines.Add(OutStr);
End;
End;


Procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

Begin
Server.Active := False;
End;


//------------------------------------------------------------------------------
// Fired when status of a Skype Call object changes
// Here we use it to detect when calls start and finish

Procedure TForm1.SkypeCallStatus(Sender: TObject; const pCall: ICall; Status: TOleEnum);

Begin
// If a new call has started
if (Status = clsInProgress) Then
Begin
Memo1.Lines.Add('Call in progress..');
pCall.OutputDevice[callIoDeviceTypePort] := IntToStr(PortNr);

// To redirect Input or Microphone voice instead of Output to a port
// use one of following commands:
// pCall.InputDevice[callIoDeviceTypePort] := IntToStr(PortNr);
// pCall.CaptureMicDevice[callIoDeviceTypePort] := IntToStr(PortNr);
End;

// If a call has finished
if Status = clsFinished Then
Begin
Memo1.Lines.Add('Call finished.');
End;
End;


//------------------------------------------------------------------------------
// Fired when Skype attachment status changes. Note that this handler also
// automatically attempts to reattach to the API if connection was temporarily lost

Procedure TForm1.SkypeAttachmentStatus(Sender: TObject; Status: TOleEnum);

Begin
Memo1.Lines.Add('Attatchment status: ' + Skype.Convert.AttachmentStatusToText(Status));
if Status = apiAttachAvailable Then Skype.Attach(7, false);
End;

End.
|
This entry was posted on 2:48 AM and is filed under . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

0 comments: