Wild media server (UPnP, DLNA, HTTP)
http://wildmediaserver.com/forum/

Example of use of the class TWmsScriptUPnpDeviceList
http://wildmediaserver.com/forum/viewtopic.php?f=19&t=1917
Page 1 of 1

Author:  Eugene [ Tue Jan 22, 2013 1:33 pm ]
Post subject:  Example of use of the class TWmsScriptUPnpDeviceList

After running the script, shows a form containing a list of available DMR-devices (similar to the list of playback devices in the main form of program).

By clicking the "Play to" you can start a playback current media resource in the main form of the program to the selected device. By timer updates the current status of the selected device and playback position.

Web-interface specialists can use this functionality to implement the "Play to" in the script of Web-navigation.

Description of UPnP-services - http://upnp.org/resources/upnpresources.zip

This example uses the service AVTransport, description in "\standardizeddcps\MediaServer_1 and MediaRenderer_1\UPnP-av-AVTransport-v1-Service-20020625.pdf"

Code:
const
  ciFormWidth  = 370;
  ciFormHeight = 320;

  UPNP_DEVICE_MEDIA_SERVER   = 2;
  UPNP_DEVICE_MEDIA_RENDERER = 3;

  udtServiceAvTransport = 7;
 
var
  UPnpDeviceListBox: TListBox;
  UPnPDeviceListForm: TForm;
  UPnPDeviceList: TWmsScriptUPnpDeviceList;
  UPnpDeviceStatusLabel, UPnpDevicePositionLabel: TLabel;
 
