iPhone應(yīng)用 AVAudioPlayer播放音頻講解
iPhone應(yīng)用 AVAudioPlayer播放音頻講解是本文要介紹的內(nèi)容,iPhone是媒體大師,其內(nèi)建的iPod功能可輕松的處理音頻和視頻,下面我將對(duì)AVAudioPlayer這個(gè)音頻播放類詳細(xì)的介紹。使用AVAudioPlayer可以實(shí)現(xiàn)載入、播放、暫停、停止音頻,監(jiān)控平均和峰值音量水平.
AVAudioPlayer處理音頻中斷
當(dāng)用戶在音頻回放期間受到電話時(shí),音頻會(huì)消失,出現(xiàn)這種情況時(shí)AVAudioPlayer委托接受audioPlayerBeginInterruption:回調(diào),音頻會(huì)話暫時(shí)無效,并且暫停播放器。
如果用戶接聽電話,那么應(yīng)用程序中止,而應(yīng)用程序委托接受一個(gè)applicationWillResignActive:回調(diào)。當(dāng)通話結(jié)束,應(yīng)用程序重新啟動(dòng)(利用applicationDidBecomeActive:回調(diào))。如果用戶拒絕接聽電話那么將向委托發(fā)送audioPlayerBeginInterruption:回調(diào)。可以從此方法回復(fù)回放。
例子:
- #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;
- }
小結(jié):iPhone應(yīng)用 AVAudioPlayer播放音頻講解的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!