在iOS中单击uicollectionviewcell时播放音频

时间:2013-05-18 14:36:19

标签: ios collectionview

当用户选择集合视图自定义单元格时,我想播放mp3文件文件。但它不是在玩。这是我为此目的编写的代码 - (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];

}

2 个答案:

答案 0 :(得分:0)

一旦尝试这样,一旦检查它是否存在于bundle Resources中。

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

答案 1 :(得分:0)

Check this checkbox.还要尝试将播放器存储为类属性。当声明为函数属性时,它可以取消分配。