Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
216 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
// | |||
// SMKMPMusicPlayer.h | |||
// SNRMusicKit | |||
// | |||
// Created by Indragie Karunaratne on 2012-08-28. | |||
// Copyright (c) 2012 Indragie Karunaratne. All rights reserved. | |||
// | |||
|
|||
#import <Foundation/Foundation.h> | |||
#import <MediaPlayer/MediaPlayer.h> | |||
#import "SMKPlayer.h" | |||
|
|||
/** | |||
SMKMPMusicPlayer is a wrapper for MPMusicPlayerController. It's implemented because | |||
AVQueuePlayer doesn't support iTunes Match streaming/downloading. | |||
SMKMPMediaTrack will automatically use this player when it's a track on the cloud | |||
*/ | |||
@interface SMKMPMusicPlayer : NSObject <SMKPlayer> | |||
|
|||
@property (nonatomic, copy) void (^finishedTrackBlock)(id<SMKPlayer> player, id<SMKTrack> track, NSError *error); | |||
@property (nonatomic, strong, readonly) MPMusicPlayerController *audioPlayer; | |||
|
|||
@property (nonatomic, assign) NSTimeInterval seekTimeInterval; | |||
@property (nonatomic, assign, readonly) NSTimeInterval playbackTime; | |||
@property (nonatomic, assign, readonly) BOOL playing; | |||
@property (nonatomic, strong, readonly) id<SMKTrack> currentTrack; | |||
|
|||
- (void)pause; | |||
- (void)play; | |||
|
|||
// Seeking (SMKPlayer @optional) | |||
- (void)seekToPlaybackTime:(NSTimeInterval)time; | |||
- (void)seekBackward; | |||
- (void)seekForward; | |||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,124 @@ | |||
// | |||
// SMKMPMusicPlayer.m | |||
// SNRMusicKit | |||
// | |||
// Created by Indragie Karunaratne on 2012-08-28. | |||
// Copyright (c) 2012 Indragie Karunaratne. All rights reserved. | |||
// | |||
|
|||
#import "SMKMPMusicPlayer.h" | |||
#import "SMKMPMediaTrack.h" | |||
#import "NSError+SMKAdditions.h" | |||
|
|||
@interface SMKMPMusicPlayer () | |||
@property (nonatomic, strong, readwrite) MPMusicPlayerController *audioPlayer; | |||
@property (nonatomic, assign, readwrite) BOOL playing; | |||
@property (nonatomic, strong) id<SMKTrack> oldCurrentTrack; | |||
@end | |||
|
|||
@implementation SMKMPMusicPlayer | |||
- (id)init | |||
{ | |||
if ((self = [super init])) { | |||
self.audioPlayer = [MPMusicPlayerController applicationMusicPlayer]; | |||
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; | |||
[nc addObserver:self selector:@selector(_playbackStateDidChange:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.audioPlayer]; | |||
[nc addObserver:self selector:@selector(_nowPlayingItemDidChange:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.audioPlayer]; | |||
[self.audioPlayer beginGeneratingPlaybackNotifications]; | |||
self.audioPlayer.repeatMode = MPMusicRepeatModeNone; | |||
self.audioPlayer.shuffleMode = MPMusicShuffleModeOff; | |||
} | |||
return self; | |||
} | |||
|
|||
- (void)dealloc | |||
{ | |||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |||
} | |||
|
|||
#pragma mark - SMKPlayer | |||
|
|||
+ (NSSet *)supportedContentSourceClasses | |||
{ | |||
return [NSSet setWithObject:NSClassFromString(@"SMKMPMediaContentSource")]; | |||
} | |||
|
|||
+ (BOOL)supportsPreloading | |||
{ | |||
return NO; | |||
} | |||
|
|||
- (BOOL)supportsSeeking | |||
{ | |||
return YES; | |||
} | |||
|
|||
- (id<SMKTrack>)currentTrack | |||
{ | |||
return self.oldCurrentTrack; | |||
} | |||
|
|||
- (void)playTrack:(id<SMKTrack>)track completionHandler:(void(^)(NSError *error))handler | |||
{ | |||
if ([track isKindOfClass:[SMKMPMediaTrack class]]) { | |||
SMKMPMediaTrack *mediaTrack = (SMKMPMediaTrack *)track; | |||
MPMediaItem *mediaItem = mediaTrack.representedObject; | |||
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:@[mediaItem]]; | |||
[self.audioPlayer setQueueWithItemCollection:collection]; | |||
[self.audioPlayer play]; | |||
if (handler) { handler(nil); } | |||
} else if (handler) { | |||
NSError *error = [NSError SMK_errorWithCode:SMKPlayerErrorFailedToEnqueueTrack description:@"MPMusicPlayerController failed to enqueue the track"]; | |||
handler(error); | |||
} | |||
} | |||
|
|||
- (void)pause | |||
{ | |||
[self.audioPlayer pause]; | |||
} | |||
|
|||
- (void)play | |||
{ | |||
[self.audioPlayer play]; | |||
} | |||
|
|||
- (NSTimeInterval)playbackTime | |||
{ | |||
return self.audioPlayer.currentPlaybackTime; | |||
} | |||
|
|||
+ (NSSet *)keyPathsForValuesAffectingPlaybackTime | |||
{ | |||
return [NSSet setWithObject:@"audioPlayer.currentPlaybackTime"]; | |||
} | |||
|
|||
- (void)seekToPlaybackTime:(NSTimeInterval)time | |||
{ | |||
self.audioPlayer.currentPlaybackTime = time; | |||
} | |||
|
|||
- (void)seekBackward | |||
{ | |||
self.audioPlayer.currentPlaybackTime = self.audioPlayer.currentPlaybackTime - self.seekTimeInterval; | |||
} | |||
|
|||
- (void)seekForward | |||
{ | |||
self.audioPlayer.currentPlaybackTime = self.audioPlayer.currentPlaybackTime + self.seekTimeInterval; | |||
} | |||
|
|||
#pragma mark - Notifications | |||
|
|||
- (void)_playbackStateDidChange:(NSNotification *)notification | |||
{ | |||
self.playing = self.audioPlayer.playbackState == MPMusicPlaybackStatePlaying; | |||
} | |||
|
|||
- (void)_nowPlayingItemDidChange:(NSNotification *)notification | |||
{ | |||
if (!self.audioPlayer.nowPlayingItem && self.finishedTrackBlock) { | |||
self.finishedTrackBlock(self, self.oldCurrentTrack, nil); | |||
} | |||
} | |||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.