How does my app recognize that a song finished? - iphone

I´m working on my first app. It plays an mp3 song. Everything works fine, but I need the app to recognize that the sound finished, then change the button “pause” image to the “play” one. I can already do that, but just when the button is pressed, not when song ends.
Another issue is that when the song restarts after stop->play, it doesn´t continue playing at the previous volume setting (got a slider). The player starts at maximum volume. I can change the volume slider position, but I am not able to make the app respect the set volume when restarts.
Any help will be very appreciated!
Thnx a lot.

When using "AVAudioPlayerDelegate protocol delegate", please do not forget to set delegate (for example) as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"33fr" ofType:#"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (error) {
NSLog(#"error = %#", error);
return;
}
[myPlayer setDelegate:self]; <<=== HERE!
}
I uploaded sample code here. Please check it. ->
http://yahoo.jp/box/X7Hxfk

I have not done much of Objective-C but it looks like you need to implement a delegate.
I quote: "You implement a delegate to handle interruptions (such as an incoming phone call) and to update the user interface when a sound has finished playing."
I am just reading the AVAudioPlayer Class Reference here:
https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008067

you can use the AVAudioPlayerDelegate protocol delegate
After the audio player is finished playing the song (if it does so
successfully), the audio PlayerDidFinishPlaying:successfully: delegate
method will be called in the delegate object of the audio player. We
can implement this method like below (this method is defined in the
AVAudioPlayerDelegate protocol):
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag)
{
NSLog(#"Audio player stopped correctly.");
}
else
{ NSLog(#"Audio player did not stop correctly.");
}
if ([player isEqual:self.audioPlayer])
{ self.audioPlayer = nil;
}
else
{ /* This is not the player */ }
}
in .h file add delegate like below code
#interface yourViewController : UIViewController <AVAudioPlayerDelegate>

Related

Can not call method from another view controller in Objective C, iPhone SDK

At my StartWorkoutViewController.m I trigger:
#import "WorkoutViewController.h"
#implementation StartWorkoutViewController
- (IBAction) start
{
[firstViewController performSelector:#selector(callMyAction)];
WorkoutViewController *ViewCon = [[WorkoutViewController alloc]init];
[ViewCon callMyAction];
[ViewCon release];
}
then at my WorkOutViewController.h I added this:
- (void)callMyAction;
and at my WorkOutViewController.m I added this:
#import "StartWorkoutViewController.h"
-(void)viewWillAppear:(BOOL)animated{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/achtergrondgeluid.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
-(void)callMyAction{
NSLog(#"suckers");
[audioPlayer pause];
}
Now my NSLog (#"suckers") triggers very well from the StarWorkoutViewController, but for some reason the audioPlayer does nothing, any idea? Also setting labels in the Workout ViewController from the callMyAction in this file. Don't work! Any idea? Thanks!
The reason nothing is happening is because your are calling the pause method before the player even starts playing. viewWillAppear does not get called until it is visually on the screen. It actually doesn't always get called even then but that is another issue entirely. So if you want to pause the player it needs to actually be playing. If you put that viewWillAppear code in viewDidLoad it will probably work as expected.
Currently your code is doing this:
Load WorkoutViewController
Pause Audio
WorkourViewController become visible (actually this may never happen as I am not seeing it being added to any windows or anything).
Plays audio.

Audio stream with AVPlayer

There's a lot of streaming apps out there for iOS. They all use a player, which I assume is the AVPlayer. Yet it seems impossible to find a decent documentation with sample code that works ! I'm pretty sure it's nothing but a few line of codes, but I just can't figure out what's wrong...
I have an EXC_BAD_ACCESS error when trying to call the "play" method. But the url is good and there is an instance of the player.
- (void)viewDidLoad {
[super viewDidLoad];
// Load the array with the sample file
NSString *urlAddress = #"http://mystreamadress.mp3";
//Create a URL object.
urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
[urlAddress release];
}
The urlStream is a property with retain attribute. Then I have an IBAction that fires when the button is clicked and that tries to play it, and that's where it crashes.
- (IBAction)playButtonPressed
{
[player play];
}
Can my problem be because I'm trying to play MP3 or what ? The real url adress I'm using works fine when I use a webview to load it.
If anyone could point me to a good sample (not the AVFoundation ou AVPlayer docs from Apple nor the AVTouchController project) it would be realy appreciated.
Thanks !
urlAddress release is causing the issue I think.
You didn't create your NSString with alloc, init so by releasing it you are overreleasing it and getting the EXC_BAD_ACCESS.
Unless you explicitly create an NSString with alloc and init then the convenience methods of creating the string are autoreleased.
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:fileURL];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
//AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
[avPlayer play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
I use it this way to play mp3, but it doesn't support stop.

Iphone audio only working in Simulator NOT in Device

I have set up part of my app to play a sound. Simple - but only is working in Simulator.
Initing audio session this way:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
AudioSessionInitialize(NULL,NULL,NULL,NULL);
AudioSessionSetActive(YES);
return self;
}
Playing sound, setting completion calback, and checking for file this way:
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesAddSystemSoundCompletion (soundID,NULL,NULL,
completionCallback,
(void*) self);
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(#"Playing Sound" );
AudioServicesPlaySystemSound(soundID);
}
else {
NSLog(#"NO SOUND");
}
In Simulator, the file is found, sound plays, and completion happens. In device, file is found, sound does not play, and completion is never called.
Any ideas? Thank you very much, both my forehead and my desk appreciate any replies...:)
OK,
So there were a few things I changed to get this working. DOn't have the code in front of me, will try to add later.
I stopped using the system sound and started using AVAudioPlayer instead.
I didn't have my UIView set up as the audio delegate
Was not starting the audio session. Now am starting the audio session in the initNib function(I saw tutrorials that said to use awakeFromNib, but the awakeFromNib never fired...)

didSelectRowAtIndexPath - access tapCount or similar

I'd like to control an instance of AVAudioPlayer from a UITableView item in a didSelectRowAtIndexPath instance.
First touch of the row item triggers 'play' of the AVAudioPlayer. Second touch of the row item triggers 'stop' of the AVAudioPlayer.
I can make the 'play' work but can't get the 'stop' to work. Also, subsequent touches of the row item starts another thread of the audio in the background.
What's the best way to ensure 1 tap starts the audio and a 2nd tap stops it?
Code samples - this method preps audio file and AVAudioPlayer for use:
- (void)makeReadyAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Murderers" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[musicPlayer prepareToPlay];
}
This block will start & stop the AVAudioPlayer within a case statement in the didSelectRowAtIndexPath section:
case 7: {
//touch to start audio sound playing, touch again to stop playing
[self makeReadyAudio];
if ([musicPlayer isPlaying]) {
[musicPlayer stop];
NSLog(#"musicPlayer tested to be playing, so stop it.");
} else {
[musicPlayer play];
NSLog(#"musicPlayer tested to be *not* playing, so play it.");
}
break;
}
How about:
- (void) didSelectRowAtIndexPath:
{
if (player.isPlaying) {
[player stop];
} else {
[player start];
}
}
In other words, keep the player around and see what it is doing.
The problem is that you call makeReadyAudio every single time the row is tapped. You need to check if you have already done that. For example by doing something like:
if (musicPlayer == nil) {
[self makeReadyAudio];
}

Can you see anything wrong in this code?

NSString *path = [[NSBundle mainBundle] pathForResource:[arraySub objectAtIndex:indexPathHere.row] ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.player = myPlayer;
[player prepareToPlay];
[player setDelegate:self];
[self.player play];
NSTimeInterval lenghtMusic = player.duration;
if (player.currentTime == lenghtMusic) {
NSLog(#"It worked");
[tableThing deselectRowAtIndexPath:indexPathHere animated:YES];
[myPlayer autorelease];
[file autorelease];
}
Can you see anything wrong?
Apparently, the "if"-statement never gets called...
If I read your code correctly you are trying to find out when the player has finished playing and then you want to deselect the current rode and release the player. Your problem is that you start the player and then immediately checks where it is currently playing. Unless you have an almost 0 length file the currentTime and length will NEVER be equal.
If that is what you are trying to do you should use the AVAudioPlayerDelegate Protocol and especially the:
– audioPlayerDidFinishPlaying:successfully:
method that is called when the player is finished.
So you need to change your class that controls the player by editing the .h file to use the AVAudioPlayerDelegate Protocol. Obviously keep extending whatever you were before.
#interface YourClass <AVAudioPlayerDelegate>
#end
In your .m file:
Then when you have created and assigned your player instance:
[player setDelegate:self];
In the .m file also add the method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
// Your success code goes here
}
You are comparing double's with ==, which is almost always false due to round-off errors.
Compare with a range e.g.
if (fabs(player.currentTime - lenghtMusic) < 0.0001) {
...
}
I'm a noob iPhone dev, but are you sure that player.currentTime is a NSTimeInterval? That looks like the most obvious candidate.
Also, I'd log out what your player.currentTime is and what lenghtMusic is before your if and that'll probably let you know what's going on.