1 -setupApplicationAudio//声音文件及播放器的准备 2 选择播放器 3 - (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection//更新播放器列表
复制代码
1 - (void) setupApplicationAudio { 2 3 // Gets the file system path to the sound to play. 4 NSString *soundFilePath = [[NSBundle mainBundle]pathForResource: @"sound" ofType:@"caf" ]; 6 // Converts the sound's file path to an NSURL object 7 NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 8 self.soundFileURL = newURL; 9 [newURL release]; 10 // Registers this class as the delegate of the audio session. 11 [[AVAudioSession sharedInstance] setDelegate: self]; 12 13 // The AmbientSound category allows application audio to mix with Media Player 14 // audio. The category also indicates that application audio should stop playing 15 // if the Ring/Siilent switch is set to "silent" or the screen locks. 16 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; 17 // Registers the audio route change listener callback function 18 AudioSessionAddPropertyListener ( 19 kAudioSessionProperty_AudioRouteChange, 20 audioRouteChangeListenerCallback, 21 self 22 ); 23 // Activates the audio session. 24 25 NSError *activationError = nil; 26 [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 27 // Instantiates the AVAudioPlayer object, initializing it with the sound 28 AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 29 self.appSoundPlayer = newPlayer; 30 [newPlayer release]; 31 32 // "Preparing to play" attaches to the audio hardware and ensures that playback 33 // starts quickly when the user taps Play 34 [appSoundPlayer prepareToPlay]; 35 [appSoundPlayer setVolume: 1.0]; 36 [appSoundPlayer setDelegate: self]; 37 }
1 //选择播放器 2 if ([self useiPodPlayer]) { 3 4 [self setMusicPlayer: [MPMusicPlayerController iPodMusicPlayer]]; 5 6 if ([musicPlayer nowPlayingItem])
{ 7 8 navigationBar.topItem.leftBarButtonItem.enabled = YES; 9 10 // Update the UI to reflect the now-playing item. 11 [self handle_NowPlayingItemChanged: nil]; 12 13 if ([musicPlayer playbackState] == MPMusicPlaybackStatePaused)
{ 14 navigationBar.topItem.leftBarButtonItem = playBarButton; 15 } 16 } 17 18 }
else { 19 20 [self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]]; 21 22 // By default, an application music player takes on the shuffle and repeat modes 23 // of the built-in iPod app. Here they are both turned off. 24 [musicPlayer setShuffleMode: MPMusicShuffleModeOff]; 25 [musicPlayer setRepeatMode: MPMusicRepeatModeNone]; 26 }
1 - (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection { 2 // Configure the music player, but only if the user chose at least one song to play 3 if (mediaItemCollection) { 4 // If there's no playback queue yet... 5 if (userMediaItemCollection == nil) { 6 7 // apply the new media item collection as a playback queue for the music player 8 [self setUserMediaItemCollection: mediaItemCollection]; 9 [musicPlayer setQueueWithItemCollection: userMediaItemCollection]; 10 [self setPlayedMusicOnce: YES]; 11 [musicPlayer play]; 12 // Obtain the music player's state so it can then be 13 // restored after updating the playback queue. 14 } else { 15 // Take note of whether or not the music player is playing. If it is 16 // it needs to be started again at the end of this method. 17 BOOL wasPlaying = NO; 18 if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) { 19 wasPlaying = YES; 20 } 21 22 // Save the now-playing item and its current playback time. 23 MPMediaItem *nowPlayingItem = musicPlayer.nowPlayingItem; 24 NSTimeInterval currentPlaybackTime = musicPlayer.currentPlaybackTime; 25 // Combine the previously-existing media item collection with the new one 26 NSMutableArray *combinedMediaItems = [[userMediaItemCollection items] mutableCopy]; 27 NSArray *newMediaItems = [mediaItemCollection items]; 28 [combinedMediaItems addObjectsFromArray: newMediaItems]; 29 30 [self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) combinedMediaItems]]; 31 [combinedMediaItems release]; 32 // Apply the new media item collection as a playback queue for the music player. 33 [musicPlayer setQueueWithItemCollection: userMediaItemCollection]; 34 35 // Restore the now-playing item and its current playback time. 36 musicPlayer.nowPlayingItem = nowPlayingItem; 37 musicPlayer.currentPlaybackTime = currentPlaybackTime; 38 39 // If the music player was playing, get it playing again. 40 if (wasPlaying) { 41 [musicPlayer play]; 42 } 43 } 44 // Finally, because the music player now has a playback queue, ensure that 45 // the music play/pause button in the Navigation bar is enabled. 46 navigationBar.topItem.leftBarButtonItem.enabled = YES; 47 [addOrShowMusicButton setTitle: NSLocalizedString (@"Show Music", @"Alternate title for 'Add Music' button, after user has chosen some music") 48 forState: UIControlStateNormal]; 49 } 50 }