0

當我在我的應用程序中用作共享實例的自定義NSObject中創建我的AVAudioPlyer時,我正在獲取EXC_BAD_ACCESS。我似乎無法隔離問題所在。它說player = [[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error:&error];的問題,但當我NSLog時播放器顯示不爲空,我也檢查了網址,它返回ipod-library://item/item.mp3?id=2689306087076130700,所以它也不爲空。注意:我在這個網站上看過類似的問題,但我仍然無法找到問題。我也試過[[AVAudioSession sharedInstance] setActive:YES error:&activationError];EXC_BAD_ACCESS當啓動AVAudioPlayer

我NSObject的:Player.h

#import <Foundation/Foundation.h> 
#import <AVFoundation/AVFoundation.h> 
#import <MediaPlayer/MediaPlayer.h> 

@interface Player : NSObject <AVAudioPlayerDelegate>{ 
    NSTimer *timer; 
} 

@property NSMutableArray *queue; 
@property NSMutableArray *upNext; 
@property AVAudioPlayer *player; 
@property NSTimer *timer; 

-(void)setCurrentPlaying:(MPMediaItem *)item; 
-(void)addUpNext:(MPMediaItem *)item; 
-(void)play; 
-(void)pause; 

-(void)setup; 
+ (id)sharedInstance; 
@end 

Player.m

#import "Player.h" 

@implementation Player 

@synthesize timer, player, queue; 

+ (id)sharedInstance{ 
    static Player *sharedInstance; 
    @synchronized(self) { 
     if (!sharedInstance){ 
      sharedInstance = [[Player alloc] init]; 
      [sharedInstance setup]; 
     } 
     return sharedInstance; 
    } 
} 

-(void)setCurrentPlaying:(MPMediaItem *)item{ 
    NSError *error = nil; 
    NSLog(@"%@l: %@", player, [item valueForProperty:MPMediaItemPropertyAssetURL]); 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error:&error]; 
    NSLog(@"%@", error.localizedDescription); 
    [player prepareToPlay]; 
    [player play]; 
} 

-(void)setup{ 
    NSError *activationError = nil; 
    [[AVAudioSession sharedInstance] setActive:YES error:&activationError]; 
    self.player = [[AVAudioPlayer alloc] init]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
            target:self 
            selector:@selector(check) 
            userInfo:nil 
            repeats:NO]; 
} 

在我看來,我呼籲:

[[Player sharedInstance] setCurrentPlaying:song]; 
+0

愚蠢的問題,也許,但有你爲什麼叫在設置功能中的「self.player」,只是「球員」的理由setCurrentPlaying函數? 而且,你在使用ARC嗎? – Jodocus

+0

不,沒有理由。我實際上嘗試將它們全部更改爲self.player,但得到了相同的結果,是的,我正在使用ARC – kezi

回答

1

你試圖訪問iPod庫?

我認爲不可能在iPod庫中使用AVAudioPlayer,除非您找到將歌曲複製到應用程序目錄的方法。

看看herehere

使用AVPlayer,而不是...

+0

感謝您指出。從我看到的其中一個問題中,他們表示在iOS5之後有可能。我想不是。我只是按照你的建議使用AVPlayer。 – kezi

+0

不客氣!在iOS5之後,我無法在有關MPMediaItem與AVAudioPlayer一起使用的文檔中找到任何參考... – Beppe

相關問題