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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-23-2009, 04:56 PM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default Using AVAudio to stop playing a sound file.

I am having trouble stopping a sound file from playing with a second touch of the same button. I am using the AVAudioPlayer frame work. I know that you must use the -(void)stop method. I am just not sure where to put that. Any assistance would be great.
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 08:02 PM   #2 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Quote:
Originally Posted by uncharteddev View Post
I am having trouble stopping a sound file from playing with a second touch of the same button. I am using the AVAudioPlayer frame work. I know that you must use the -(void)stop method. I am just not sure where to put that. Any assistance would be great.
Is this something like what you're trying to accomplish?

Code:
- (void) buttonPressed:(UIButton *)button{
	if ([myPlayer isPlaying]) {
		[myPlayer stop];
	}
	else {
	   [myPlayer play];
	}
}
deansx is offline   Reply With Quote
Old 05-23-2009, 08:09 PM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default THANKS!

Thank you for the help. So this is right?

NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
{theAudio play];

(void) buttonPressedUIButton *)button{
if ([myPlayer isPlaying]) {
[myPlayer stop];
}
else {
[myPlayer play];
}







Quote:
Originally Posted by deansx View Post
Is this something like what you're trying to accomplish?

Code:
- 
}
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 08:34 PM   #4 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Quote:
Originally Posted by uncharteddev View Post
Thank you for the help. So this is right?

NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
{theAudio play];

(void) buttonPressedUIButton *)button{
if ([myPlayer isPlaying]) {
[myPlayer stop];
}
else {
[myPlayer play];
}
I do my loading slightly differently, but it looks like the way that you're doing it should work. There's a syntax error on your first play. It should be:
Code:
[theAudio play];
You have a curly bracket instead of a square bracket at the beginning.

You'll have to define the method to be called when a button is pressed, in my example, the method is:

Code:
-(void) buttonPressed:(UIButton *)button
You'll also need to store the value for theAudio somewhere so that it's available to the buttonPressed method across different calls to the method. I was using myPlayer as an example of an instance variable in a viewController class that handles touches. Your application may well be quite different, but you'll need to store theAudio somewhere so that it's available to the buttonPressed method, assuming that theAudio is available, the buttonPressed method should be:

Code:
-(void) buttonPressed:(UIButton *)button{
	if ([theAudio isPlaying]) {
		[theAudio stop];
	}
	else {
	   [theAudio play];
	}
}
Note that I'm also assuming that you're loading the sound somewhere other than in the button pressed method.
deansx is offline   Reply With Quote
Old 05-23-2009, 08:49 PM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default Im slamming my head against the table!!!

Im guessing I dont have to tell you that I am a fresh noob. Sorry Im not getting this quickly. Here is my .m file and my .h file. What am I doing wrong. Thank you very much for the help.


#import "MainView.h"
#import <AVFoundation/AVAudioPlayer.h>

@implementation MainView
- (IBAction)myAction1id)sender {

NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
[theAudio play];
-(void) buttonPressed: (UIButton *)myAction1{
if ([theAudio isPlaying]) {
[theAudio stop];
}
else {
[theAudio play];
}
}




THE .h file


#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView :UIView {

}
- (IBAction)myAction1id)sender;
-(void)buttonPressed;
@end


































Quote:
Originally Posted by deansx View Post
I do my loading slightly differently, but it looks like the way that you're doing it should work. There's a syntax error on your first play. It should be:
Code:
[theAudio play];
You have a curly bracket instead of a square bracket at the beginning.

You'll have to define the method to be called when a button is pressed, in my example, the method is:

Code:
-(void) buttonPressed:(UIButton *)button
You'll also need to store the value for theAudio somewhere so that it's available to the buttonPressed method across different calls to the method. I was using myPlayer as an example of an instance variable in a viewController class that handles touches. Your application may well be quite different, but you'll need to store theAudio somewhere so that it's available to the buttonPressed method, assuming that theAudio is available, the buttonPressed method should be:

Code:
-(void) buttonPressed:(UIButton *)button{
	if ([theAudio isPlaying]) {
		[theAudio stop];
	}
	else {
	   [theAudio play];
	}
}
Note that I'm also assuming that you're loading the sound somewhere other than in the button pressed method.
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 09:00 PM   #6 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

