1
我正在致力於子類AVPlayer
,以便我可以創建一個方法,將skipsBackwards
排列在音樂隊列中。我想創建一個功能齊全的音樂播放器,使用用戶iPod的音樂。所以這意味着AVAudioPlayer
已經出來以及MPMusicPlayerController
。另外核心音頻讓我害怕,所以我現在希望避開這種情況。如何劃分AVPlayer?
我使用類別來擴展AVPlayer
類考慮,但我需要我的子類有一個類變量,存儲MPMediaItemCollection
所以我想,這最有意義的子類,而該添加類別。
這裏是我的例外
![在這裏輸入的形象描述] [1]
這裏是我的appDelegate代碼,我的配置,它的子類爲SWPlayer
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "SWPlayer.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain)SWPlayer *bookPlayer;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Setup AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
// Allow the audio to mix with other apps (necessary for background sound)
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
//Grab some Songs to Test with
MPMediaQuery *query = [MPMediaQuery songsQuery];
NSArray *collection = [query items];
//Setup bookPLayer to Test
MPMediaItem *item = [collection objectAtIndex:5];
NSLog(@"%@",[item valueForProperty:MPMediaItemPropertyTitle]);
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
self.bookPlayer = [[SWPlayer alloc]initWithURL:url];
[bookPlayer play];
[bookPlayer skipForwards];
return YES;
}
這裏AVPlayer
是我很基本子類...
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface SWPlayer : AVPlayer
@property(nonatomic,retain)MPMediaItemCollection *mediaItemCollection;
-(void)skipForwards;
-(void)skipBackwards;
@end
#import "SWPlayer.h"
@implementation SWPlayer
@synthesize mediaItemCollection;
-(void)skipForwards{
NSLog(@"SWPlayer called skipForward method");
}
-(void)skipBackwards{
NSLog(@"SWPlayer called skipBackward method");
}
@end
使用我的子類AVPlayer
播放音樂。問題是,只要我撥打SWPlayer
的skipForward
方法,我就會收到無法識別的選擇器錯誤,並且應用程序崩潰。我在這裏錯過了什麼?
解決了這個問題。謝謝彼得。 – Aaronium112 2012-01-27 20:11:47
np,並感謝重新格式化問題,以便其他人可以輕鬆搜索它! – 2012-01-27 22:19:48
重新格式化很好,但我花了一段時間才注意到Peter引用了'playerWithURL' - 從原始文章中刪除此內容使得很難看到問題出在哪裏。 – amergin 2014-01-21 18:25:42