ios AVAudioPlayer ( Audio Play )
iOS 2013. 2. 18. 10:46http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/
http://blog.naver.com/myungjik?Redirect=Log&logNo=20142627935
AVAudioPlayerDelegate Protocol Reference
Conforms to |
|
Framework |
/System/Library/Frameworks/AVFoundation.framework |
Availability |
Available in iOS 2.2 and later. |
Declared in |
AVAudioPlayer.h |
Related sample code |
Overview
The delegate of an AVAudioPlayer object must adopt the AVAudioPlayerDelegate protocol. All of the methods in this protocol are optional. They allow a delegate to respond to audio interruptions and audio decoding errors, and to the completion of a sound’s playback.
Tasks
Responding to Sound Playback Completion
- – audioPlayerDidFinishPlaying:successfully:
Responding to an Audio Decoding Error
- – audioPlayerDecodeErrorDidOccur:error:
Handling Audio Interruptions
- – audioPlayerBeginInterruption:
- – audioPlayerEndInterruption:withOptions:
- – audioPlayerEndInterruption: Deprecated in iOS 6.0
- – audioPlayerEndInterruption:withFlags: Deprecated in iOS 6.0
Instance Methods
audioPlayerBeginInterruption:
Called when an audio player is interrupted, such as by an incoming phone call.
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
Parameters
player
The audio player that has been interrupted.
Discussion
Upon interruption, your application’s audio session is deactivated and the audio player pauses. You cannot use the audio player again until you receive a notification that the interruption has ended.
Availability
- Available in iOS 2.2 and later.
See Also
Declared In
AVAudioPlayer.h
audioPlayerDecodeErrorDidOccur:error:
Called when an audio player encounters a decoding error during playback.
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
Parameters
player
The audio player that encountered the decoding error.
error
The decoding error.
Availability
- Available in iOS 2.2 and later.
Declared In
AVAudioPlayer.h
audioPlayerDidFinishPlaying:successfully:
Called when a sound has finished playing.
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
Parameters
player
The audio player that finished playing.
flag
YES on successful completion of playback; NO if playback stopped because the system could not decode the audio data.
Discussion
This method is not called upon an audio interruption. Rather, an audio player is paused upon interruption—the sound has not finished playing.
Availability
- Available in iOS 2.2 and later.
Declared In
AVAudioPlayer.h
audioPlayerEndInterruption:withOptions:
Called after your audio session interruption ends, with options indicating the state of the audio session.
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
Parameters
player
The audio player whose interruption has ended.
flags
Options indicating the state of the audio session when this method is called. Options are described in AVAudioSessionInterruptionOptions.
Discussion
When an interruption ends, such as by a user ignoring an incoming phone call, the audio session for your application is automatically reactivated; at that point you can again interact with the audio player. To resume playback, call the play method.
If this delegate method receives the AVAudioSessionInterruptionOptionShouldResume constant in its options parameter, the audio session is immediately ready to be used.
Availability
- Available in iOS 6.0 and later.
Declared In
AVAudioPlayer.h
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate>
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"[Piano] James Morrison-You Give Me Something"
ofType: @"mp3"];
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
[fileUrl release];
[soundFilePath release];
[newPlayer setNumberOfLoops:0];
[newPlayer play];
// NSError *error;
// AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
// NSLog(@"%@", audioPlayer);
return YES;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player release];
}
'iOS' 카테고리의 다른 글
release is unavailable not available in automatic reference counting mode (0) | 2015.07.10 |
---|---|
Could not find Developer Disk Image - Xcode 7 - iOS 8.4 (0) | 2015.07.08 |
mac shell commands (0) | 2012.03.09 |
Cocos2D 기초 강좌 (0) | 2012.03.07 |
Cocos2D example (0) | 2012.03.07 |