Skip to content

Instantly share code, notes, and snippets.

@JensMertelmeyer
Created July 2, 2020 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensMertelmeyer/d62f9399f07ed4ceef6684f30c30af21 to your computer and use it in GitHub Desktop.
Save JensMertelmeyer/d62f9399f07ed4ceef6684f30c30af21 to your computer and use it in GitHub Desktop.
Delphi VCL MP4 Playback
type
TVideoParams = record
filePath: String;
isAutoPlay: Boolean;
end;
procedure TForm1.playVideo(const params: TVideoParams);
var
autoplayAttribute: String;
htmlContent: String;
begin
if(params.isAutoPlay) then
autoplayAttribute := 'autoplay'
else
autoplayAttribute := String.Empty;
htmlContent :=
TFile.ReadAllText('html.html')
.Replace('$PATH_TO_VIDEO$', params.filePath)
.Replace('$AUTOPLAY$', autoplayAttribute);
WebBrowser1.runHtml(htmlContent);
end;
unit Helpers.TWebBrowser;
interface uses SHDocVw;
type
TWebBrowserHelper = class helper for TWebBrowser
/// <remarks>
/// Quelle: https://github.com/uparlayan/HTML5
/// </remarks>
procedure runHtml(const html: String);
/// <remarks>
/// Quelle: http://delphidabbler.com/articles?article=21
/// </remarks>
procedure runJavascript(const jsCode: string);
end;
implementation uses
System.Classes,
Winapi.ActiveX,
MSHTML;
procedure TWebBrowserHelper.runHtml(const html: String);
var
SS: TStringStream;
// HTML KodlarımızıTWebBrowser'a aktarmak için kullanacağız
begin
if Assigned(Document) then
begin // TWebBrowser boş sayfamızı işlemeye hazır ise yükleme işlemine başlayabiliriz...
SS := TStringStream.Create();
try
SS.WriteString(html);
// HTML kodlarımızı StringStream'ımıza yüklüyoruz.
SS.Seek(0, 0);
// StringStream'in ilk baytına pozisyonumuzu alıyoruz.
(Document as IPersistStreamInit).Load(TStreamAdapter.Create(SS));
// TWebBrowser'in Document nesnesine IPersistStreamInit arabirimi vasıtasıyla HTML içeriğimizi yüklüyoruz.
finally
SS.Destroy(); // Bitti, hepsi bu kadar. Artık HTML kodlarınının sonucunu bir web sayfası olarak görebiliriz...
end;
end;
end;
procedure TWebBrowserHelper.runJavascript(const jsCode: string);
var
Doc: IHTMLDocument2; // current HTML document
HTMLWindow: IHTMLWindow2; // parent window of current HTML document
begin
Doc := (Document as IHTMLDocument2);
if not Assigned(Doc) then
Exit;
// Get parent window of current document
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
HTMLWindow.execScript(jsCode, 'JavaScript');
HTMLWindow.blur();
end;
end.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style type="text/css">
html, body, p, video
{
height: 100%;
padding: 0px;
margin: 0px;
}
.fullwidth-video {
object-fit: contain;
height: 100%;
width: 100%
}
</style>
</head>
<body>
<video
$AUTOPLAY$
controls
src="$PATH_TO_VIDEO$"
class="fullwidth-video"
/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment