-2

Sorry I made title too long as I'm trying to accomplish a lot of things in an attempt to play audio file from an url using AVPlayer in a modal view controller. I've used Apple's foundation example - AVFoundationSimplePlayer, tweaked it and added few things. Player works fine but I've few challenges to solve as described below:

Here is the complete source code Source Code of my project

1) Refer to screenshot below, I want the background of modal vc to be transparent so that I can see the view which launched this modal view. Refer to screenshot below

2) There is delay after loading the modal vc and before the popup view appears with controls highlighted. I'm not sure if this is normal to have delay? If it's normal and we can't do anything about it then I want to add activity control during this loading period. I'm not sure when to stop this activity control.

3) There are errors shown before player loads the control. Not sure what they are?

2018-09-05 13:32:56.519014+0100 AVFoundationSimplePlayer-Swift[44042:2403222] Task .<2> finished with error - code: -999 2018-09-05 13:32:57.607560+0100 AVFoundationSimplePlayer-Swift[44042:2403221] Task .<3> finished with error - code: -999

teja_D
  • 383
  • 3
  • 18
Matt
  • 315
  • 2
  • 6
  • 20

1 Answers1

1

I just checked your attached sample code. You are using play function before assigning any AVPlayerItem to your player. You're loading your asset asynchronously and before downloading the asset player can not play. So what you should do

asset = AVURLAsset(url: movieURL, options: nil)

/// Just after making an asset and remove the code written in asset's setter
let item = AVPlayerItem(asset: asset!)
player = AVPlayer(playerItem: item)

/// This extra line is to playing the live url. It will play what it downloads in chunks.
if #available(iOS 10.0, *) {
    player.automaticallyWaitsToMinimizeStalling = false
} else {
    // Fallback on earlier versions
}

/// Now your player is ready to play
player.play()

EDIT 1
If you're still struggling with above piece of code so I have made changes in PlayerViewController.m file to fix your issues. Here is the gist link of that file. Search /// TheTiger Change to find what change I made. Now I'm able to play audio file with proper time and slider value.

EDIT 2
I thought you will be able to see the difference but no problem I will explain in more detail.

#3. Regarding your error message this is just a Xcode debug message and you can disable it. See this answer for more detail.

#2. There is a delay in presenting the modal because you have all the code in your viewWillAppear: method just move that code in videDidAppear: and let the view appear first before doing anything. It will remove the delay.

#1. Background of modal vc to be transparent. So it is possible to make it transparent See this answer.

This Sample Code just works fine.

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • 1
    Thanks for looking into code. Sorry I'm not expert coder, I'm still learning. I'm overwhelmed with the Apple's sample code itself a bit so not sure which piece of code goes where. If you have it in your file which you modified then can you copy n paste it here? Tx – Matt Sep 05 '18 at 13:48
  • Actually I have left for the day and online via mobile app.... But for Now 'asset = AVURLAsset(url: movieURL, options: nil)' search this line in your project and add the code here. First try simple player as you don't need that much code for this purpose. You already have created the asset.. make an playerItem and pass it to AVPlayer and play. – TheTiger Sep 05 '18 at 14:08
  • Yes I did try, didn't work. It didn't load. May be I'm doing something wrong. Another thing is which one of my problem your solution will solve? Pl. refer to 3 issues I mentioned above. – Matt Sep 05 '18 at 14:25
  • 3rd problem which has error and popup delay too. And try to make it separate question because it is too broad for anyone to answer all three at once. – TheTiger Sep 05 '18 at 16:25
  • Thanks for taking time and effort to look into the code. I copied and pasted your entire code but I'm still getting delay in loading and playing and errors are still there. – Matt Sep 06 '18 at 16:22
  • Audio is not playing? And the time and slider not working? It was working :| – TheTiger Sep 06 '18 at 18:24
  • Everything works as it did in my original code. You said your code should have solved delay in loading audio and errors shown which it didn't. There is still delay in loading the audio and the errors are still showing. – Matt Sep 06 '18 at 18:52
  • @Matt No this is not same as your original code. When you run your code it will take huge time to play the audio compare to mine as your code downloads the whole asset first then starts playing. Your activity just doesn't hide as it should be. I have updated my answer in detail for all three questions. See **EDIT 2** part. – TheTiger Sep 07 '18 at 06:02
  • Thank you very much for taking time and fixing code for me. It works now and all three issues have been resolved. You are star! – Matt Sep 08 '18 at 15:52