Skip to content

Commit

Permalink
Basic queueing is functional
Browse files Browse the repository at this point in the history
  • Loading branch information
indragiek committed Oct 23, 2015
1 parent 7ad0215 commit 859b16d
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Audio Players/SMKAVQueuePlayer.m
Expand Up @@ -30,7 +30,7 @@ @implementation SMKAVQueuePlayer {
- (id)init
{
if ((self = [super init])) {
self.audioPlayer = [AVQueuePlayer queuePlayerWithItems:nil];
self.audioPlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray array]];
__weak SMKAVQueuePlayer *weakSelf = self;
_timeObserver = [self.audioPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.f, 1.f) queue:NULL usingBlock:^(CMTime time) {
SMKAVQueuePlayer *strongSelf = weakSelf;
Expand Down
3 changes: 3 additions & 0 deletions Audio Players/SMKMPMusicPlayer.m
Expand Up @@ -8,6 +8,8 @@

#import "SMKMPMusicPlayer.h"
#import "SMKMPMediaTrack.h"

#import "SMKErrorCodes.h"
#import "NSError+SMKAdditions.h"

@interface SMKMPMusicPlayer ()
Expand All @@ -29,6 +31,7 @@ - (id)init
[self.audioPlayer beginGeneratingPlaybackNotifications];
self.audioPlayer.repeatMode = MPMusicRepeatModeNone;
self.audioPlayer.shuffleMode = MPMusicShuffleModeOff;
self.seekTimeInterval = SMKPlayerDefaultSeekTimeInterval;
}
return self;
}
Expand Down
6 changes: 5 additions & 1 deletion Audio Players/SMKSpotifyPlayer.m
Expand Up @@ -43,6 +43,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if (self.finishedTrackBlock)
self.finishedTrackBlock(self, self.oldCurrentTrack, nil);
self.oldCurrentTrack = nil;
if (self.preloadedTrack) {
[self skipToPreloadedTrack];
}
} else {
self.preloadedTrack = nil;
}
Expand Down Expand Up @@ -124,7 +127,8 @@ - (void)preloadTrack:(id<SMKTrack>)track completionHandler:(void(^)(NSError *err

- (void)skipToPreloadedTrack
{
[self playTrack:self.preloadedTrack completionHandler:nil];
if (self.preloadedTrack)
[self playTrack:self.preloadedTrack completionHandler:nil];
}

#pragma mark - Accessors
Expand Down
1 change: 0 additions & 1 deletion Common Headers/SMKErrorCodes.h
Expand Up @@ -9,7 +9,6 @@
extern NSInteger const SMKPlayerErrorFailedToCreateInputSource;
extern NSInteger const SMKPlayerErrorFailedToCreateDecoder;
extern NSInteger const SMKPlayerErrorFailedToEnqueueTrack;
extern NSInteger const SMKPlayerErrorTrackAlreadyPreloaded;
extern NSInteger const SMKPlayerErrorItemAlreadyExists;

extern NSInteger const SMKCoreDataErrorDataStoreNotAFolder;
Expand Down
1 change: 1 addition & 0 deletions Common Headers/SMKErrorCodes.m
Expand Up @@ -11,6 +11,7 @@
NSInteger const SMKPlayerErrorFailedToCreateInputSource = 0;
NSInteger const SMKPlayerErrorFailedToCreateDecoder = 1;
NSInteger const SMKPlayerErrorItemAlreadyExists = 2;
NSInteger const SMKPlayerErrorFailedToEnqueueTrack = 3;

NSInteger const SMKCoreDataErrorDataStoreNotAFolder = 4;
NSInteger const SMKCoreDataErrorFailedToInitializeStore = 5;
4 changes: 3 additions & 1 deletion Content Sources/MPMediaLibrary/Data Model/SMKMPMediaTrack.m
Expand Up @@ -9,6 +9,8 @@
#import "SMKMPMediaTrack.h"
#import "SMKMPMediaAlbum.h"
#import "SMKMPMediaHelpers.h"
#import "SMKAVQueuePlayer.h"
#import "SMKMPMusicPlayer.h"

@interface SMKMPMediaAlbum (SMKInternal)
- (id)initWithRepresentedObject:(MPMediaItem*)object contentSource:(id<SMKContentSource>)contentSource;
Expand Down Expand Up @@ -44,7 +46,7 @@ + (NSSet *)supportedSortKeys

- (Class)playerClass
{
return NSClassFromString((self.playbackURL != nil) ? @"SMKAVQueuePlayer" : @"SMKMPMusicPlayer");
return (self.playbackURL != nil) ? [SMKAVQueuePlayer class] : [SMKMPMusicPlayer class];
}

#pragma mark - SMKTrack
Expand Down
1 change: 1 addition & 0 deletions Example Apps/MPMediaLibraryExample/DetailViewController.h
Expand Up @@ -13,4 +13,5 @@
@property (strong, nonatomic) id detailItem;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
- (IBAction)seekForward:(id)sender;
@end
18 changes: 11 additions & 7 deletions Example Apps/MPMediaLibraryExample/DetailViewController.m
Expand Up @@ -10,6 +10,7 @@
#import "SNRMusicKitiOS.h"
#import "SMKMPMediaContentSource.h"
#import "SMKMPMusicPlayer.h"
#import "SMKQueueController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
Expand All @@ -18,7 +19,7 @@ - (void)configureView;
@end

@implementation DetailViewController {
SMKMPMusicPlayer *_player;
SMKQueueController *_controller;
}

#pragma mark - Managing the detail item
Expand All @@ -42,7 +43,6 @@ - (void)configureView
strongSelf.tracks = tracks;
[self.tableView reloadData];
}];
_player = [SMKMPMusicPlayer new];
}

