Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2011, 05:42 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 67
gbenna is on a distinguished road
Default play audio with button from NSString

I have an app which has a tableview. when certain cells are selected a new view controller with a xib is loaded. The xib has a UIButton which when selected plays an audio file. The audio file played depends on which cell is selected and is designated in a plist associated with the tableview. I have created an NSString properties for the audio file and I have "didSelectRowAtIndexPath" assigning these properties. Then in the view controller I have tried to set up the audio in the ViewDidLoad but for some reason it is not working. Can someone help me out?

here is some of my code

this is in my table view.

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {

const NSDictionary *const row = [self rowForIndexPath:indexPath];

NSString *wantedClassName = [row objectForKey:@"controller"];
UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
NSLog(@"controller is -%@-", wantedClassName);

if ([vc isKindOfClass:[Location_One class]])
{
Location_One* loVC = (Location_One*)vc;
loVC.myAudio = [row objectForKey:@"audio"];
}


Viewcontroller .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>


@interface Location_One : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
NSString *myAudio;
}

@property (nonatomic, retain) NSString *myAudio;
@property (nonatomic, retain) AVAudioPlayer *theAudio;
-(IBAction)pushButton;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end

viewcontroller .m file

@synthesize myAudio;
@synthesize theAudio;

-(IBAction)pushButton {
}
-(IBAction)play {
[theAudio play];
}
-(IBAction)stop {
[theAudio stop];
}
-(IBAction)pause {
[theAudio pause];
}


- (void)viewDidLoad {
[super viewDidLoad];

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:self.myAudio ofType:@"mp3"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:audioFilePath] error:nil];
theAudio.delegate =self;
[theAudio play];
}

I have assigned the button in IB.
gbenna is offline   Reply With Quote
Old 09-30-2011, 10:43 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 1
DigitalDownbeat is on a distinguished road
Default make string into NSURL

You have to convert the string to an NSURL: Like this:

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:self.myAudio ofType:@"mp3"];
if (theAudio) [theAudio release];

//checking to see if file exists.. good idea otherwise app can crash
if([[NSFileManager defaultManager] fileExistsAtPath:audioFilePath]){
NSURL *audioFile=[NSURL fileURLWithPath:audioFilePath];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFile error:nil];
theAudio.delegate =self;
[theAudio play];
}


That should make it work out for you.

Last edited by DigitalDownbeat; 09-30-2011 at 10:46 AM.
DigitalDownbeat is offline   Reply With Quote
Old 09-30-2011, 11:15 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 67
gbenna is on a distinguished road
Default

Thanks this helped a lot the only thing I changed was to change the @"mp3" to ofType:nil]; since it was already declared in the plist. Thanks again for your reply and help.
gbenna is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, nsstring, plist, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 378
9 members and 369 guests
bignoggins, epaga, hussain1982, jcdevelopments, LunarMoon, markolo, omagod, pinacate, skog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,643
Threads: 94,110
Posts: 402,860
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Diligent
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0