MacOS Video Player: changes
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; |
| 210 | 212 | ||
| 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 @@ | |||
| 263 | 268 | ||
| 264 | procedure TForm1.LoadButtonClick(Sender: TObject); | 269 | procedure TForm1.LoadButtonClick(Sender: TObject); |
| 265 | begin | 270 | begin |
| 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 URL | 280 | // 1. Create a URL |
| 268 | // | 281 | // |
| @@ -281,7 +294,7 @@ | |||
| 281 | // wait while media loads for playback | 294 | // wait while media loads for playback |
| 282 | while(myPlayerItem.Status = AVPlayerItemStatusUnknown) | 295 | while(myPlayerItem.Status = AVPlayerItemStatusUnknown) |
| 283 | do | 296 | do |
| 284 | application.processmessages; | 297 | application.processmessages; |
| 285 | 298 | ||
| 286 | {$ifdef DEBUG} | 299 | {$ifdef DEBUG} |
| 287 | // Status information for debugging | 300 | // Status information for debugging |
| @@ -289,6 +302,10 @@ | |||
| 289 | ShowMessage('AVplayer status: failed') | 302 | ShowMessage('AVplayer status: failed') |
| 290 | else if(myPlayer.Status = AVPlayerStatusReadyToPlay) then | 303 | 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} |
| 293 | 310 | ||
| 294 | // | 311 | // |
| @@ -321,6 +338,8 @@ | |||
| 321 | + 'Needs display: ' + BoolToStr(myPlayerView.needsDisplay) + LineEnding | 338 | + 'Needs display: ' + BoolToStr(myPlayerView.needsDisplay) + LineEnding |
| 322 | + 'Timescale: ' + FloatToStr(myPlayer.currentItem.duration.timescale) + LineEnding | 339 | + 'Timescale: ' + FloatToStr(myPlayer.currentItem.duration.timescale) + LineEnding |
| 323 | + 'Duration: ' + FloatToStr(myPlayer.currentItem.duration.value) + LineEnding | 340 | + '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) + LineEnding | 343 | + 'Minutes: ' + FloatToStr(myPlayer.currentItem.duration.value / myPlayer.currentItem.duration.timescale / sixtysecs) + LineEnding |
| 325 | + 'MM:SS: ' + FloatToStr(Trunc(mins)) + ':' + FloatToStr(Trunc(Round(secs))) + LineEnding | 344 | + '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 @@ | |||
| 334 | 353 | ||
| 335 | // | 354 | // |
| 336 | // Resize PlayerView when Form resized | 355 | // Resize PlayerView when Form resized |
| 337 | // | 356 | // |
| 338 | procedure TForm1.FormResize(Sender: TObject); | 357 | procedure TForm1.FormResize(Sender: TObject); |
| 339 | begin | 358 | begin |
| 340 | // Reset video frame size and bounds | 359 | // Reset video frame size and bounds |
| @@ -344,7 +363,7 @@ | |||
| 344 | 363 | ||
| 345 | // | 364 | // |
| 346 | // Quit player | 365 | // Quit player |
| 347 | // | 366 | // |
| 348 | procedure TForm1.QuitButtonClick(Sender: TObject); | 367 | procedure TForm1.QuitButtonClick(Sender: TObject); |
| 349 | begin | 368 | begin |
| 350 | Close; | 369 | Close; |
| @@ -360,6 +379,8 @@ | |||
| 360 | end. | 379 | end. |
| 361 | ``` | 380 | ``` |
| 362 | 381 | ||
| 382 | Full 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 player | 384 | ## Example code using AVPlayerLayer for a custom player |
| 364 | 385 | ||
| 365 | ```pascal | 386 | ```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; |
| 483 | 505 | ||
| 484 | AVURLAsset = objcclass external (AVAsset) | 506 | AVURLAsset = objcclass external (AVAsset) |
| @@ -546,6 +568,14 @@ | |||
| 546 | // | 568 | // |
| 547 | procedure TForm1.PlayButtonClick(Sender: TObject); | 569 | procedure TForm1.PlayButtonClick(Sender: TObject); |
| 548 | begin | 570 | begin |
| 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 URL | 580 | // 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); |
| 566 | 596 | ||
| 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 | do | 603 | 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; | ||
| 575 | 605 | ||
| 576 | loadingLabel.visible := False; // hide label now media loaded | 606 | loadingLabel.visible := False; // hide label now media loaded |
| 577 | 607 | ||
| 578 | {$ifdef DEBUG} | 608 | {$ifdef DEBUG} |
| 579 | // Status information for debugging | 609 | // Status information for debugging |
| @@ -655,9 +685,13 @@ | |||
| 655 | // release created objects | 685 | // release created objects |
| 656 | myAsset.release; | 686 | myAsset.release; |
| 657 | myPlayerItem.release; | 687 | myPlayerItem.release; |
| 658 | myPlayer.release; | 688 | myPlayer.replaceCurrentItemWithPlayerItem(Nil); |
| 689 | myPlayer.release; | ||
| 690 | |||
| 659 | end. | 691 | end. |
| 660 | ``` | 692 | ``` |
| 693 | |||
| 694 | Full source code available at [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_avplayer_layer/). | ||
| 661 | 695 | ||
| 662 | ## Detecting invalid AVAsset URLs | 696 | ## Detecting invalid AVAsset URLs |
| 663 | 697 | ||