PDA

View Full Version : AVAudioPlayer - how to show stop-button?




Danneman101
Mar 7, 2009, 09:54 AM
How do you display controls for the AVAudioPlayer. I just want the stop-button at the bottom of the screen, but my code displays no controls at all. Ive been googling like a maniac for this, but nothing.


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



dejo
Mar 7, 2009, 12:34 PM
I believe you need to provide your own controls and then have them call AVAudioPlayer's playback control methods. This is all described in more detail in the AVAudioPlayer Class Reference.

ghayenga
Mar 7, 2009, 12:49 PM
How do you display controls for the AVAudioPlayer. I just want the stop-button at the bottom of the screen, but my code displays no controls at all. Ive been googling like a maniac for this, but nothing.


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


Add a button to your view and and an IBAction to your viewController, connect them, and then call [theAudio stop] in your action method.

Danneman101
Mar 9, 2009, 03:41 AM
Ok, thanks. Theyll probably add an interface in some comming version, I guess.

dejo
Mar 9, 2009, 10:43 AM
Theyll probably add an interface in some comming version, I guess.
Doubtful. It's meant more as an audio engine than a UI interface. Hence, the lack of the word 'Controller' in the class name. If they want to add an interface component, they will probably add a new AVAudioPlayerController class to the API.

ethana
Mar 9, 2009, 11:55 AM
Super easy:


In .h
- (IBAction)stopMusic;

In .m
- (IBAction)stopMusic {
[theAudio stop];
}


Connect the button to the stopMusic action method in Interface Builder.

Ethan