Pergunta

Im getting a EXC_BAD_ACCESS when I create my AVAudioPlyer in a custom NSObject that I use as a shared instance across my application. I can't seem to isolate where the problem is though. It says the problems at player = [[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error:&error]; but player shows up not null when I NSLog it, also I checked the url and it returns ipod-library://item/item.mp3?id=2689306087076130700 so it is not null either. Note: I did look at similar questions on this site but am still unable to find the problem. I also tried [[AVAudioSession sharedInstance] setActive:YES error:&activationError];

My 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];
}

In my view I am calling:

[[Player sharedInstance] setCurrentPlaying:song];
Foi útil?

Solução

Are you trying to access iPod library?

I think it is not possible to use AVAudioPlayer with iPod library, unless you find a way to copy songs to your app directory.

Take a look here or here

Use AVPlayer instead...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top