audioPlayerDidFinishPlaying never triggers - iphone

I created a custom class to handle audio playback.
AudioPlayback.h
#import <Foundation/Foundation.h>
#import "AVFoundation/AVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayback : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
BOOL playing;
NSTimeInterval duration;
}
#property (nonatomic, assign) AVAudioPlayer *player;
#property (readonly) BOOL playing;
#property (readonly) NSTimeInterval duration;
-(NSTimeInterval)GetSoundFileDuration:(NSString *) sound_file_name;
-(void)PlaySoundFile:(NSString *) sound_file_name;
-(void)play;
-(void)stop;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
#end
Then, in AudioPlayback.h
#import "AudioPlayback.h"
#implementation AudioPlayback
#synthesize player;
-(id)init
{
self = [super init];
if (self != nil)
{
player = [[AVAudioPlayer alloc] init];
[player initWithContentsOfURL:nil error:nil];
player.delegate = self;
}
return self;
}
-(void)PlaySoundFile:(NSString *) sound_file_name
{
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:#"mp3"]];
[player initWithContentsOfURL:sound_file error:nil];
[player prepareToPlay];
[player play];
[sound_file release];
}
...
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"audioPlayerDidFinishPlaying");
}
The callback never triggers. Am I missing anything obvious?

I think "player.delegate = self" is call too early. You "initWithContentsOfURL" again in your "PlaySoundFile", which I think that cause the problem.
Try to put "player.delegate = self" before the "[player prepareToPlay]" in your "PlaySoundFile" method

Related

Play two loops for .mp3 files together

Is there any way to play 2 loop for the sound file together in iphone sdk
Thanks & Regards
shweta
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface MultiAVPlay : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer* myplayer;
NSArray* fileNames;
int ind;
}
#property (nonatomic, retain) AVAudioPlayer* myplayer;
#property (nonatomic, retain) NSArray* fileNames;
- (id)initWithFileNameQueue:(NSArray*)names;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)play:(int)i;
- (void)stop;
#end
#import "MultiAVPlay.h"
#implementation MultiAVPlay
#synthesize myplayer, fileNames;
- (id)initWithFileNameQueue:(NSArray*)files {
if ((self = [super init])) {
self.fileNames = files;
index = 0;
[self play:index];
}
return self;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (index < fileNames.count) {
[self playMp3:index];
} else {
//reached end of queue
}
}
- (void)playMp3:(int)i {
self.myplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNames objectAtIndex:i] ofType:nil]] error:nil];
[myplayer release];
myplayer.delegate = self;
[myplayer prepareToPlay];
[myplayer play];
index++;
}
- (void)stop {
if (self.myplayer.playing) [myplayer stop];
}
- (void)dealloc {
self.filenames = nil;
self.myplayer = nil;
[super dealloc];
}
#end

AVAudioPlayer not responding to stop command?

