Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer Duration is NAN with Twilio

The duration property of AVPlayer.currentItem is returning NAN always with the Twilio audio urls. However, the audio is playing fine.

I'm able to get the duration property of all other audio urls than Twilio.

Sample url to test this issue:

http://api.twilio.com/2010-04-01/Accounts/AC1db6911efe574fc890ee332f140f7e8c/Recordings/RE06adfbfd2ad2cfd5d95585ff91cb3b88.mp3

Here are the different ways that I have tried out:

if (avPlayer.currentItem.status.rawValue == AVPlayerStatus.ReadyToPlay.rawValue) {

     var asset = AVURLAsset(URL: sourceURL, options: nil)
     var duration: Float64 = CMTimeGetSeconds(asset.duration)
     println(duration)
}


if (avPlayer.currentItem.status.rawValue == AVPlayerStatus.ReadyToPlay.rawValue) {
     var duration: Float64 = CMTimeGetSeconds(self.avPlayer.currentItem.duration)
     println(duration)
}


var thePlayerItem = self.avPlayer.currentItem
if thePlayerItem.status.rawValue == AVPlayerStatus.ReadyToPlay.rawValue{

     println(CMTimeGetSeconds(thePlayerItem.duration))
     println(CMTimeGetSeconds(thePlayerItem.asset.duration))
}

Any help on this is appreciated.

I have requested GET through the browser and it returned all the details including the audio duration <Duration>37</Duration>

GET /2010-04-01/Accounts/ACda6f1.../Recordings/RE557ce644e5ab84fa21cc21112e22c485.xml

Response:

HEADERS

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: ETag
Connection: keep-alive
Content-Length: 550
Content-Type: application/xml
Date: Tue, 08 Sep 2015 06:50:43 GMT
Etag: b1512f..
Last-Modified: Fri, 04 Sep 2015 04:19:20 +0000
Strict-Transport-Security: max-age=15768000
X-Powered-By: AT-5000
X-Shenanigans: none
BODY
view raw
<?xml version='1.0' encoding='UTF-8'?>
<TwilioResponse>
  <Recording>
    <Sid>RE…</Sid>
    <AccountSid>ACda6f1... </AccountSid>
    <CallSid>CA3..</CallSid>
    <Duration>37</Duration>
    <DateCreated>Fri, 04 Sep 2015 04:19:20 +0000</DateCreated>
    <ApiVersion>2010-04-01</ApiVersion>
    <DateUpdated>Fri, 04 Sep 2015 04:19:20 +0000</DateUpdated>
    <Price/>
    <Uri>/2010-04-01/Accounts/ACda6f1.../Recordings/RE557ce644e5ab84fa21cc21112e22c485.xml</Uri>
  </Recording>
</TwilioResponse>
like image 495
Shamsudheen TK Avatar asked Sep 08 '15 07:09

Shamsudheen TK


3 Answers

I had the same problem but I was able to get the duration by doing the following:

let timeRange = self.avPlayer.currentItem.loadedTimeRanges[0].CMTimeRangeValue
let duration = CMTimeGetSeconds(timeRange.duration)

Hope this helps!

like image 181
inga Avatar answered Nov 13 '22 21:11

inga


Hey I am just doing it and in fact recording from twilio with mp3 extension didn't wokred for me when I have used .duration, and when using suggested @inga loadedTimeRanges[0].rangeValue.duration it returns wrong time. But I changed to twilio path without .mp3 extension and it start working

if let playerItem = player.currentItem, playerItem.status == AVPlayerItemStatus.readyToPlay {
    let durationTime = playerItem.duration 
   //playerItem.loadedTimeRanges[0].timeRangeValue.duration
}
like image 1
Michał Ziobro Avatar answered Nov 13 '22 21:11

Michał Ziobro


I finally found out the reason for this. Actually the song i was trying to get the duration from was not having the metadata set with it that's why it was giving Nan duration. AVPlayerItem asynchronously loads the data with load-asynchronous with keys method, when data is there you will get it in the current Item property of avplayer.

By the way using loadedTimeRanges[0].rangeValue.duration is not the correct way to calculate the total duration becuase it this value gets updated every second until the whole data is not loaded. For Shorter data it sometimes works fine as it loads whole data at one shot and when you use loadedtimeinranges.rangeValue.duration property it gives you correct Results but for larger data this is not the right solution.

If you want to see if your problem is related to the metadata, just stream any online mp3 file and check if the problem is still there, it should work fine for streaming any downloaded file or any remote file if it is having the meta data set with it and should give you correct duration. Hope This helps!!

like image 1
Ankur JAIN Avatar answered Nov 13 '22 21:11

Ankur JAIN