I’ve been playing around with the iOS SDK for a few days now, and am getting to the point where I’m starting to put the concepts and approaches I’ve learnt by writing a noddy little application which displays what the currently playing song from the music library is on my iPhone.
The problem I’ve hit is that whilst there are delegate methods on AVAudioPlayerDelegate for when a piece of audio finishes playing, I can’t seem to find any references for any notifications / events/ delegate methods that are called and can be hooked into / listened for when a piece of audio STARTS playing – i.e. when the user presses the play button, or even just the track changes.
After doing 3 whole days (!!!) of iOS dev and playing around, there is clearly going to be a way to do it, but I’m not sure what / where to find it. Aside from having a push button on my UI as I do at the moment to populate the information (which works fine, the back-end code does what it’s meant to), the only other approach I can see is to hook into the ‘track finished playing’ method on the aforementioned delegate and use that.
But that seems a bit hacky.
UPDATED
Well, I think I momentarily went full retard. This is what you get for fiddling with iOS for the first time with a Java background. I implemented your suggestion (with some syntactic tweaks) as follows:
h file:
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@protocol GeoTunesAVAudioPlayerDelegate <AVAudioPlayerDelegate>
-(void)didStartPlaying;
@end
@interface GeoTunesAVAudioPlayer : AVAudioPlayer <AVAudioPlayerDelegate>
@end
and m file:
#import "GeoTunesAVAudioPlayer.h"
@implementation GeoTunesAVAudioPlayer
- (id)init
{
self = [super init];
return self;
}
- (BOOL) play
{
if ([[self delegate] respondsToSelector:@selector(didStartPlaying)])
{
[(id)[self delegate] didStartPlaying];
}
return [super play];
}
@end
And I implement this in my Controller with:
// Set up the audio delegate
player = [[GeoTunesAVAudioPlayer alloc] init];
player.delegate = self;
But this little arrangement craps out with EXC_BAD_ACCESS, which is frustrating as I don’t understand enough about what’s going on behind the scenes here to even assess what the problem is. Any suggestions?
I don’t think it’s available! Here’s an idea though:
playand implement you delegate method and callsuperGeneral idea: (warning, syntax might be wrong!)
EDIT after comments:
I think your subclass of AVAudioPlayer (GeoTunesAVAudioPlayer) class should not conform to AVAudioPlayerDelegate as this is the delegating class. You should have the controller that contains the player object conforming to your subprotocol GeoTunesAVAudioPlayerDelegate
//.h
and m file:
and in your controller: