iPhone應用 AVAudioPlayer播放音頻講解
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播放音頻講解的內容介紹完了,希望本文對你有所幫助!