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:
- #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;
- }
Summary:IPhoneApplicationAVAudioPlayerPlayAudioI hope this article will help you!