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 07-23-2009, 12:49 PM   #1 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
Unhappy AVAudioPlayer HELP!

Hey all,

I am using AVAudioPlayer in my project to play different songs, about 3 or 4 minutes longs, but when I play one song and then play a different song they merge. How can I play one song, and then when I play a different song the first one would stop. I'm using a button for each song to play.
helios is offline   Reply With Quote
Old 07-23-2009, 01:39 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Just call [currentPlayer stop] before starting the new player - where currentPlayer is a pointer to the player that is playing now.

I suspect you're creating an AVAudioplayer, starting it, and then not keeping a pointer arround. You should probably have "currentPlayer" as an instance variable and property for your class. If you do that, then this will work:

Code:
//on button click

[currentPlayer stop];
self.currentPlayer = //create a new player here.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-23-2009, 01:44 PM   #3 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
Default

sorry but im a complete noob.. can you show me how to do the coding? at the moment i have this for each button:

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"m4a"];
AVAudioPlayer* theAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio1.delegate = self;
[theAudio1 play];
helios is offline   Reply With Quote
Old 07-23-2009, 03:56 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

For this to work, You need to variable "theAudio1" to be available anywhere in your class- that means you need to declare it in the .h file as an "instance variable."

Code:
//put this between the interface brackets in your .h file
AVAudioPlayer *theAudio1;
Code:
//put this stuff inside your button method
NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"m4a"];

[theAudio1 stop];
[theAudio1 release]; // this prevents us from leaking memory

theAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];

theAudio1.delegate = self;
[theAudio1 play];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-23-2009, 04:42 PM   #5 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
Default

woooh!!!! THANKS!!! little type you made there though..! [NSURL fileURLWithPathath] is what your supposed to have. BUT THANKS ANYWAY!!!
helios is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, avaudioplayer, music, objective-c

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: 241
21 members and 220 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, FAED, fredidf, iDifferent, iph_s, JamesCahall, JasonR, mer10, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, timle8n1, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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