I'm currently trying to use a custom class for the AVAudioPlayer and all works great for playing a audio file but when it comes to stoping the said file it just skips over the stop command and plays another on top of the current file,
If anyone has any ideas on why this is happening I'd really appreciate the help.
Below is my AudioPlayer1.h file:
#import <Foundation/Foundation.h>
#import "AVFoundation/AVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayer1 : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
BOOL playing;
NSTimeInterval duration;
}
#property (nonatomic, assign) AVAudioPlayer *player;
#property(readonly, getter=isPlaying) BOOL playing;
#property (readonly) NSTimeInterval duration;
-(void)GetSoundFileDuration:(NSString *) sound_file_name;
-(void)play;
-(void)stop;
-(void)setVolume:(float) volume;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)PlaySoundFile:(NSString *) sound_file_name;
- (void)notPlaying;
#end
Below is the contents of AudioPlayer1.m file:
#import "AudioPlayer1.h"
#implementation AudioPlayer1
#synthesize player;
#synthesize duration;
#synthesize playing;
- (id)init
{
self = [super init];
if (self != nil)
{
player = [[AVAudioPlayer alloc] init];
[player initWithContentsOfURL:nil error:nil];
player.delegate = self;
}
return self;
}
- (void) setVolume:(float)volume
{
[player setVolume:volume];
}
- (void)PlaySoundFile:(NSString *) sound_file_name
{
[player stop];
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:#"mp3"]];
[player initWithContentsOfURL:sound_file error:nil];
[player prepareToPlay];
playing = YES;
[sound_file release];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"audioPlayerDidFinishPlaying");
playing = NO;
}
- (void) play
{
NSLog(#"Play Called");
[player setVolume:0.1];
[player play];
playing = YES;
}
- (void) stop
{
NSLog(#"Stop Called");
[player setVolume:0.0];
[player stop];
playing = NO;
}
-(void)GetSoundFileDuration:(NSString *) sound_file_name
{
NSLog(#"%#",duration);
}
-(void) notPlaying
{
playing = NO;
}
#end
And below is the code that would start the audio:
- (IBAction)WPNaritive01:(id)sender
{
AudioPlayer1 * player = [[AudioPlayer1 alloc] init ];
if (player.playing == YES)
{
[player stop];
}
if (player.playing == NO)
{
[player PlaySoundFile:#"1"];
[player setVolume:0.1];
[player play];
}
}
I apologize about the odd layout to the code as I'm still learning. I've only now found out about making these classes by chance from reading another question. lol
Ok, I've figured it out.
Rather than creating the object over and over like I was perhaps doing in the code above I should have been alloc and init the player in viewdidload rather than doing it in the IBAction like I did.
This is what I did incase anyone else comes across the same problem:
LockonLocation.h
#interface LockOnLocation : UIViewController {
AudioPlayer1 *player;
...
LockonLocation.m
- (void)viewDidLoad
player = [[AudioPlayer1 alloc] init ];
...
And I used the same code as above to play the audio so that was correct.
If anyone has any other advice for me or any other members I'd love to hear from you...

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!

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.

AVAudioPlayer - playing multiple audio files, in sequence

I want to play multiple MP3 files, in sequence (one after the other) , using AVAudioPlayer. I tried it, and it stops after playing the first MP3. However, if I go into debugger, it works fine.. any ideas? I read somewhere AVAudioPlayer plays audio in the background.. how do I prevent it from doing this?
Vas
I think the AVQueuePlayer (subclass of AVPlayer) does exactly this job (play a sequence of items) since iOS 4.1 :
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference.html
I didn't try it myself however but will definitely give a try to it.
Use one AVAudioPlayer per sound.
Well, your code example didn't work out of the box for me. Soooo, I figured I'd respond with a fixed version:
Looper.h:
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface Looper : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer* player;
NSArray* fileNameQueue;
int index;
}
#property (nonatomic, retain) AVAudioPlayer* player;
#property (nonatomic, retain) NSArray* fileNameQueue;
- (id)initWithFileNameQueue:(NSArray*)queue;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)play:(int)i;
- (void)stop;
#end
Looper.m:
#import "Looper.h"
#implementation Looper
#synthesize player, fileNameQueue;
- (id)initWithFileNameQueue:(NSArray*)queue {
if ((self = [super init])) {
self.fileNameQueue = queue;
index = 0;
[self play:index];
}
return self;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (index < fileNameQueue.count) {
[self play:index];
} else {
//reached end of queue
}
}
- (void)play:(int)i {
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNameQueue objectAtIndex:i] ofType:nil]] error:nil];
[player release];
player.delegate = self;
[player prepareToPlay];
[player play];
index++;
}
- (void)stop {
if (self.player.playing) [player stop];
}
- (void)dealloc {
self.fileNameQueue = nil;
self.player = nil;
[super dealloc];
}
#end
And here's how I would call it:
Looper * looper = [[Looper alloc] initWithFileNameQueue:[NSArray arrayWithObjects: audioFile, audioFile2, nil ]];
I only have slightly over a year of experience with iPhone/iPad development using Objective-C so feel free to respond with additional criticism.
I've implemented a class to handle this.
To use just do something like this:
[looper playAudioFiles:[NSArray arrayWithObjects:
#"add.mp3",
[NSString stringWithFormat:#"%d.mp3", numeral1.tag],
#"and.mp3",
[NSString stringWithFormat:#"%d.mp3", numeral2.tag],
nil
]];
Looper.m
#import "Looper.h"
#implementation Looper
#synthesize player, fileNameQueue;
- (id)initWithFileNameQueue:(NSArray*)queue {
if ((self = [super init])) {
self.fileNameQueue = queue;
index = 0;
[self play:index];
}
return self;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (index < fileNameQueue.count) {
[self play:index];
} else {
//reached end of queue
}
}
- (void)play:(int)i {
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNameQueue objectAtIndex:i] ofType:nil]] error:nil];
[player release];
player.delegate = self;
[player prepareToPlay];
[player play];
index++;
}
- (void)stop {
if (self.player.playing) [player stop];
}
- (void)dealloc {
self.fileNameQueue = nil;
self.player = nil;
[super dealloc];
}
#end
Looper.h
#import <Foundation/Foundation.h>
#interface Looper : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer* player;
NSArray* fileNameQueue;
int index;
}
#property (nonatomic, retain) AVAudioPlayer* player;
#property (nonatomic, retain) NSArray* fileNameQueue;
- (id)initWithFileNameQueue:(NSArray*)queue;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)play:(int)i;
- (void)stop;
#end
It is a good idea to initialize, prepare the items, and queue ahead of time, for example on the viewDidLoad method.
If you are working on Swift,
override func viewDidLoad() {
super.viewDidLoad()
let item0 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("url", withExtension: "wav")!)
let item1 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("dog", withExtension: "aifc")!)
let item2 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("GreatJob", withExtension: "wav")!)
let itemsToPlay:[AVPlayerItem] = [item0, item1, item2]
queuePlayer = AVQueuePlayer.init(items: itemsToPlay)
}
and then when an event occurs,
queuePlayer.play()
Notice that if you use the queue, you still might have some gaps between the sounds.
You can find the Objective-C version in the question How to do something when AVQueuePlayer finishes the last playeritem
Hope it helps.