function CreateButton(aParent: TWinControl; const aCaption: string; aLeft, aTop, aWidth, aHeight: Integer; aModalResult: Integer = mrNone): TButton;
begin
  Result := TButton.Create(UPnPDeviceListForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.ModalResult := aModalResult;
  Result.Parent := aParent
end;

function CreateLabel(aParent: TWinControl; const aCaption: string; aLeft, aTop: Integer): TLabel;
begin
  Result := TLabel.Create(UPnPDeviceListForm);
  Result.Left := aLeft;
  Result.Top  := aTop;
  Result.Caption := aCaption;
  Result.Parent := aParent
end;

function CreateGroupBox(aParent: TWinControl; const aCaption: string; aLeft, aTop, aWidth, aHeight: Integer): TGroupBox;
begin
  Result := TGroupBox.Create(UPnPDeviceListForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.Parent := aParent
end;

function CreateListBox(aParent: TWinControl; aLeft, aTop, aWidth, aHeight: Integer): TListBox;
begin
  Result := TListBox.Create(UPnPDeviceListForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Parent := aParent
end;

procedure DoPlayToDevice(Sender: TObject);
begin
  if (WmsCurrentMediaListItem <> nil) and (UPnpDeviceListBox.ItemIndex <> - 1) then
    WmsPlayToDevice(WmsCurrentMediaListItem, UPnPDeviceList[UPnpDeviceListBox.ItemIndex].Properties[udpIpAddress])
end;

procedure DoPause(Sender: TObject);
var
  UPnPService: TWmsScriptUPnPService;
begin
  if UPnpDeviceListBox.ItemIndex <> - 1 then begin
    UPnPService := UPnPDeviceList[UPnpDeviceListBox.ItemIndex].FindService(udtServiceAvTransport);
    if UPnPService <> nil then
      UPnPService.InvokeUPnPAction('Pause', ['InstanceID', '0'])
  end
end;

procedure DoPlay(Sender: TObject);
var
  ResponseItem: TXmlItem;
  UPnPService: TWmsScriptUPnPService;
begin
  if UPnpDeviceListBox.ItemIndex <> - 1 then begin
    UPnPService := UPnPDeviceList[UPnpDeviceListBox.ItemIndex].FindService(udtServiceAvTransport);
    if UPnPService <> nil then begin
      if UPnPService.InvokeUPnPActionWithResponseItem('Play', UPnPService.BuildActionContent('Play', ['InstanceID', '0', 'Speed', '1']), ResponseItem) <> 0 then
        if ResponseItem <> nil then
          ShowMessage(ResponseItem.SaveToString);
      UPnPService.FreeResponseItem(ResponseItem)
    end 
  end
end;

procedure DoDeviceStatus(Sender: TObject);
var
  UPnPService: TWmsScriptUPnPService;
  vResultValues: Variant;
begin
  if UPnpDeviceListBox.ItemIndex <> - 1 then begin
    UPnPService := UPnPDeviceList[UPnpDeviceListBox.ItemIndex].FindService(udtServiceAvTransport);
    if UPnPService <> nil then begin
      if UPnPService.InvokeUPnPActionWithResultValues('GetTransportInfo', ['InstanceID', '0'], ['CurrentTransportState'], vResultValues) = 0 then begin
        UPnpDeviceStatusLabel.Caption := vResultValues[0];
        if UPnpDeviceStatusLabel.Caption = 'PLAYING' then begin
          if UPnPService.InvokeUPnPActionWithResultValues('GetPositionInfo', ['InstanceID', '0'], ['RelTime'], vResultValues) = 0 then
            UPnpDevicePositionLabel.Caption := vResultValues[0]
          else
            UPnpDevicePositionLabel.Caption := ''
        end else
          UPnpDevicePositionLabel.Caption := ''
      end 
    end 
  end
end;

var
  i: Integer;
  UPnpDeviceListGroupBox: TGroupBox;
  UPnPDeviceStatusTimer: TTimer;         
begin
  UPnPDeviceListForm := TForm.Create(Application);
  try
    UPnPDeviceListForm.BorderStyle := bsDialog;
    UPnPDeviceListForm.ClientWidth := ciFormWidth;
    UPnPDeviceListForm.ClientHeight := ciFormHeight;
    UPnPDeviceListForm.Caption := 'Example of use of the class TWmsScriptUPnpDeviceList';
    UPnPDeviceListForm.Position := poScreenCenter;
    UPnpDeviceListGroupBox := CreateGroupBox(UPnPDeviceListForm, '', 10, 10, ciFormWidth - 20, ciFormHeight - 55);
    UPnpDeviceListBox := CreateListBox(UPnpDeviceListGroupBox, 10, 10, ciFormWidth - 50, UPnpDeviceListGroupBox.Height - 80);
    CreateButton(UPnPDeviceListForm, 'OK', ciFormWidth - 170, ciFormHeight - 35, 75, 25, mrOK);
    CreateButton(UPnPDeviceListForm, 'Cancel', ciFormWidth - 85, ciFormHeight - 35, 75, 25, mrCancel);
    CreateButton(UPnpDeviceListGroupBox, 'Play to', 10, UPnpDeviceListGroupBox.Height - 35, 100, 25).OnClick := @DoPlayToDevice;
    CreateButton(UPnpDeviceListGroupBox, 'Pause', 120, UPnpDeviceListGroupBox.Height - 35, 100, 25).OnClick := @DoPause;
    CreateButton(UPnpDeviceListGroupBox, 'Continue', 230, UPnpDeviceListGroupBox.Height - 35, 100, 25).OnClick := @DoPlay;
    CreateLabel(UPnpDeviceListGroupBox, 'Status: ', 15, UPnpDeviceListGroupBox.Height - 60);
    UPnpDeviceStatusLabel   := CreateLabel(UPnpDeviceListGroupBox, '', 85, UPnpDeviceListGroupBox.Height - 60);
    CreateLabel(UPnpDeviceListGroupBox, 'Position: ', 200, UPnpDeviceListGroupBox.Height - 60);
    UPnpDevicePositionLabel := CreateLabel(UPnpDeviceListGroupBox, '', 255, UPnpDeviceListGroupBox.Height - 60);
    UPnPDeviceList := TWmsScriptUPnPDeviceList.Create(UPNP_DEVICE_MEDIA_RENDERER);
    try
      for i := 0 to UPnPDeviceList.Count - 1 do
        UPnpDeviceListBox.Items.Add(UPnPDeviceList[i].Properties[udpDisplayName]);
      UPnPDeviceStatusTimer := TTimer.Create(UPnPDeviceListForm);
      UPnPDeviceStatusTimer.OnTimer := @DoDeviceStatus;
      UPnPDeviceStatusTimer.Enabled := True;
      UPnPDeviceListForm.ShowModal;
      UPnPDeviceStatusTimer.Enabled := False;   
    finally
      UPnPDeviceList.Free
    end
  finally
    UPnPDeviceListForm.Free
  end
end.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/