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

Reply
 
LinkBack Thread Tools Display Modes
Old 03-09-2011, 02:35 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 10
hanumandev is on a distinguished road
Default AVAudioPlayer UIButton - second touch stop current audio play.

I have a UIButton that when touched plays an audio file. At the moment touching the button twice in succession plays the audio file twice instead of stopping it.

I'm looking to have the second click stop the audio file and not play a second instance. any suggestions would be very much appreciated. thanks

-(IBAction) buttonNoisePlay {

NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathathsoundFile] error:NULL];
sound.delegate = self;
sound.numberOfLoops = -1;
sound.volume = 1;

if(sound == nil || sound.playing == NO) //if not playing
{

[sound play];
}
else
{
[sound stop];
}
}
hanumandev is offline   Reply With Quote
Old 03-09-2011, 03:00 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 139
sumosumo84 is on a distinguished road
Default

make a boolean called play, and set it false in viewdidload, or something like that.

then use this code

Code:
-(IBAction) buttonNoisePlay {

NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathathsoundFile] error:NULL];
sound.delegate = self;
sound.numberOfLoops = -1;
sound.volume = 1;	

if(play == FALSE) //if not playing
{

[sound play];
play = FALSE;
}
else
{
[sound stop];
play = TRUE;
}
}
sumosumo84 is offline   Reply With Quote
Old 03-09-2011, 09:07 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Tokyo
Posts: 163
kazbluesky is on a distinguished road
Default

I hesitate to say this because I'm very new to programming, but when I had this problem, through a lot of trial and error, I found that putting:


// This stops audio overlap ********

if (playSound) [playSound release];

//**************************************

at the end of the IBAction stops any overlap. At least it does on my little app. If I was you I would try anything that the experienced developers suggest first and if that doesn't work try this out from me. Like I said, at this stage I barely know what I'm doing (but this solution does seem to work though ).
kazbluesky is offline   Reply With Quote
Old 03-10-2011, 12:33 AM   #4 (permalink)
JaGDiSH
 
Join Date: Sep 2010
Location: Ahmedabad
Posts: 57
jagds is on a distinguished road
Send a message via Skype™ to jagds
Default

Use one BOOL variable..
While playing set BOOL variable isPlaying = YES;
And when audio is playing st BOOL variable NO,

Like Here Code..
[audio prepareToPlay];
isPlaying = YES;
if(isPlaying)
{
[audio play];
isPlaying = NO;
}
else
{
[audio stop];
isPlaying = YES;
}
Have a nice time..
jagds is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, ipad, ipad app development, iphone

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: 362
15 members and 347 guests
Absentia, akphyo, apatsufas, BinHex, dre, fredidf, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, PavelMik, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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