MacOS Video Player: changes

Revision 144192
Trev,
/* Overview */ Fix typo
Older edit
Revision 144198 (current)
Trev,
/* Example code using AVPlayerLayer for a custom player */ Add SourceForge URL
@@ -206,6 +206,8 @@
206 function currentItem: AVPlayerItem; message 'currentItem';206 function currentItem: AVPlayerItem; message 'currentItem';
207 function status: AVPlayerStatus; message 'status';207 function status: AVPlayerStatus; message 'status';
208 function error: NSError; message 'error';208 function error: NSError; message 'error';
209 procedure setVolume(newValue: Single); message 'setVolume:';
210 procedure replaceCurrentItemWithPlayerItem(newItem: AVPlayerItem); message 'replaceCurrentItemWithPlayerItem:';
209 end;211 end;
210212
211 AVPlayerPlaybackControl = objccategory external (AVPlayer)213 AVPlayerPlaybackControl = objccategory external (AVPlayer)
@@ -234,6 +236,9 @@
234 AVPlayerStatusUnknown = 0;236 AVPlayerStatusUnknown = 0;
235 AVPlayerStatusReadyToPlay = 1;237 AVPlayerStatusReadyToPlay = 1;
236 AVPlayerStatusFailed = 2;238 AVPlayerStatusFailed = 2;
239 AVPlayerItemStatusUnknown = 0;
240 AVPlayerItemStatusReadyToPlay = 1;
241 AVPlayerItemStatusFailed = 2;
237 AVPlayerViewControlsStyleNone = 0;242 AVPlayerViewControlsStyleNone = 0;
238 AVPlayerViewControlsStyleInline = 1;243 AVPlayerViewControlsStyleInline = 1;
239 AVPlayerViewControlsStyleFloating = 2;244 AVPlayerViewControlsStyleFloating = 2;
@@ -263,6 +268,14 @@
263268
264procedure TForm1.LoadButtonClick(Sender: TObject);269procedure TForm1.LoadButtonClick(Sender: TObject);
265begin270begin
271 // Prevent multiple instances of myPlayer
272 if(myPlayer <> Nil) then
273 begin
274 myPlayer.pause;
275 myPlayer.replaceCurrentItemWithPlayerItem(Nil);
276 myPlayer := Nil;
277 end;
278
266 //279 //
267 // 1. Create a URL280 // 1. Create a URL
268 //281 //
@@ -281,7 +294,7 @@
281 // wait while media loads for playback294 // wait while media loads for playback
282 while(myPlayerItem.Status = AVPlayerItemStatusUnknown)295 while(myPlayerItem.Status = AVPlayerItemStatusUnknown)
283 do296 do
284 application.processmessages; 297 application.processmessages;
285298
286 {$ifdef DEBUG}299 {$ifdef DEBUG}
287 // Status information for debugging300 // Status information for debugging
@@ -289,6 +302,10 @@
289 ShowMessage('AVplayer status: failed')302 ShowMessage('AVplayer status: failed')
290 else if(myPlayer.Status = AVPlayerStatusReadyToPlay) then303 else if(myPlayer.Status = AVPlayerStatusReadyToPlay) then
291 ShowMessage('AVplayer status: ready to play');304 ShowMessage('AVplayer status: ready to play');
305 if(myPlayerItem.Status = AVPlayerItemStatusFailed) then
306 ShowMessage('AVplayerItem status: failed' + '-' + NSStringToString(myPlayerItem.error.description))
307 else if(myPlayerItem.Status = AVPlayerItemStatusReadyToPlay) then
308 ShowMessage('AVplayerItem status: ready to play');
292 {$endif}309 {$endif}
293310
294 //311 //
@@ -321,6 +338,8 @@
321 + 'Needs display: ' + BoolToStr(myPlayerView.needsDisplay) + LineEnding338 + 'Needs display: ' + BoolToStr(myPlayerView.needsDisplay) + LineEnding
322 + 'Timescale: ' + FloatToStr(myPlayer.currentItem.duration.timescale) + LineEnding339 + 'Timescale: ' + FloatToStr(myPlayer.currentItem.duration.timescale) + LineEnding
323 + 'Duration: ' + FloatToStr(myPlayer.currentItem.duration.value) + LineEnding340 + 'Duration: ' + FloatToStr(myPlayer.currentItem.duration.value) + LineEnding
341 + 'Flags: ' + FloatToStr(myPlayer.currentItem.duration.flags) + LineEnding
342 + 'Epoch: ' + FloatToStr(myPlayer.currentItem.duration.epoch) + LineEnding
324 + 'Minutes: ' + FloatToStr(myPlayer.currentItem.duration.value / myPlayer.currentItem.duration.timescale / sixtysecs) + LineEnding343 + 'Minutes: ' + FloatToStr(myPlayer.currentItem.duration.value / myPlayer.currentItem.duration.timescale / sixtysecs) + LineEnding
325 + 'MM:SS: ' + FloatToStr(Trunc(mins)) + ':' + FloatToStr(Trunc(Round(secs))) + LineEnding344 + 'MM:SS: ' + FloatToStr(Trunc(mins)) + ':' + FloatToStr(Trunc(Round(secs))) + LineEnding
326 + 'Control style: ' + IntToStr(myPlayerView.controlsStyle));345 + 'Control style: ' + IntToStr(myPlayerView.controlsStyle));
@@ -334,7 +353,7 @@
334353
335//354//
336// Resize PlayerView when Form resized355// Resize PlayerView when Form resized
337// 356//
338procedure TForm1.FormResize(Sender: TObject);357procedure TForm1.FormResize(Sender: TObject);
339begin358begin
340 // Reset video frame size and bounds359 // Reset video frame size and bounds
@@ -344,7 +363,7 @@
344363
345//364//
346// Quit player365// Quit player
347// 366//
348procedure TForm1.QuitButtonClick(Sender: TObject);367procedure TForm1.QuitButtonClick(Sender: TObject);
349begin368begin
350 Close;369 Close;
@@ -360,6 +379,8 @@
360end.379end.
361```380```
362381
382Full source code is available from [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_avplayer_view/).
383
363## Example code using AVPlayerLayer for a custom player384## Example code using AVPlayerLayer for a custom player
364385
365```pascal386```pascal
@@ -479,6 +500,7 @@
479 function status: AVPlayerStatus; message 'status';500 function status: AVPlayerStatus; message 'status';
480 function error: NSError; message 'error';501 function error: NSError; message 'error';
481 procedure setVolume(newValue: Single); message 'setVolume:';502 procedure setVolume(newValue: Single); message 'setVolume:';
503 procedure replaceCurrentItemWithPlayerItem(newItem: AVPlayerItem); message 'replaceCurrentItemWithPlayerItem:';
482 end;504 end;
483505
484 AVURLAsset = objcclass external (AVAsset)506 AVURLAsset = objcclass external (AVAsset)
@@ -546,6 +568,14 @@
546//568//
547procedure TForm1.PlayButtonClick(Sender: TObject);569procedure TForm1.PlayButtonClick(Sender: TObject);
548begin570begin
571 // Prevent multiple instances of myPlayer
572 if(myPlayer <> Nil) then
573 begin
574 myPlayer.pause;
575 myPlayer.replaceCurrentItemWithPlayerItem(Nil);
576 myPlayer := Nil;
577 end;
578
549 //579 //
550 // 1. Create a URL580 // 1. Create a URL
551 //581 //
@@ -564,16 +594,16 @@
564 myPlayerItem := AVPlayerItem.alloc.initWithAsset(myAsset);594 myPlayerItem := AVPlayerItem.alloc.initWithAsset(myAsset);
565 myPlayer := AVPlayer.alloc.initWithPlayerItem(myPlayerItem);595 myPlayer := AVPlayer.alloc.initWithPlayerItem(myPlayerItem);
566596
597 // Display media loading message
598 loadingLabel.caption := 'Loading ...'; // or set in the Object Inspector
599 loadingLabel.visible := True; // set to false in the Object Inspector
600
567 // Wait until status is known (while loading AVAsset media)601 // Wait until status is known (while loading AVAsset media)
568 while(myPlayer.Status = AVPlayerStatusUnknown)602 while(myPlayer.Status = AVPlayerStatusUnknown)
569 do603 do
570 begin
571 loadingLabel.caption := 'Loading ...'; // or set in the Object Inspector
572 loadingLabel.visible := True; // set to false in the Object Inspector
573 application.processmessages;604 application.processmessages;
574 end;
575605
576 loadingLabel.visible := False; // hide label now media loaded606 loadingLabel.visible := False; // hide label now media loaded
577607
578 {$ifdef DEBUG}608 {$ifdef DEBUG}
579 // Status information for debugging609 // Status information for debugging
@@ -655,9 +685,13 @@
655// release created objects 685// release created objects
656myAsset.release;686myAsset.release;
657myPlayerItem.release;687myPlayerItem.release;
658myPlayer.release; 688myPlayer.replaceCurrentItemWithPlayerItem(Nil);
689myPlayer.release;
690
659end.691end.
660```692```
693
694Full source code available at [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_avplayer_layer/).
661695
662## Detecting invalid AVAsset URLs696## Detecting invalid AVAsset URLs
663697