AVPlayer vs. AVAudioPlayer
Solution 1
AVPlayer can play from AVPlayerItem using AVURLAsset with an iPod library url. The AVAudioPlayer cannot play from an iPod library url.
AVPlayer has no volume property and requires the use of the system volume setting which can be controlled only by the hardware switch or an MPVolumeView. But you can set the mix volume of AVAudioPlayer.
AVPlayer seems to report an incorrect currentTime after seeking. But AVAudioPlayer reports accurately.
Solution 2
7 years after...
From a point of view with dependence on Swift and CocoaPods, so my answer is comparing for iOS 8+ only.
1. iPod library support
2. volume control
identical support:
- You can set the mix volume of
AVAudioPlayer
directly. - You can set the mix volume of
AVPlayer
with anAVAudioMix
on theAVPlayerItem
3. seeking control
both AVPlayer and AVAudioPlayer seem to report an incorrect currentTime after seeking:
- for AVAudioPlayer, it is suggested to
stop()
theAVAudioPlayer
before seeking - for AVPlayer, it is suggested to pass the option
AVURLAssetPreferPreciseDurationAndTimingKey
when initializing AVURLAssets. And rely on values given by observer block.
4. changing source
- you need only one
AVPlayer
to play multiple files - you need multiple
AVAudioPlayer
to play multiple files
Solution 3
The AVPlayer actually has a similar property as the playing property of AVAudioPlayer.
Take a look at the rate
property.
jarry jafery
I program computers, phones, tablets, web servers, robots, anything I can really. I love creating and I love changing people’s lives for the better. Building software is one of the best ways I have found to do both of those things at once.
Updated on August 01, 2020Comments
-
jarry jafery almost 4 years
The documentation for AVPlayer states the following:
[The] player works equally well with local and remote media files
However, the documentation for AVAudioPlayer states the following:
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream
For the work I am doing I need some of the capabilities of AVAudioPlayer, but all my audio is being streamed. The main thing I need from AVAudioPlayer that AVPlayer does not have is the "playing" property. It is difficult to build a player UI without that property, among others.
So what is the difference between AVPlayer and AVAudioPlayer that makes the latter unsuitable for network streaming? Is there a way to get some of the info from AVPlayer that AVAudioPlayer provides such as the "playing" property?
-
jarry jafery almost 14 yearsYeah I am using that now but it isn't super convenient. Thanks for the idea though!
-
RK- over 13 yearsHi Macinjosh, If you figure out the exactly the difference between the two, please post it.
-
Corey Floyd over 10 yearsIf you need exact timing for AVPlayer create AVURLAssets and pass the option
AVURLAssetPreferPreciseDurationAndTimingKey
when you initialize it. Huzzah! -
mkc842 over 10 yearsIs this still accurate? Have any important changes emerged in the past 3 years? Thanks
-
Yoon Lee over 10 yearsWrong. For me, AVPlayer worked out perfect in performance, accuracy for getting current time. AVPlayer is great when you have to deal with graphical played time path. it is not running stupid NSTimer, but runs more smooth in GCD Block. Check the sample of - (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block; it just works great.
-
arlomedia almost 10 years@mkc842, AVAudioPlayer can play from an iPod library URL since iOS 6. Also, you can set the volume of AVPlayer with the code at this answer: stackoverflow.com/a/6178912/462162
-
Michał Klimczak about 3 yearsI would add that completion and error handling for AVPlayer is messed up (notification center, kvo, not really reporting errors at all when local path is wrong etc). AVAudioPlayer has do-catch for errors and a completion handler.