iPhone应用 AVAudioPlayer播放音频讲解

1.本文摘自:http://mobile.51cto.com/iphone-278789.htm

iPhone应用 AVAudioPlayer播放音频讲解是本文要介绍的内容,iPhone是媒体大师,其内建的iPod功能可轻松的处理音频和视频,下面我将对AVAudioPlayer这个音频播放类详细的介绍。使用AVAudioPlayer可以实现载入、播放、暂停、停止音频,监控平均和峰值音量水平.

AVAudioPlayer处理音频中断

当用户在音频回放期间受到电话时,音频会消失,出现这种情况时AVAudioPlayer委托接受audioPlayerBeginInterruption:回调,音频会话暂时无效,并且暂停播放器。

如果用户接听电话,那么应用程序中止,而应用程序委托接受一个applicationWillResignActive:回调。当通话结束,应用程序重新启动(利用applicationDidBecomeActive:回调)。如果用户拒绝接听电话那么将向委托发送audioPlayerBeginInterruption:回调。可以从此方法回复回放。

例子:

  1. #import <UIKit/UIKit.h> 
  2. #import <AVFoundation/AVFoundation.h> 
  3.  
  4. #define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]  
  5. #define BARBUTTON(TITLE, SELECTOR)  [[[UIBarButtonItem alloc] initWithTitle:TITLE style:
  6. UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]  
  7. #define SYSBARBUTTON(ITEM, TARGET, SELECTOR) [[[UIBarButtonItem alloc] 
  8. initWithBarButtonSystemItem:ITEM target:TARGET action:SELECTOR] autorelease]  
  9.  
  10. @interface TestBedViewController : UIViewController <AVAudioPlayerDelegate> 
  11. {  
  12.  AVAudioPlayer *player;  
  13. }  
  14. @property (retain) AVAudioPlayer *player;  
  15. @end  
  16.  
  17. @implementation TestBedViewController  
  18. @synthesize player;  
  19.  
  20. - (BOOL) prepAudio  
  21. {  
  22.  NSError *error;  
  23.  NSString *path = [[NSBundle mainBundle] pathForResource:@"MeetMeInSt.Louis1904" ofType:@"mp3"];  
  24.  if (![[NSFileManager defaultManager] fileExistsAtPath:path]) return NO;  
  25.    
  26.  // Initialize the player  
  27.  self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];  
  28.  selfself.player.delegate = self;  
  29.  if (!self.player)  
  30.  {  
  31.   NSLog(@"Error: %@", [error localizedDescription]);  
  32.   return NO;  
  33.  }  
  34.    
  35.  [self.player prepareToPlay];  
  36.  
  37.  return YES;  
  38. }  
  39.  
  40. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag  
  41. {  
  42.  // just keep playing  
  43.  [self.player play];  
  44. }  
  45.  
  46. - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player  
  47. {  
  48.  // perform any interruption handling here  
  49.  printf("Interruption Detected\n");  
  50.  [[NSUserDefaults standardUserDefaults] setFloat:[self.player currentTime] forKey:@"Interruption"];  
  51. }  
  52.  
  53. - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player  
  54. {  
  55.  // resume playback at the end of the interruption  
  56.  printf("Interruption ended\n");  
  57.  [self.player play];  
  58.    
  59.  // remove the interruption key. it won't be needed  
  60.  [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];  
  61. }  
  62.  
  63. - (void) viewDidLoad  
  64. {  
  65.  self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;  
  66.  [self prepAudio];  
  67.  
  68.  // Check for previous interruption  
  69.  if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Interruption"])  
  70.  {   
  71.   self.player.currentTime = [[NSUserDefaults standardUserDefaults] floatForKey:@"Interruption"];  
  72.   [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];  
  73.  }  
  74.    
  75.  // Start playback  
  76.  [self.player play];  
  77. }  
  78.  
  79. @end  
  80.  
  81. @interface TestBedAppDelegate : NSObject <UIApplicationDelegate> 
  82. @end  
  83.  
  84. @implementation TestBedAppDelegate  
  85. - (void)applicationDidFinishLaunching:(UIApplication *)application {   
  86.  UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  87.  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]];  
  88.  [window addSubview:nav.view];  
  89.  [window makeKeyAndVisible];  
  90. }  
  91. @end  
  92.  
  93. int main(int argc, char *argv[])  
  94. {  
  95.  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  96.  int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");  
  97.  [pool release];  
  98.  return retVal;  

小结:iPhone应用 AVAudioPlayer播放音频讲解的内容介绍完了,希望本文对你有所帮助!

 

posted on 2011-12-07 08:37  wtq  阅读(587)  评论(0编辑  收藏  举报