0

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];
keji
  • 5,947
  • 3
  • 31
  • 47
  • stupid question perhaps, but is there a reason why you call 'self.player' in the setup function and just 'player' in the setCurrentPlaying function? And, are you working with ARC? – Jodocus Aug 03 '13 at 15:00
  • No there is no reason. I actually tried changing them all to self.player but that yielded the same result and yes I am using ARC – keji Aug 03 '13 at 21:18

1 Answers1

1

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...

Community
  • 1
  • 1
Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47
  • Thanks for pointing that out. From one of the questions I saw they said it was possible after iOS5. I guess not though. Ill just use the AVPlayer as you suggested. – keji Aug 04 '13 at 03:44
  • You are welcome!! I can't find any reference in the docs about MPMediaItem working with AVAudioPlayer after iOS5... – Giuseppe Garassino Aug 04 '13 at 09:55