当前位置: 首页 > news >正文

addMusic 和playMusic(AVAudioPlayer)

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

相关文章:

  • 12:Web及MySQL服务异常监测案例
  • 一个***的自白:年赚两三百万 生活纸醉金迷(3)
  • weex 项目开发(四)项目框架搭建 及 自定义 TabBar 组件
  • 项目规划管理 - 1
  • C# DLL资源文件打包(图片、JS、CSS)[WebResource]
  • 阅读摘要
  • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
  • exchange日常管理之十五:报550错误
  • 12. ZooKeeper之Java客户端API使用—创建会话。
  • 安装office2007 1706错误
  • 36.Linux驱动调试-根据oops定位错误代码行
  • 自我激励的20种方法
  • 数据结构C++(1)线性表——数组实现(arrayList)
  • C++拷贝构造函数(深拷贝与浅拷贝)
  • 支付系统接口性能压力测试TPS优化之路
  • 【附node操作实例】redis简明入门系列—字符串类型
  • 2018一半小结一波
  • Debian下无root权限使用Python访问Oracle
  • ECMAScript6(0):ES6简明参考手册
  • git 常用命令
  • gulp 教程
  • Java应用性能调优
  • Netty 4.1 源代码学习:线程模型
  • Python进阶细节
  • Python爬虫--- 1.3 BS4库的解析器
  • SegmentFault 2015 Top Rank
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 大整数乘法-表格法
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于List、List?、ListObject的区别
  • 聊聊flink的TableFactory
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 前嗅ForeSpider中数据浏览界面介绍
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 无服务器化是企业 IT 架构的未来吗?
  •  一套莫尔斯电报听写、翻译系统
  • 一些关于Rust在2019年的思考
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​渐进式Web应用PWA的未来
  • #微信小程序:微信小程序常见的配置传值
  • (分布式缓存)Redis持久化
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (转)德国人的记事本
  • (转载)深入super,看Python如何解决钻石继承难题
  • .describe() python_Python-Win32com-Excel
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .NET 5种线程安全集合
  • .NET Core 通过 Ef Core 操作 Mysql
  • .NET MVC之AOP
  • .net中生成excel后调整宽度
  • .php文件都打不开,打不开php文件怎么办
  • .stream().map与.stream().flatMap的使用
  • @Controller和@RestController的区别?
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [3300万人的聊天室] 作为产品的上游公司该如何?