-1

I want to play mp3 file file when user select a collection view custom cell. but it is not playing . here is the code i have written for this purpose -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"cell selected ");
    AVAudioPlayer * audioPlayer;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bismillah" ofType:@"mp3"];
    NSURL *audioUrl=[NSURL fileURLWithPath:path];
    audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:audioUrl error:nil];
    //audioPlayer.delegate=self;
    [audioPlayer play];
   // cell.backgroundColor = [UIColor blueColor];

}

Atif Saeed
  • 27
  • 4

2 Answers2

0

once try like this, and once check is it present in bundle Resources or not.

NSString *audio=[[NSBundle mainBundle]pathForResource:@"Ranz des Vaches" ofType:@"mp3"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:audio];
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
avPlayer.delegate = self;
[avPlayer prepareToPlay];
[avPlayer play];
Balu
  • 8,470
  • 2
  • 24
  • 41
  • Still not working...can You please list down the steps needed to play audio when collection view cell is selected. – Atif Saeed May 18 '13 at 15:09
  • have you checked in bundle Resources files? – Balu May 18 '13 at 15:10
  • kindly answer my question ...i have tried may ways but still audio is not playing. Is it possible to play the audio in didSelectItemAtIndexPath delegate function of Collection view cell? – Atif Saeed May 19 '13 at 14:08
0

Check this checkbox. Also try to store your player as a class property. It can dealloc when declared as function property.

Roman R
  • 1
  • 3