2 IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in
3 consideration of your agreement to the following terms, and your use, installation,
4 modification or redistribution of this Apple software constitutes acceptance of these
5 terms. If you do not agree with these terms, please do not use, install, modify or
6 redistribute this Apple software.
8 In consideration of your agreement to abide by the following terms, and subject to these
9 terms, Apple grants you a personal, non-exclusive license, under Appleās copyrights in
10 this original Apple software (the "Apple Software"), to use, reproduce, modify and
11 redistribute the Apple Software, with or without modifications, in source and/or binary
12 forms; provided that if you redistribute the Apple Software in its entirety and without
13 modifications, you must retain this notice and the following text and disclaimers in all
14 such redistributions of the Apple Software. Neither the name, trademarks, service marks
15 or logos of Apple Inc. may be used to endorse or promote products derived from
16 the Apple Software without specific prior written permission from Apple. Except as expressly
17 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18 herein, including but not limited to any patent rights that may be infringed by your
19 derivative works or by other works in which the Apple Software may be incorporated.
21 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
22 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
24 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
26 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
29 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
30 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
31 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #import "AudioPlayer.h"
36 #import <AVFoundation/AVFoundation.h>
38 static void *itemStatusKVOContext = &itemStatusKVOContext;
40 @implementation AudioPlayer
42 @synthesize audioPlayerDelegate=_audioPlayerDelegate;
44 - (instancetype)initWithURL:(NSURL *)url
46 if (!url.absoluteString.length)
49 if (!(self = [super init]))
52 AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
53 _playerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
54 [_playerItem addObserver:self forKeyPath:@"status" options:0 context:itemStatusKVOContext];
55 _audioPlayer = [[AVPlayer alloc] initWithPlayerItem:_playerItem];
62 [_playerItem removeObserver:self forKeyPath:@"status"];
63 [_audioPlayer release];
64 [_playerItem release];
70 return _audioPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay;
78 - (void)setPlaying:(BOOL)playing
80 if (_playing == playing)
83 if (playing && !self.isReadyToPlay)
93 [_audioPlayerDelegate playStateDidChangeForAudioPlayer:self];
98 return _audioPlayer.isMuted;
101 - (void)setMuted:(BOOL)muted
103 if (self.isMuted == muted)
106 _audioPlayer.muted = muted;
107 [_audioPlayerDelegate mutedStateDidChangeForAudioPlayer:self];
110 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
112 if (context == &itemStatusKVOContext) {
113 dispatch_async(dispatch_get_main_queue(), ^{ [_audioPlayerDelegate readyStateDidChangeForAudioPlayer:self]; });
116 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];