博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
addMusic 和playMusic(AVAudioPlayer)
阅读量:7069 次
发布时间:2019-06-28

本文共 6907 字,大约阅读时间需要 23 分钟。

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 }

转载于:https://www.cnblogs.com/pengyingh/articles/2340980.html

你可能感兴趣的文章
awesome-nlp
查看>>
第 102 章 Ntop
查看>>
利用路由器连接别人家的网络
查看>>
超越存储 遇见未来 HP 3PAR StoreServ存储系列更新
查看>>
工信部:中国物联网产业规模已超9300亿元
查看>>
能源革命为太阳能市场打开了多个缺口?
查看>>
拟态防御或助网络安全实现再平衡
查看>>
理解Spark的核心RDD
查看>>
如何安装pandom : 一个针对Linux的真随机数生成器
查看>>
用模板实现 seqlist
查看>>
Mac go delve debug
查看>>
人人都能开发物联网
查看>>
Android开发学习笔记(14):OnTouch、OnClick、OnLongClick、OnFocusChange调用关系
查看>>
1--信息系统监理师知识框架图-(IS2-160523p)
查看>>
Nginx实现负载均衡
查看>>
基于Linux系统部署Web服务器
查看>>
CentOS6.3安装配置iscsi并实现自动挂载
查看>>
在Kubernetes中运行Spark集群
查看>>
VMware Horzion Workspace POC文档--安装2(集成VIEW)
查看>>
浅谈HDFS的读流程
查看>>