【发布时间】:2013-08-03 14:05:43
【问题描述】:
当我在一个自定义 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
播放器.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];
【问题讨论】:
-
也许是个愚蠢的问题,但是有没有理由在 setup 函数中调用“self.player”而在 setCurrentPlaying 函数中只调用“player”?而且,您在使用 ARC 吗?
-
不,没有理由。我实际上尝试将它们全部更改为 self.player 但产生了相同的结果,是的,我正在使用 ARC
标签: iphone objective-c avaudioplayer exc-bad-access mpmediaitem