How are you defining the button? Are you using Interface Builder, or are you defining it with code, or...?
deansx is offline   Reply With Quote
Old 05-23-2009, 09:20 PM   #7 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default IB

I am using Interface Builder.










Quote:
Originally Posted by deansx View Post
How are you defining the button? Are you using Interface Builder, or are you defining it with code, or...?
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 09:49 PM   #8 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Ahh, then you've already got myAction hooked up to a button in your view? If so, I'd do the code for more like this (Note that I'm not compiling this, so I might have some syntax errors, sorry...):

THE .h file


Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface MainView :UIView {
    // the player
    AVAudioPlayer* theAudio;
}
- (IBAction)myAction:(id)sender;

@end
The source file:

Code:
#import "MainView.h"


@implementation MainView
- (IBAction)myAction:(id)sender {
   if (theAudio == nil) {
      // Set up the player.
      NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
      theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
      theAudio.delegate = self;
      [theAudio play];
   }
   else if ([theAudio isPlaying]) {
      // there's no point in stopping it, if it's not playing...
      [theAudio stop];
   }
   /* Uncomment this if you want the button to start the sound again...
   else {
      [theAudio play];
   }
   */
}	
@end
... at least I hope that this is close. To what you want. I didn't realize that you were using myAction as the target for the button. Also, note that this board seems to be automatically replacing colon open paren with a frowny face...

Last edited by deansx; 05-23-2009 at 10:25 PM.
deansx is offline   Reply With Quote
Old 05-23-2009, 11:03 PM   #9 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default Almost.....

Im getting a couple of errors here. That i cant quite figure out. Error: expected specifier-qualifier-list before [ token.

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

@interface MainView :UIView {
// the player
AVAudioPlayer* theAudio;
}
- (IBAction)myActionid)sender;

@end



And here it's saying that I havent declared theAudio. (First use in this Function. I had some other errors but I got those out of the way. These are the only two I couldnt figure out.

