Table of Contents

Working with videos

The core of the component is the channel's videos: TTMSFNCCloudGoogleYouTube loads them into a Videos collection and uploads new ones from a local file. Both operations are asynchronous and report through events. This chapter covers listing the videos and uploading a new one with progress feedback.

Listing videos

Call GetAllVideos to load the channel's videos; the result arrives in OnGetVideosComplete, after which the Videos collection is populated. Each TTMSFNCCloudGoogleYouTubeVideo exposes its metadata, such as Title, and is the entry point for that video's comments (see Reading comments).

procedure TForm1.LoadVideos;
begin
  FYouTube.GetAllVideos; // result arrives in OnGetVideosComplete
end;

procedure TForm1.YouTubeGetVideos(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  I: Integer;
  Video: TTMSFNCCloudGoogleYouTubeVideo;
begin
  if not ARequestResult.Success then
    Exit;

  ListBox1.Clear;
  for I := 0 to FYouTube.Videos.Count - 1 do
  begin
    Video := FYouTube.Videos[I];
    ListBox1.Items.AddObject(Video.Title, Video);
  end;
end;

Uploading a video

UploadVideo takes the local file name, a title, and a description, and uploads the file to the channel. When the upload finishes, OnUploadVideo fires with the newly created TTMSFNCCloudGoogleYouTubeVideo. Refresh the list there so the new video appears.

procedure TForm1.Upload(const AFileName, ATitle, ADescription: string);
begin
  FYouTube.UploadVideo(AFileName, ATitle, ADescription); // progress via OnUploadVideoProgress
end;

procedure TForm1.YouTubeUploadProgress(Sender: TObject;
  const AFileName: TTMSFNCUtilsFile; AProgress: Single;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  // AProgress runs from 0 to 100 as the file is uploaded.
  ProgressBar1.Value := AProgress;
end;

procedure TForm1.YouTubeUploadVideo(Sender: TObject;
  const AVideo: TTMSFNCCloudGoogleYouTubeVideo;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  // Fires once the upload finishes; AVideo is the newly created video.
  if ARequestResult.Success then
    FYouTube.GetAllVideos;
end;

Showing transfer progress

Uploads can take a while, so OnUploadVideoProgress fires repeatedly during the transfer with an AProgress value from 0 to 100. Bind it to a progress bar to give the user feedback, as the upload snippet above shows.

Pitfalls

  • Act on results in the completion event. The Videos collection and the uploaded video are only guaranteed once OnGetVideosComplete/OnUploadVideo fires.
  • Check ARequestResult.Success before reading the collection or the new video.
  • Uploads need the upload scope. A token without it authenticates but the upload is rejected — see Scopes and permissions.

See also