- (void)viewDidLoad
Expand Down Expand Up @@ -74,11 +74,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SMKMPMediaTrack *object = self.tracks[indexPath.row];
NSLog(@"%@", [object playbackURL]);
[_player playTrack:object completionHandler:^(NSError *error) {
NSLog(@"%@", error);
}];
NSArray *array = [self.tracks subarrayWithRange:NSMakeRange(indexPath.row, [self.tracks count] - indexPath.row)];
NSLog(@"%@", array);
_controller = [SMKQueueController queueControllerWithTracks:array];
[_controller play:nil];
}

#pragma mark - Split view
Expand All @@ -97,4 +96,9 @@ - (void)splitViewController:(UISplitViewController *)splitController willShowVie
self.masterPopoverController = nil;
}

- (IBAction)seekForward:(id)sender
{
[_controller seekForward:nil];
}

@end
Expand Up @@ -56,7 +56,13 @@
<outlet property="delegate" destination="k1S-Fw-fPT" id="NJm-jj-izg"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Tracks" id="xYw-DX-WhN"/>
<navigationItem key="navigationItem" title="Tracks" id="xYw-DX-WhN">
<barButtonItem key="rightBarButtonItem" title="Seek Forward" id="df0-Kk-nhT">
<connections>
<action selector="seekForward:" destination="k1S-Fw-fPT" id="HfE-WB-NFW"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="view" destination="jqB-za-Asj" id="yMr-mm-BhV"/>
</connections>
Expand Down Expand Up @@ -143,6 +149,7 @@
<class className="DetailViewController" superclassName="UITableViewController">
<source key="sourceIdentifier" type="project" relativePath="./Classes/DetailViewController.h"/>
<relationships>
<relationship kind="action" name="seekForward:"/>
<relationship kind="outlet" name="detailDescriptionLabel" candidateClass="UILabel"/>
</relationships>
</class>
Expand Down
52 changes: 33 additions & 19 deletions Master API/SMKQueueController.m
Expand Up @@ -15,10 +15,10 @@ @interface SMKQueueItem : NSObject
@interface SMKQueueController ()
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong, readwrite) id<SMKPlayer> currentPlayer;
@property (nonatomic, assign, readwrite) NSUInteger indexOfCurrentTrack;
@end

@implementation SMKQueueController

