我正在创建子类AVPlayer
,以便我可以在音乐队列中创建skipsBackwards
的方法。我想构建一个功能齐全的音乐播放器,它使用iPod用户的音乐。这意味着AVAudioPlayer
和MPMusicPlayerController
一样。此外,Core Audio让我感到害怕,所以我希望暂时避开它。
我已经考虑过使用类别来扩展AVPlayer
类但是我需要我的子类来创建一个类变量来存储MPMediaItemCollection
所以我认为它最有意义的是子类而不是添加一个类别
这是我的例外
![在此处输入图片说明] [1]
这是我的appDelegate代码,我在其中设置AVPlayer
,其子类为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;
}
这是我非常基本的子类......
#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
子类播放。问题是,只要我调用skipForward
的{{1}}方法,我就会收到无法识别的选择器错误,并且应用程序崩溃了。我在这里缺少什么?
答案 0 :(得分:3)
问题是,您调用的playerWithURL
方法由AVPlayer
拥有,并且即使在子类化时也会始终返回AVPlayer
,除非您覆盖它。尝试使用:
self.bookPlayer = [[SWPlayer alloc] initWithURL:url];