【发布时间】:2012-01-27 00:19:55
【问题描述】:
我正在对AVPlayer 进行子类化,以便我可以在音乐队列中创建skipsBackwards 的方法。我想构建一个功能齐全的音乐播放器,使用来自用户 iPod 的音乐。所以这意味着AVAudioPlayer 和MPMusicPlayerController 都出局了。 Core Audio 也让我害怕,所以我希望暂时避开它。
我考虑过使用类别来扩展 AVPlayer 类,但我需要我的子类有一个类变量来存储 MPMediaItemCollection,所以我认为子类比添加类别更有意义。
这是我的例外
![在此处输入图片描述][1]
这是我的 appDelegate 代码,我在其中设置了子类为 SWPlayer 的 AVPlayer
#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;
}
这是我非常基本的子类...
#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 方法,我就会收到无法识别的选择器错误并且应用程序崩溃。我在这里缺少什么?
【问题讨论】:
标签: ios design-patterns avplayer subclassing