2017-04-14 108 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

    相關問題