iPhone應用 AVAudioPlayer播放音頻講解

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.