Reputation: 9866
Here is my code on button click
- (void)playButton:(id)sender
{
NSLog(@"Play Button Clicked!!!...");
audioPlayer = [self setUpAudioPlayer:[songs objectAtIndex:i]];
}
-(AVAudioPlayer *)setUpAudioPlayer:(NSString *)filename
{
NSLog(@"File name : %@",filename);
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"caf"];
NSLog(@"File path = %@",path);
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//audioPlayer.volume = 0.5;
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(@"Song is playing....");
return [audioPlayer autorelease];
}
here songs is NSMutableArray which contains file name... All things go correct with NSLog... But no sound is coming and no error comes.. What is wrong with the code..???
Upvotes: 1
Views: 1693
Reputation: 10245
Your audio player is probably getting released as soon as it starts playing. Try to retain it after setUpAudioPlayer
is called.
Upvotes: 3
Reputation: 5005
Most probably you need to initialize an audio session, some like this
- (void)setupAudioSession { AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error = nil; [session setCategory: AVAudioSessionCategoryPlayback error: &error]; if (error != nil) NSLog(@"Failed to set category on AVAudioSession"); // AudioSession and AVAudioSession calls can be used interchangeably OSStatus result = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self); if (result) NSLog(@"Could not add property listener! %d\n", result); BOOL active = [session setActive: YES error: nil]; if (!active) NSLog(@"Failed to set category on AVAudioSession"); }
Upvotes: 1