AVAudioPlayer initWithData Problem - iphone

I added a sound file in my Xcode resources folder.
I want to play the sound file by AVAudioPlayer
I can use the image by [UIImage imageNamed:#"xxxx.png"];
But I don't know how to attach the file to the AVAudioPlayer.
Thank you for helping me.

In the .h file
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface RootViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property(nonatomic, retain) AVAudioPlayer *audioPlayer;
& also synthesize this in your .m file
Now in your .m file
NSString *mp3Path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"test.mp3"];
NSURL *mp3Url = [NSURL fileURLWithPath:mp3Path];
NSError *err;
AVAudioPlayer *audPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mp3Url error:&err];
[self setAudioPlayer:audPlayer];
if(audioPlayer)
[audioPlayer play];
[audPlayer release];
Also add the follwing frameworks
AudioToolbox.framework & AVFoundation.framework
Hope it helps

Related

ios AVAudioPlayer plays sound with echo

I have two AVAudioPlayer to determine what sound has finished to do something after that. the first sound plays just fine but the second sound (twoPlayer) plays the sound with echo. any of you knows why or how this happend?
I have this on my .h file:
#interface myClass : UIViewController<AVAudioPlayerDelegate>
{
AVAudioPlayer *onePlayer;
AVAudioPlayer *twoPlayer;
}
#property (retain, nonatomic) AVAudioPlayer *onePlayer;
#property (retain, nonatomic) AVAudioPlayer *twoPlayer;
.m file:
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"m4a"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;
twoPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
twoPlayer.delegate=self;
twoPlayer.volume=0.5;
if (twoPlayer == nil)
NSLog(#"[error description]");
else
[twoPlayer play];
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (player==onePlayer) {
[onePlayer release];
}
if (player==twoPlayer) {
[numberPlayer release];
}
}
I found the error I was setting a UIButton with "forControlEvents:UIControlEventAllTouchEvents" and the actionw was been trigger more then once. I replace the UIButton with this forControlEvents:UIControlEventTouchDown and works just fine.

AVAudioPlayerDelegate method doesn't seem to work

I'm simply trying to do something when an audio file has finished playing, but it doesn't seem to work.
In my .h file I've got this:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#interface soundViewController : UIViewController
<AVAudioPlayerDelegate>
{
//various outlets
}
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
#end
And in the .m file I've got:
#import "soundViewController.h"
#interface soundViewController ()
#end
#implementation soundViewController
#synthesize audioPlayer;
And the method's:
- (void) playWord{
Word *currentWord = [[Word alloc]init];
currentWord = [testWords objectAtIndex:wordNum-1];
NSString *item = [currentWord word];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#.mp3", [[NSBundle mainBundle] resourcePath], item]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil){
NSLog(#"error in audioPlayer");
}
else{
[audioPlayer play];
NSLog(#"Playing word");
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (flag==YES){
NSLog(#"Finished!");
}
}
So... it all works swimmingly (apart from the fact that the sound file won't work if it starts with a capital letter), but why am I not seeing the log message "Finished" when the sound finishes?
Thanks for your help,
Morten
You actually need to set the delegate of the player after you instantiate it, like:
audioPlayer.delegate = self;
Hope this helps!

Audio Toolbox sound change problem

i'm new in iphone programin and i wrote a little animation program. i used audiotoolbox frame for sound and it was working.
But when i change the sound file. (deleted exfile from resources and folder and add the other file with the same name, same path)
but after changing sound is not working.
i also rechanged sound file with ex-file.But even ex file is not working.
(both are .wav type)
if you want i can write codes here.
thx for your help.
Have you tried rewriting the code?
You could also try using the AVFoundation Framework instead.
.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayerViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#end
.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}

AvAudioPlayer memory leaks?

Im doing an application that needs to play alot of short sounds (mp3-files). Im using AvAudioPlayer and the sounds are playing just fine, BUT the leaks are building up until my app crashes.
I have a seperate class for the player
AVSnd.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface AVSoundPlayer : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *msoundPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *msoundPlayer;
-(id)initWithMp3File: (NSString *)inString;
-(void) playNum:(int)num;
#end
AVSND.m
#implementation AVSoundPlayer
#synthesize msoundPlayer;
-(id)initWithMp3File: (NSString *)fileName{
if (self = [super init]){
NSBundle *mainBundle = [NSBundle mainBundle];
NSError *error;
NSURL *sURL = [NSURL fileURLWithPath:[mainBundle
pathForResource:fileName ofType:#"mp3"]];
self.msoundPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:sURL error:&error];
if (!self.msoundPlayer) {
NSLog(#"Sound player problem: %#", [error localizedDescription]);
}
}
return self;
}
-(void) playNum:(int)num{
self.msoundPlayer.numberOfLoops = num;
[self.msoundPlayer prepareToPlay];
AVAudioPlayer *tmpPlayer = self.msoundPlayer;
[tmpPlayer play];
}
- (void)dealloc {
[self.msoundPlayer release];
[super dealloc];
}
#end
Then I make an instance of this object in the views i want sound.
in .h files I add following lines:
#class AVSnd;
AVSnd *mPlayer;
#property (nonatomic, retain) AVSnd *mPlayer;
and in .m files i use:
#synthezise mPlayer;
[self.mPlayer initWithMp3File:#"soundFileName"];
[self.mPlayer playNum:1];
[self.mPlayer release];
But why do I get memory-leaks every time i play a sound? Should i implement the player in another way?
Thank you so much for any help!
I had the same problem and solved it with autorelease:
self.audioPlayer = [[[AVAudioPlayer alloc]
initWithContentsOfURL:url error:&error] autorelease];
self.msoundPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:sURL error:&error];
Here you are retaining the object twice, in self. (because of the property), and when alloc. It could be the reason.

How do you make an iPhone beep?

What code allows me to produce a standard beep sound on the iPhone?
Well it depends on what kind of sound you want.
Here's how to play a sound using the AVFoundation audio framework.
#import <UIKit/UIKit.h>
#class AVAudioPlayer;
#interface AudioPlayer : UIViewController {
IBOutlet UIButton *playButton;
IBOutlet UIButton *stopButton;
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) IBOutlet UIButton *playButton;
#property (nonatomic, retain) IBOutlet UIButton *stopButton;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
-(IBAction)play;
-(IBAction)stop;
#end
- (void)viewDidLoad {
[super viewDidLoad];
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"TNG_Theme"
ofType:#"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
// Preloads the buffer and prepares the audio for playing.
[self.audioPlayer prepareToPlay];
[filePath release];
[fileURL release];
}
-(IBAction)play {
// Make sure the audio is at the start of the stream.
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
-(IBAction)stop {
[self.audioPlayer stop];
}
AudioServicesPlaySystemSound is one thing you can do for a simple sound.