Help stopping audio using AVAudioPlayer - iphone

Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file in .caf format. What I'm trying to do is have a play and pause button. Here is a sample of a simple IBAction to play the audio from a button.
- (IBAction) clear {
NSString *path = [[NSBundle mainBundle] pathForResource:#"clear" ofType:#"caf"];
AVAudioPlayer* myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
myAudio.delegate = self;
myAudio.volume = 1.0;
myAudio.numberOfLoops = 0;
[myAudio play];
}
So when I go ahead and create another IBAction with the method...
- (IBAction) stopaudio {
[myAudio stop];
audioPlayer.currentTime = 0;
}
I compile and xcode tells me that myAudio in the stopaudio IBAction is not defined. How do I fix it so I'm able to access myAudio in another IBAction?
My Second question is that I'm using NavigationController to setup a TableView, when I make a selection it takes me to a UIView with a back button to go back to the TableView. What I'm trying to do is to kill any audio that I have playing in the UIView when I go back to the TableView. The code above is part of the code that I'm going to have in the UIView.
Also one quick question. Lets say I have an audio file that is 1 minute long. Clicking play again will overlap a new instance of that audio before the old instance is done playing. How can I check to see if myAudio is playing and not allow it to be played again until myAudio is finished playing?
Thank You!

First of all, you should read the documentation. It will remove almost all of your questions. Nevertheless, to have access to your instance of AVAudioPlayer from all methods of your class, you should define it in your .h file. To stop your player on returning to the previous screen, call [myAudio stop] in your viewWillDisappear:(BOOL)animated method. About your last question. If you define myAudio in your .h file, you will be able to check if it is playing now. smth like
- (IBAction) clear {
if(![myAudio isPlaying]){
[myAudio play];
}
}

Related

AudioServicesPlaySystemSound sound not audible when called from IBAction method

I'm having a problem with AudioServicesPlaySystemSound in my iOS app.
I have a method defined to play a system sound. When I call the method from viewDidLoad, I can hear the sound play, but when I call it from a button handler, I do not hear the sound play.
Here's the code from my view controller:
SystemSoundID startSound = 0;
-(void)playStartSound
{
AudioServicesPlaySystemSound(startSound);
}
-(void)viewDidLoad
{
NSURL *urlStart = [[NSBundle mainBundle] URLForResource:#"Beep" withExtension:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)urlStart, &startSound);
[self playStartSound];
[super viewDidLoad];
}
- (IBAction)start:(id)sender
{
[self playStartSound];
}
The IBAction method is associated with a button. When I select the button, the playStartSound method gets called, but the sound is not audible.
The system sound is not disposed of with AudioServicesDisposeSystemSoundID until the dealloc method is called.
I've tried calling AudioServicesCreateSystemSoundID from within playStartSound, but it didn't make any difference.
The app also uses AV Foundation, Media Player, Core Media, Core Video, and Core Location. I'm wondering if one of these might be interfering with AudioToolbox?
UPDATE: I just put together a simple app that just contains the above code and a button and the sound plays just fine when I select the button.
So then I removed all the video capture code from my controller and now the audio plays fine.
This was not working because I had an active video capture session in my application.
AudioServicesPlaySystemSound will not play when there is an active video capture session.
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"7777" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath: soundPath]), &soundID);
AudioServicesPlaySystemSound (soundID);
Hey try like that it will play

AVAudioPlayer - Muting button

