I have an AVQueuePlayer
property which is set up as below. The NSLog
statements report that the audio should be playing properly (while the app is running) however when testing in the simulator and on the iOS device itself (iPhone 5s iOS 8.1.1) there is no audio playing.
I have confirmed that the device is not muted, and is at full volume. I am using Xcode 6 and iOS 8 SDK. I have import AVFoundation framework in the "Link binaries" section and have imported it into my class.
-(void)viewDidLoad
{
[super viewDidLoad];
[self music];
// other setup code
}
- (void)music
{
music = [[NSMutableArray alloc]initWithArray:@[
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM1"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM2"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM3"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM4"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM5"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM6"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM7"
ofType:@"mp3" ]]]
]];
for(AVPlayerItem *a in music)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:a];
}
[music shuffle];
self.audio = [[AVQueuePlayer alloc]initWithItems:music];
[self.audio play];
NSLog(@"Now playing%@",[self.audio currentItem]);
NSLog(@"%@",self.audio);
}
-(void)playerItemFinished:(NSNotification *)notification
{
if([[self.audio currentItem] isEqual:[music lastObject]])
{
self.audio = nil;
[music shuffle];
self.audio = [[AVQueuePlayer alloc]initWithItems:music];
[self.audio play];
}
}