2017-04-14 43 views
0

我在每个单元格上都有一个按钮,并且点击按钮时,我想为每个单元格从阵列播放不同的音频文件。Objective-C如何在点击单元格按钮时播放声音?

我不知道如何为我的表格视图单元格实现声音/音频文件的数组。

我有一个按钮插座和我的UITableViewCell上的动作。但我不知道如何初始化我AVaudioplayer在tableview.m特别是在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell22"; 

下面我插入我想要实现这个项目的形象。 image1

回答

0

检查现在我更新的代码:在.H或.m文件的播放声音

1.创建属性UR AVAudioPlayer。像

@property (strong, nonatomic) AVAudioPlayer *audioPlayer; 
  • 声明在.m文件<AVAudioPlayerDelegate>
  • @synthesize audioPlayer;

    3。实现此代码:

    - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法: (NSIndexPath *)indexPath {

    yourSoundArray=[NSArray arrayWithObjects:@"sss",@"sss",@"sss",@"sss",@"sss",@"sss", nil];//dont include formate type 
    
    
        NSString *audioPath = [[NSBundle mainBundle] pathForResource: [yourSoundArray objectAtIndex:indexPath.row] ofType:@"mp3"]; 
            NSLog(@"%@",audioPath); 
    
            NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
    
            NSError *audioError = [[NSError alloc] init]; 
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError]; 
            audioPlayer.delegate=self; 
    
           if (!audioError) { 
             NSLog(@"playing!"); 
             audioPlayer play]; 
            } 
            else { 
              NSLog(@"Error!"); 
            } 
    

    4.

    -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
    { 
        NSLog(@"%d",playing the audio); 
    } 
    

    编码快乐的工作请检查:

    +0

    非常感谢您的代码。我想通过声音分配给每个单元的按钮: 如果([_detailPhrases isEqualToString:@“基础知识”]){ } 能否上述内,实现代码if语句,并与连接的按钮? – didayo

    +0

    在这个不需要按钮只需点击单元格,它会播放声音。播放声音无需创建按钮的努力。只要你可以休息上面的代码,它会工作完美 –

    +0

    在这个不需要按钮只需点击单元格,它会播放声音。用于播放声音无需创建按钮的额外工作,为按钮提供标签值并检查条件。只是你可以在代码上面休耕,它会工作完美。你可以完美地处理响应。 –

    3

    你可以尝试这样的:

    1. 进口AVFoundation.framework

    2. 将此代码放在didSelectRowAtIndexPath方法

      的NSString *和声= [[一个NSBundle mainBundle] pathForResource:@“你声音名称“ofType:@”mp3“];

      NSURL * soundUrl = [NSURL fileURLWithPath:soundPath]; AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; [玩家播放];

    注:添加音频文件到项目,然后把名字变成一个数组。然后您可以使用基于indexPath的音频名称。

    如果你要玩上按一下按钮这是tableViewCell然后添加标签为indexPath到该按钮,并在cellForRowAtIndexPath方法

    button.tag = indexPath.row; 
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    

    添加动作像这个按钮并粘贴按钮动作相同的代码声。但使用sender.tag代替indexPath.row

    -(void)buttonPressed:(UIButton *)sender 
    { 
        NSString *soundName = [yourArray objectAtIndex:sender.tag]; 
    
        NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"]; 
    
        NSURL *soundUrl = [NSURL fileURLWithPath:soundPath]; 
    
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; [player play]; 
    } 
    
    +0

    非常感谢您的代码。我想通过声音分配给每个单元的按钮: 如果([_detailPhrases isEqualToString:@“基础知识”]){ } 能否上述内,实现代码if语句,并与连接的按钮? – didayo

    +0

    什么是_detalPhrases和Basics? – Himanth

    +0

    看到我编辑的答案 – Himanth

    0

    配合您需要使用两件东西主要是 第一次导入有关详细信息, AVFounfation.framwork 第二待办事项代码ID “didSelectRowAtIndexPath方法” Xcode 6 - Press button to play sound

    0

    - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"%@",selectedCell.textLabel.text); 
    AVAudioPlayer *audioPlayer101; 
    
    if ([_detailPhrases isEqualToString:@"Basics"]){ 
    
    
    NSArray *soundArray = [NSArray arrayWithObjects:@"cellTapSound", @"Second", nil]; 
    
    NSString *audioPath = [[NSBundle mainBundle] pathForResource: [soundArray objectAtIndex:indexPath.row] ofType:@".mp3"]; 
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
    NSError *audioError = [[NSError alloc] init]; 
    audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError]; 
    
    if (!audioError) { 
        [audioPlayer101 play]; 
        NSLog(@"playing!"); 
    } 
    else { 
        NSLog(@"Error!"); 
    } 
    } 
    

    }

    +0

    你的问题是什么?什么是_detailPharses –

    +0

    cellTapSound,第二个 - 他们是否是mp3格式的音频文件 –

    +0

    我正在使用两个表格视图控制器:/我需要将声音文件添加到第二个表格视图的单元格中第一个表视图。'_detailPhrases'用于执行两个表视图控制器之间的连接。 – didayo

    相关问题