IPhone AVAudioPlayer audio playback

Source: Internet
Author: User

IPhoneApplicationAVAudioPlayerPlayAudioThe description is the content to be introduced in this article,IPhoneAs a media master, its built-in iPod function can easily Process audio and video.AVAudioPlayerThisAudioA detailed description of the playback class. UseAVAudioPlayerIt can load, play, pause, and stop audio, and monitor average and peak volume levels.

AVAudioPlayer handles audio interruptions

When the userAudioAudio disappears when the player receives a call during playback. In this case, AVAudioPlayer authorizes audioPlayerBeginInterruption: callback. The audio session is temporarily invalid and the player is paused.

If the user receives the call, the application is suspended, and the application delegates an applicationWillResignActive: callback. When the call ends, the application restarts using applicationDidBecomeActive: callback ). If the user refuses to answer the call, the audioPlayerBeginInterruption: callback will be sent to the delegate. This method can be used for playback.

Example:

 
 
  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;  

Summary:IPhoneApplicationAVAudioPlayerPlayAudioI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.