当我在一个自定义NSObject中创建我的AVAudioPlyer时,我得到了一个EXC_BAD_ACCESS,我将其用作应用程序中的共享实例。不过,我似乎无法孤立问题出在哪里。它说player = [[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error:&error];
有问题,但当我NSLog它时,播放器显示不为空,而且我检查了url,它返回ipod-library://item/item.mp3?id=2689306087076130700
,所以它也不为空。注意:我确实在这个网站上看过类似的问题,但仍然找不到问题。我也试过[[AVAudioSession sharedInstance] setActive:YES error:&activationError];
我的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];
您正在尝试访问iPod库吗?
我认为不可能将AVAudioPlayer
与iPod库一起使用,除非你找到了将歌曲复制到应用程序目录的方法。
看看这里或这里
请改用AVPlayer。。。