I have searched for this answer and have not found it.
I have music that plays in the background when my iPhone app is launched. But I want a button so that users can mute the music. There are sound effects within the app also so sliding the mute button on the side of the device won't cut it.
This is the current code I have for the AVAudioPlayer.
- (void)viewDidLoad{
#if TARGET_IPHONE_SIMULATOR
//here code for use when execute in simulator
#else
//in real iphone
NSString *path = [[NSBundle mainBundle] pathForResource:#"FUNKYMUSIC" ofType:#"mp3"];
AVAudioPlayer *TheAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
TheAudio.delegate = self;
[TheAudio play];
TheAudio.numberOfLoops = -1;
#endif
}
Can anyone help me out the code needed for a simple button to simply stop the music and start it again.
Thanks in advanced.
Put this code in the viewcontroller.h file:
-(IBAction) btnStop:(id)sender;
Put this code in the viewcontroller.m file:
-(IBAction) btnStop:(id)sender {
[TheAudio stop];
//Whatever else you want to do when the audio is stopped
}
In the Interface builder, connect a button to this action, so when it is clicked this action will be called.
That should make the music stop.
Its easier to display code in an answer:
-(IBAction) playerPlay:(id)sender {
if([player isPlaying]) {
[player stop];
}
if(![player isPlaying]) {
[player play];
}
}
I'll explain:
The [player isPlaying] method checks to see if the audio is playing. If the audio is playing, everything in the brackets is executed (in this situation, the audio stops playing).
Because of the "!" in ![player isPlaying], the method is made the opposite of what it usually is. This means that if the player is NOT playing, everything in the brackets is executed (in this situation, the audio starts playing).
All of this is enclosed in the IBAction, so that it is executed when the button is clicked.
For future reference, the correct format for an If statement in Objective-C is:
if(thing to check for) {
things that happen if the thing that is check for is correct;
}
The word "then" is never actually used, but it is the same thing and whatever is in the brackets.
Hope this helps!

iphone, how to refresh the gui?

i'm coding a small app for the iphone (just for fun)
what i want:
if i press the "update" button:
send something to the server
parse the answer of the server
update the content of some labels on the screen
show the answer
play a system sound //using the audio toolbox
sleep some secods, just enough to finish the previos system sound call
do other stuff
end
actually, everything works but... for some reason, the label content is updated at the end of the method / callback / function that is called when pressed the "update" button.
what am i doing wrong?
thanks!
some code:
-(void) playASound: (NSString *) file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//play the file
AudioServicesPlaySystemSound(soundID);
}
- (IBAction)update: (id) sender{
BOOL error=[self sendContent];
if ( error == NO ){
result.text=[self parseContent];
[self playSound:#"ready"];
sleep(4);
....
}
// here is updated the gui
}
The GUI is displayed by the runloop. The loop will not reach its next iteration until your method is done executing. Therefore, your method blocks the view from updating. Either use a background thread or a timer.
You don't want to use sleep there. Sleep stops the whole process from continuing that's why your label gets updated only at the end of the function. I'd recommend either using NSTimer or performSelector:withObject:afterDelay: (see here).
This is because you have blocked the main thread when you sleep. UI can only be drawn on the main thread.
A solution is to use [NSTimer scheduledTimerWithTimeInterval:X_SEC target:self selector:#selector(doOtherStuff) userInfo:nil repeats:NO] in place of sleep and do other stuff.
What about performing the [self sendContent] call in a separate thread?

i cannot see movie playing in iphone simulator

i have created a application that plays movie on button click..my application is running without any warning or error. But i am not able to see the movie don't know why?
I have added the Media player framework and also imported the #import in viewController.h
my button action code is as follow...
-(IBAction)playMyMovie:(id)sender{
NSBundle *bundle =[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"jumps" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}
Please tell me what i am missing..
in iPhone simulator you could not play movie properly.
test the applcation on iPhone (Real instrument).
It will work fine.
You are creating a MPMoviePlayerController. It has a view property - that is its view. It is your job to put that view into your interface. If you don't, you won't see anything.
try add this line before the [theMovie play]
[self.view addSubview:theMovie.view];
and also remember to setFullScreen:
[self.theMovie setFullscreen:YES];
if you dont want fullscreen but want to embed into the view, you can ignore the "setFullscreen", but set the frame for the MPMoviePlayerController view:
[self.theMovie.view setFrame:CGRectMake("add in the size you want here")];

AVAudioPlayer sound on iPhone stops looping unexpectedly: How to debug?

I am implementing a sound effect that plays while a user is dragging a UISlider.
In a previous question, I used AudioServicesPlaySystemSound() but that type of sound can't be halted. I need the sound to halt when the user is not actively dragging the slider but has not released it.
Now I am creating a sound using an AVAudioPlayer object. I initialize it in my View Controller like this:
#interface AudioLatencyViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player1;
}
#implementation AudioLatencyViewController
#property (nonatomic, retain) AVAudioPlayer *player1;
#synthesize player1;
-(void)viewDidLoad {
[super viewDidLoad];
// the sound file is 1 second long
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"tone1" ofType:#"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.player1 = newPlayer;;
[newPlayer release];
[self.player1 prepareToPlay];
[self.player1 setDelegate:self];
}
I have two methods to control the sound. The first one plays the sound whenever the slider is being moved. It is connected to the UISlider's Value Changed event in Interface Builder.
-(IBAction)sliderChanged:(id)sender;
{
// only play if previous sound is done. helps with stuttering
if (![self.player1 isPlaying]) {
[self.player1 play];
}
}
The second method halts the sound when the user releases the slider button. It's connected to the UISlider's Touch Up Inside event.
-(IBAction)haltSound;
{
[self.player1 stop];
}
This works when the slider button is moved then released quickly, but if you hold down the button longer than two or three seconds, the sound repeats (good) then breaks up (bad). From then on, the sound won't play again (super bad).
I have tried setting the sound to loop using:
[self.player1 setNumberOfLoops:30];
...but that leads to the app freezing (plus locking up the Simulator).
What can I do to debug this problem?
When a user is holding the UISlider, Value Changed gets called MANY MANY times (any time the user moves just a slight amount). I would use Touch Down instead.
You might want to still try using the loop for this route...
I like the idea of playing a sound while the user is moving the slider though. Let me know how it works out.
deggstand#gmail.com