1

I have a button on each cell and while the button is tapped I want to play different audio file from array for each cell.

I don't know how to implement an array of sounds/audio files for my table view cells.

I have a button outlet and action on my UITableViewCell. But I am not sure how to initialise my AVaudioplayer in tableview.m specifically in:

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

Below I've inserted an image of the project I want to achieve. image1

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
didayo
  • 21
  • 5

4 Answers4

3

You can try like this:

  1. Import AVFoundation.framework

  2. Place this code in didSelectRowAtIndexPath method

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"your sound name" ofType:@"mp3"];

    NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; [player play];

Note: Add your audio files to project and then take the names into one array. Then you can use your audio names based on indexPath.

If you want to play sound on button click which is in tableViewCell then add tag as indexPath to that button and add action to button like this in cellForRowAtIndexPath method

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

and paste the same code in button action. but use sender.tag instead of 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];
}
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Himanth
  • 2,381
  • 3
  • 28
  • 41
  • Thank you very much for your code. I am trying to assign sounds to the button of each cell through: if ([_detailPhrases isEqualToString:@"Basics"]) { } Can I implement code above within such If statement and with the connection to the button? – didayo Apr 14 '17 at 10:53
  • What is the _detalPhrases and Basics? – Himanth Apr 14 '17 at 11:49
0

check now i updated code :its playing sound

1.create property in .h or .m file for ur AVAudioPlayer . like

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
  1. declare <AVAudioPlayerDelegate> in .m file and

@synthesize audioPlayer;

3.implement this code in :

-(void)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);
}

happy coding its working please check :

NAVEEN KUMAR
  • 669
  • 6
  • 25
  • Thank you very much for your code. I am trying to assign sounds to the button of each cell through: if ([_detailPhrases isEqualToString:@"Basics"]) { } Can I implement code above within such If statement and with the connection to the button? – didayo Apr 14 '17 at 10:55
  • in this no need button just tap on cell it will play sound. for playing sound no need to create button its effort. simply u can fallow above code it will work perfect – NAVEEN KUMAR Apr 14 '17 at 11:07
  • in this no need button just tap on cell it will play sound. for playing sound no need to create button its extra work, giving tag values for the buttons and checking the conditions . simply u can fallow above code it will work perfect. u can handle response perfectly. – NAVEEN KUMAR Apr 14 '17 at 11:14
  • Thank you for help, I've added the inserted code above. Unfortunately, my sounds are not being played while tapping on the cells. Maybe I did something wrong. Could you please guide me. – didayo Apr 14 '17 at 11:53
  • NAVEEN KUMAR thank you very much ;) Now everything is working ;) I am so glad that you have helped me with it ;) I have one last question, where did get so much knowledge about AVFoundation? Because me and my friend started our objective-c studies recently, and we want to get into depth of Audio playing ;) Thank you for help and for your recommendation ) – didayo Apr 14 '17 at 12:59
  • Wc .. i am glad to help u. can follow just google and stackoverflow previous answers and compare with ur code u will get solutions easily. for better knowledge fallow apple Documentation. https://developer.apple.com/reference/avfoundation/avaudioplayer . please upvote the answer. :D – NAVEEN KUMAR Apr 14 '17 at 13:05
0

Mate You need to use two things mostly 1st import AVFounfation.framwork 2nd Do code id "didSelectRowAtIndexPath" for more details Xcode 6 - Press button to play sound

Community
  • 1
  • 1
singh.jitendra
  • 490
  • 1
  • 9
  • 19
0

-(void)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!");
}
}

}

didayo
  • 21
  • 5
  • what is ur issue ? and what is _detailPharses – NAVEEN KUMAR Apr 14 '17 at 11:54
  • cellTapSound,Second - are they mp3 format audio files or not – NAVEEN KUMAR Apr 14 '17 at 11:57
  • I am working with two table view controllers :/ and I need to add sound files to the cells of the second table view from the 'Basics' cell of the first table view.The '_detailPhrases' is used to perform segue connection between two table view controller. – didayo Apr 14 '17 at 11:57
  • for exaple u need to save soundAraray like this NSArray *soundAraray = [NSArray arrayWithObjects: @"cellTapSound.mp3", @"Second.mp3", nil]; – NAVEEN KUMAR Apr 14 '17 at 11:59
  • u tryed ?.. is it playing – NAVEEN KUMAR Apr 14 '17 at 12:05
  • I've tried, but unfortunately it did not play any sound :( – didayo Apr 14 '17 at 12:07
  • For instance, I am trying to make my own app with the similar structure as this app SpeakEasy Russian https://appsto.re/gb/yJ0QA.i and dont know how to pass audio to the cells of the second table viewcontroller – didayo Apr 14 '17 at 12:25
  • i updated my answer please check it will work . please comment if u got any issues. – NAVEEN KUMAR Apr 14 '17 at 12:44