iPhone应用 AVAudioPlayer播放音频讲解是本文要介绍的内容,iPhone是媒体大师,其内建的iPod功能可轻松的处理音频和视频,下面我将对AVAudioPlayer这个音频播放类详细的介绍。使用AVAudioPlayer可以实现载入、播放、暂停、停止音频,监控平均和峰值音量水平.
AVAudioPlayer处理音频中断
当用户在音频回放期间受到电话时,音频会消失,出现这种情况时AVAudioPlayer委托接受audioPlayerBeginInterruption:回调,音频会话暂时无效,并且暂停播放器。
如果用户接听电话,那么应用程序中止,而应用程序委托接受一个applicationWillResignActive:回调。当通话结束,应用程序重新启动(利用applicationDidBecomeActive:回调)。如果用户拒绝接听电话那么将向委托发送audioPlayerBeginInterruption:回调。可以从此方法回复回放。
例子:
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
- #define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
- #define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:
- UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
- #define SYSBARBUTTON(ITEM, TARGET, SELECTOR) [[[UIBarButtonItem alloc]
- initWithBarButtonSystemItem:ITEM target:TARGET action:SELECTOR] autorelease]
- @interface TestBedViewController : UIViewController <AVAudioPlayerDelegate>
- {
- AVAudioPlayer *player;
- }
- @property (retain) AVAudioPlayer *player;
- @end
- @implementation TestBedViewController
- @synthesize player;
- - (BOOL) prepAudio
- {
- NSError *error;
- NSString *path = [[NSBundle mainBundle] pathForResource:@"MeetMeInSt.Louis1904" ofType:@"mp3"];
- if (![[NSFileManager defaultManager] fileExistsAtPath:path]) return NO;
- // Initialize the player
- self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
- selfself.player.delegate = self;
- if (!self.player)
- {
- NSLog(@"Error: %@", [error localizedDescription]);
- return NO;
- }
- [self.player prepareToPlay];
- return YES;
- }
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
- {
- // just keep playing
- [self.player play];
- }
- - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
- {
- // perform any interruption handling here
- printf("Interruption Detected\n");
- [[NSUserDefaults standardUserDefaults] setFloat:[self.player currentTime] forKey:@"Interruption"];
- }
- - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
- {
- // resume playback at the end of the interruption
- printf("Interruption ended\n");
- [self.player play];
- // remove the interruption key. it won't be needed
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];
- }
- - (void) viewDidLoad
- {
- self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
- [self prepAudio];
- // Check for previous interruption
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Interruption"])
- {
- self.player.currentTime = [[NSUserDefaults standardUserDefaults] floatForKey:@"Interruption"];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];
- }
- // Start playback
- [self.player play];
- }
- @end
- @interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
- @end
- @implementation TestBedAppDelegate
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]];
- [window addSubview:nav.view];
- [window makeKeyAndVisible];
- }
- @end
- int main(int argc, char *argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
- [pool release];
- return retVal;
- }
小结:iPhone应用 AVAudioPlayer播放音频讲解的内容介绍完了,希望本文对你有所帮助!