- (id)init
{
if ((self = [super init])) {
Expand Down Expand Up @@ -73,16 +73,6 @@ - (void)removeTrack:(id<SMKTrack>)track

#pragma mark - Accessors

- (NSUInteger)indexOfCurrentTrack
{
return [self.items indexOfObject:self.currentTrack];
}

+ (NSSet *)keyPathsForValuesAffectingIndexOfCurrentTrack
{
return [NSSet setWithObject:@"currentTrack"];
}

- (NSTimeInterval)playbackTime
{
return [self.currentPlayer playbackTime];
Expand Down Expand Up @@ -177,7 +167,15 @@ - (IBAction)playPause:(id)sender

- (IBAction)next:(id)sender
{

if ([[self.currentPlayer class] supportsPreloading] && [self.currentPlayer preloadedTrack]) {
[self.currentPlayer skipToPreloadedTrack];
self.indexOfCurrentTrack++;
} else {
NSUInteger nextIndex = self.indexOfCurrentTrack + 1;
if (nextIndex < [self countOfItems])
[self _beginPlayingItemAtIndex:nextIndex];
}
[self _recalculateIndexOfCurrentTrack];
}

- (IBAction)previous:(id)sender
Expand All @@ -187,12 +185,12 @@ - (IBAction)previous:(id)sender

- (IBAction)seekForward:(id)sender
{

[self.currentPlayer seekForward];
}

- (IBAction)seekBackward:(id)sender
{

[self.currentPlayer seekBackward];
}

#pragma mark - Private
Expand Down Expand Up @@ -223,17 +221,33 @@ - (NSUInteger)_indexOfTrack:(id<SMKTrack>)track
- (void)_beginPlayingItemAtIndex:(NSUInteger)index
{
id<SMKTrack> track = [[self.items objectAtIndex:index] track];
self.currentPlayer = [[track playerClass] new];
id<SMKPlayer> player = [[track playerClass] new];
NSLog(@"now playing %@ with %@", [track name], player);
self.currentPlayer = player;
__weak SMKQueueController *weakSelf = self;
[self.currentPlayer playTrack:track completionHandler:^(NSError *error) {
[player playTrack:track completionHandler:^(NSError *error) {
SMKQueueController *strongSelf = weakSelf;
if (error) {
SMKQueueController *strongSelf = weakSelf;
SMKGenericErrorLog([NSString stringWithFormat:@"Error playing track %@", track], error);
[strongSelf removeObjectFromItemsAtIndex:index];
strongSelf.currentPlayer = nil;
if (index < [self.items count])
[self _beginPlayingItemAtIndex:index];
if (index < [strongSelf countOfItems])
[strongSelf _beginPlayingItemAtIndex:index];
} else {
[strongSelf _recalculateIndexOfCurrentTrack];
}
}];
[player setFinishedTrackBlock:^(id<SMKPlayer> player, id<SMKTrack> track, NSError *error) {
SMKQueueController *strongSelf = weakSelf;
[strongSelf next:nil];
}];
}

- (void)_recalculateIndexOfCurrentTrack
{
self.indexOfCurrentTrack = [self _indexOfTrack:self.currentTrack];
}
@end

@implementation SMKQueueItem
@end
4 changes: 4 additions & 0 deletions SNRMusicKit.xcodeproj/project.pbxproj
Expand Up @@ -118,6 +118,8 @@
036994CF15E3F3EB003BCCBE /* UIImageView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 036994A615E3F3EB003BCCBE /* UIImageView+AFNetworking.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
036994D015E3F461003BCCBE /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 036991F615E3EAE1003BCCBE /* CoreData.framework */; };
036994D215E3F475003BCCBE /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 036994D115E3F475003BCCBE /* CoreData.framework */; };
036F857515EEC45100DF72B9 /* SMKQueueController.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0371CF9915EDECF200B304CC /* SMKQueueController.h */; };
036F857615EEC54500DF72B9 /* SMKAVQueuePlayer.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 03FBEC9D15E6E0C100A67DEE /* SMKAVQueuePlayer.h */; };
0370109C15E5535500FEAF6F /* SMKiTunesSyncOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 0370109A15E5535500FEAF6F /* SMKiTunesSyncOperation.h */; };
0370109D15E5535500FEAF6F /* SMKiTunesSyncOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 0370109B15E5535500FEAF6F /* SMKiTunesSyncOperation.m */; };
037010A015E5630000FEAF6F /* SMKiTunesConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 0370109E15E5630000FEAF6F /* SMKiTunesConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -403,6 +405,8 @@
0306DB6B15EC199800D6409B /* SMKMPMediaTrack.h in CopyFiles */,
0371CF9515EDE47700B304CC /* SMKMPMediaAlbum.h in CopyFiles */,
0371CF9315EDE44C00B304CC /* SMKMPMusicPlayer.h in CopyFiles */,
036F857515EEC45100DF72B9 /* SMKQueueController.h in CopyFiles */,
036F857615EEC54500DF72B9 /* SMKAVQueuePlayer.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.

0 comments on commit 859b16d

Please sign in to comment.