當我在我的應用程序中用作共享實例的自定義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];
愚蠢的問題,也許,但有你爲什麼叫在設置功能中的「self.player」,只是「球員」的理由setCurrentPlaying函數? 而且,你在使用ARC嗎? – Jodocus
不,沒有理由。我實際上嘗試將它們全部更改爲self.player,但得到了相同的結果,是的,我正在使用ARC – kezi