0

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

1 回答 1

1

您是否正在尝试访问 iPod 库?

我认为不可能AVAudioPlayer与 iPod 库一起使用,除非您找到将歌曲复制到应用程序目录的方法。

看看这里这里

改用AVPlayer ...

于 2013-08-04T02:29:38.040 回答