No audio from AVAudio Player

Discussion in 'iOS Programming' started by rukasu01, Jul 9, 2012.

  1. rukasu01, Jul 9, 2012
    Last edited: Jul 9, 2012

    rukasu01 macrumors newbie

    Joined:
    Jul 8, 2012
    #1
    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
    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.
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    No Brony's allowed here..
    That aside ;)
    you can try setting an actual error, also, is the mute button on of your device? AVAudioPlayer won't play if the mute button of the device is on.
    ANd for the love of coders.. Please use CAPITALS when making a class, I want to put my eyes in a blender when I see non-capitalized classes :)

    EDIT:
    ALso, you can just use self.soundArray, you created the synthesized for something :)
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Have you checked if path is non-nil and your if-block is being run?
     

Share This Page