@implementation MainView
- (IBAction)myAction1id)sender {
if (theAudio == nil) {











Quote:
Originally Posted by deansx View Post
Ahh, then you've already got myAction hooked up to a button in your view? If so, I'd do the code for more like this (Note that I'm not compiling this, so I might have some syntax errors, sorry...):

THE .h file


Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface MainView :UIView {
    // the player
    AVAudioPlayer* theAudio;
}
- (IBAction)myAction:(id)sender;

@end
The source file:

Code:
#import "MainView.h"


@implementation MainView
- (IBAction)myAction:(id)sender {
   if (theAudio == nil) {
      // Set up the player.
      NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
      theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
      theAudio.delegate = self;
      [theAudio play];
   }
   else if ([theAudio isPlaying]) {
      // there's no point in stopping it, if it's not playing...
      [theAudio stop];
   }
   /* Uncomment this if you want the button to start the sound again...
   else {
      [theAudio play];
   }
   */
}	
@end
... at least I hope that this is close. To what you want. I didn't realize that you were using myAction as the target for the button. Also, note that this board seems to be automatically replacing colon open paren with a frowny face...
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 11:23 PM   #10 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Quote:
Originally Posted by uncharteddev View Post
Im getting a couple of errors here. That i cant quite figure out. Error: expected specifier-qualifier-list before [ token.
This one is because those nasty conversions to smiley faces bit us. The code should be:

Code:
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
The smiley faces messed up fileURLWithPathath
deansx is offline   Reply With Quote
Old 05-23-2009, 11:34 PM   #11 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default Still getting errors.

This is the implementation file. I am still getting these errors. I wrote the errors in the code exactly where and how they are appearing in xcode. Im guessing they are just little syntax errors.


#import "MainView.h"
EXPECTED SPECIFIER-QUALIFIER BEFORE AVAUDIOPLAYER
#import <AVFoundation/AVAudioPlayer.h>

@implementation MainView
- (IBAction)myAction1id)sender {



NSString *path = [[NSBundle mainBundle] pathForResource:@"HF" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
[theAudio play];

}
else if ([theAudio isPlaying]) {
EXPECTED IDENTIFIER BEFORE ( OR ELSE
[theAudio stop];

}
else {
EXPECTED IDENTIFIER BEFORE ( OR ELSE
([theAudio play]);


}

}
@end













































Quote:
Originally Posted by deansx View Post
This one is because those nasty conversions to smiley faces bit us. The code should be:

Code:
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
The smiley faces messed up fileURLWithPathath
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 11:40 PM   #12 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

From the code snippets, it's not obvious to me why theAudio isn't defined. Usually, when I see this it's because of either a typo somewhere in the header file, or else a typo in the name. I don't see anything obvious. It's probably unrelated, but I noticed that you appear to be wanting to use the <AVAudioPlayerDelegate>
protocol, but you're not adopting it in the class header.
deansx is offline   Reply With Quote
Old 05-23-2009, 11:51 PM   #13 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default

So if I deleted the AvAudio framework and readded it to the project could that work? I have no idea why those syntax errors are showing up. I'm even more lost now.
uncharteddev is offline   Reply With Quote
Old 05-23-2009, 11:56 PM   #14 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Did you notice that I changed the spot for

Code:
#import <AVFoundation/AVAudioPlayer.h>
from the source file to the .h file?

This allowed me to declare theAudio in the header. I noticed that, in the fragment of source you copied above, you were still importing the file in the source.
deansx is offline   Reply With Quote
Old 05-23-2009, 11:59 PM   #15 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

I don't think that deleting the framework would help. The first thing that I'd check is making sure that the AVAudioPlayer.h is properly imported in your header.

If it would help, I can send you some source files that I know will work. I'm a bit hesitant to post them on the board, but I'd be happy to e-mail them to you. You could use them as a starting point.
deansx is offline   Reply With Quote
Old 05-24-2009, 12:02 AM   #16 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default AVAUDIO

So if I put the framework in the .h file then that error will go away. Everything should still work the same right?
uncharteddev is offline   Reply With Quote
Old 05-24-2009, 12:08 AM   #17 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

In theory. I'll try to send you a private message with a .h and a .m file that I know work fine.
deansx is offline   Reply With Quote
Old 05-24-2009, 12:10 AM   #18 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default

That would be awesome. You can send them to elchris75150@yahoo.com
thanks again.
uncharteddev is offline   Reply With Quote
Old 05-24-2009, 12:10 AM   #19 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Nope, can't send you a private message.
deansx is offline   Reply With Quote
Old 05-24-2009, 12:18 AM   #20 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default OOOPS

Yea I just realized I posted my email address for everyone to see. Oh well. So I have heard that its a real pain to get anything into the app store. As far as building a distribution version of your app. Is that true?
uncharteddev is offline   Reply With Quote
Old 05-24-2009, 12:19 AM   #21 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Just sent you an email with the files. Let me know if there were any problems...
deansx is offline   Reply With Quote
Old 05-24-2009, 12:23 AM   #22 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

So far, we haven't had any real problems, but I've also heard all of the stories, so we're pretty careful. We currently have two apps in the store, one more waiting approval and one update also waiting approval. The toughest part is the long wait, and just not knowing whether the app will be approved.
deansx is offline   Reply With Quote
Old 05-24-2009, 12:28 AM   #23 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 18
Send a message via AIM to uncharteddev
Default

I haven't received them yet. Belive it or not my app isn't far from being ready to ship. The main thing in my way is getting sounds to stop on request. That and getting a feel for instruments and fixing leaks.
uncharteddev is offline   Reply With Quote
Old 05-24-2009, 12:31 AM   #24 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Stupid slow email systems. I'll just post them here.
deansx is offline   Reply With Quote
Old 05-24-2009, 12:35 AM   #25 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Header file:
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface StopSoundViewController : UIViewController <AVAudioPlayerDelegate> {
	UIButton*            soundButton;
	AVAudioPlayer*     theAudio;

}

- (IBAction) soundButtonPressed:(UIButton *)button;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;

/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;

@end
deansx is offline   Reply With Quote
Reply

Bookmarks

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: 239
15 members and 224 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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