2012-01-27 63 views
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播放音樂。問題是,只要我撥打SWPlayerskipForward方法,我就會收到無法識別的選擇器錯誤,並且應用程序崩潰。我在這裏錯過了什麼?

回答

3

的問題是,playerWithURL方法您呼叫由AVPlayer擁有,總是會返回一個AVPlayer,子類甚至當,除非你將其覆蓋。嘗試創建它:

self.bookPlayer = [[SWPlayer alloc] initWithURL:url]; 
+0

解決了這個問題。謝謝彼得。 – Aaronium112 2012-01-27 20:11:47

+0

np,並感謝重新格式化問題,以便其他人可以輕鬆搜索它! – 2012-01-27 22:19:48

+0

重新格式化很好,但我花了一段時間才注意到Peter引用了'playerWithURL' - 從原始文章中刪除此內容使得很難看到問題出在哪裏。 – amergin 2014-01-21 18:25:42