I have a little problem here. I am trying to make a soundboard, and i need to use table cells in place of buttons. I would think that the following code would work, but for some reason it refuses to play the sounds. I have checked the file names vs the cell text and everything appears to be correct. Can someone please help me find where I went wide left?
ponyTest.h
ponyTest.m
EDIT 1: I noticed the table data shown formatting that didn't match the file name. I fixed that in the code above, and the problem persists. the comma in "oh, wunderbar" is also in the filename.
ponyTest.h
Code:
//
// ponyTest.h
// soundtests
//
// Created by Lion User on 09/07/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <AudioToolbox/Audiotoolbox.h>
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
NSMutableArray *soundArray;
@interface ponyTest : UITableViewController <AVAudioPlayerDelegate>
@property NSMutableArray *soundArray;
@end
ponyTest.m
Code:
//
// ponyTest.m
// soundtests
//
// Created by Lion User on 09/07/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <AudioToolbox/Audiotoolbox.h>
#import <AVFoundation/AVFoundation.h>
#import "ponyTest.h"
@interface ponyTest ()
@end
@implementation ponyTest
@synthesize soundArray = _soundArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_soundArray = [NSMutableArray array];
[_soundArray addObject:@"shneezes"];
[_soundArray addObject:@"fluttershy!"];
[_soundArray addObject:@"i go"];
[_soundArray addObject:@"oh, wunderbar"];
[_soundArray addObject:@"time to make the magics"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return [_soundArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [_soundArray objectAtIndex:indexPath.row];
NSLog(@"Current Index: %i", indexPath.row);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selectedSoundFile = [_soundArray objectAtIndex:[indexPath row]];
NSString *path = [[NSBundle mainBundle] pathForResource:selectedSoundFile ofType:@"mp3"];
if (path) {
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate:self];
[theAudio play];
}
}
@end
EDIT 1: I noticed the table data shown formatting that didn't match the file name. I fixed that in the code above, and the problem persists. the comma in "oh, wunderbar" is also in the filename.
Last edited: