Recent

Author Topic: [SOLVED] AVPlayer - have sound, but no video  (Read 2162 times)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1799
  • Former Delphi 1-7, 10.2 user
[SOLVED] AVPlayer - have sound, but no video
« on: August 08, 2020, 11:31:50 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PlayButtonClick(Sender: TObject);
  2. begin
  3.   //
  4.   // 1. Create a URL
  5.   //
  6.   URL := NSURL.URLWithString(NSSTR(PAnsiChar('https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8')));
  7.   if(Url = Nil) then
  8.     ShowMessage('NSURL.URLWithString failed!');
  9.  
  10.   //
  11.   // 2. Create AVPlayer object
  12.   //
  13.   myAsset := AVAsset.alloc.assetWithURL(URL);
  14.   myPlayerItem := AVPlayerItem.alloc.initWithAsset(myAsset);
  15.   myPlayer := AVPlayer.alloc.initWithPlayerItem(myPlayerItem);
  16.  
  17.   while(myPlayer.Status = AVPlayerStatusUnknown)
  18.     do
  19.       application.processmessages;
  20.  
  21.   if(myPlayer.Status = AVPlayerStatusFailed) then
  22.     ShowMessage('AVplayer status: failed')
  23.   else if(myPlayer.Status = AVPlayerStatusReadyToPlay) then
  24.     ShowMessage('AVplayer status: ready to play');
  25.  
  26.   //
  27.   // 3. Creete playerView object to display video
  28.   //
  29.   myPlayerView.alloc.setPlayer(myPlayer);
  30.   myPlayerView.setControlsStyle(1);  // inline
  31.  
  32.   if(myPlayerView.isReadyForDisplay) then
  33.     ShowMessage('AVPlayerView ready for display')
  34.   else
  35.     ShowMessage('AVPlayerView not ready for display');
  36.  
  37.   //
  38.   // 4. Play
  39.   //
  40.   myPlayer.play;
  41. end;  
  42.  

I thought this ws going to be simple but I was only half right - I have sound, but no video.

* AVplayer status: ready to play - yes
* AVPlayerView ready for display - no (delay before makes no difference)

Obviously there's something wrong with my AVPlayerView setup. Any ideas?
« Last Edit: November 03, 2021, 12:58:55 pm by trev »
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1799
  • Former Delphi 1-7, 10.2 user
Re: AVPlayer - have sound, but no video
« Reply #1 on: August 17, 2020, 09:58:20 am »
Slightly further forward. I finally noticed that:

Code: Pascal  [Select][+][-]
  1. myPlayerView.alloc.setPlayer(myPlayer);

need to be changed to:

Code: Pascal  [Select][+][-]
  1.  
  2. myPlayerView := AVPlayerView.alloc.initWithFrame(CGRectMake(0,0,500,500));
  3. myPlayerView.setPlayer(myPlayer);

to actually create myPlayerView ... but still no video :(
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1799
  • Former Delphi 1-7, 10.2 user
Re: AVPlayer - have sound, but no video
« Reply #2 on: November 03, 2021, 12:57:12 pm »
It took a while, but having slept on it for 120 days, I realised what I was missing :-)

See the Wiki article macOs Video Player for code examples using AVplayerView (standard video player controls) and the alternative AVPLayerLayer (custom video player controls).
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

 

TinyPortal © 2005-2018