启动AVAudioPlayer时EXC_BAD_ACCESS

时间:2013-08-03 14:05:43

标签: iphone objective-c avaudioplayer exc-bad-access mpmediaitem

当我在自定义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];

我的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];

1 个答案:

答案 0 :(得分:1)

您是否尝试访问iPod库?

我认为除非您找到将歌曲复制到应用目录的方法,否则无法将AVAudioPlayer用于iPod库。

查看herehere

使用AVPlayer代替......