iOS 下能支持歌曲和声音播放的的类有几个:
AVFoundtion库中的AVAudioPlayer #重要
MediMPMusicPlayerController
常用音频控件
3. MPMediaPickerController 本地音乐库选择器
5. MPVolumeView 播放进度条
这里有一个PPT在解释几种概念:
https://ccrma.stanford.edu/~jsanchez/NSSpain.pdf
这教程中同时用不同机制播放例子:
https://github.com/jsanchezsierra/AudioLab
如果想要程序中输出声音,波形,频谱以及其它特效,
一定要看一下这一篇教程:
http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
它是种用AVAudioPlayer 的averagePowerForChannel 这样接口来输出波形文件。
MPMusicPlayerController没有发现支持这一功能
另外Apple官方给出一个输出例子:aurioTouch 录音数据的波形,其中带普通波形文件,以及经过FFT运算得到频谱数据。可以参考。
源码在此:https://developer.apple.com/library/prerelease/ios/samplecode/aurioTouch/Introduction/Intro.html
以及更新版(苹果已经移走这个版本)
https://github.com/caseytcaprice/aurioTouch2
根据
https://github.com/irtemed88/PitchDetector
画得更加完美的波形文件:
https://github.com/irtemed88/PitchDetector
Apple官方给的例子,显示录音实时波开:
https://developer.apple.com/library/ios/samplecode/SpeakHere/Introduction/Intro.html
更简单的声音转波形的例子
https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html
音乐App的歌曲来源有三种,一个是本地沙盒自带歌曲,另一个网络歌曲,
第三个就系统音乐库带的歌曲,
它可以由 MPMediaPickerController 类来调用
- (IBAction)addPressed:(id)sender {
MPMediaType mediaType = MPMediaTypeMusic;
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:mediaType];
picker.delegate = self;
[picker setAllowsPickingMultipleItems:YES];
picker.prompt = NSLocalizedString(@"Select items to play", @"Select items to play");
[self presentViewController:picker animated:YES completion:nil];
}
它通过MPMediaPickerControllerDelegate接口返回一个
MPMediaItemCollection 选择的歌曲列列表,只需对MPMediaPickerController调用如下接口即可进行播放,以及上一首,下一首
[self.player setQueueWithItemCollection:self.collection];
如果是AVAudioPlayer来播放需要做得更多一点。即可把MPMediaItemCollection里歌曲URL取出来,直接传给AVAudioPlayer即可,这个URL格式类似于
ipod-library://item/item.m4a?id=1529654720874100371
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
// Play the item using AVPlayer
self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.avAudioPlayer play];
}
MMMediaPlayer是自动支持,播放下一首,上一首可用如下方法:
上一首:
- (IBAction)rewindPressed:(id)sender {
if ([self.player indexOfNowPlayingItem] == 0) {
[self.player skipToBeginning];
} else {
[self.player endSeeking];
[self.player skipToPreviousItem];
}
}
下一首:
- (IBAction)fastForwardPressed:(id)sender {
NSUInteger nowPlayingIndex = [self.player indexOfNowPlayingItem];
[self.player endSeeking];
[self.player skipToNextItem];
if ([self.player nowPlayingItem] == nil) {
if ([self.collection count] > nowPlayingIndex+1) {
// added more songs while playing
[self.player setQueueWithItemCollection:self.collection];
MPMediaItem *item = [[self.collection items] objectAtIndex:nowPlayingIndex+1];
[self.player setNowPlayingItem:item];
[self.player play];
}
else {
// no more songs
[self.player stop];
NSMutableArray *items = [NSMutableArray arrayWithArray:[self.toolbar items]];
[items replaceObjectAtIndex:3 withObject:self.play];
[self.toolbar setItems:items];
}
}
}
暂停和恢复播放:
- (IBAction)playPausePressed:(id)sender {
[self.pause setTintColor:[UIColor blackColor]];
MPMusicPlaybackState playbackState = [self.player playbackState];
NSMutableArray *items = [NSMutableArray arrayWithArray:[self.toolbar items]];
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
[self.player play];
[items replaceObjectAtIndex:3 withObject:self.pause];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[self.player pause];
[items replaceObjectAtIndex:3 withObject:self.play];
}
[self.toolbar setItems:items animated:NO];
}
AVAudioPlayer 有播放结束的调用,因此在的上一首播完,重设一下一首歌即可
如果需要后台播放音乐,需要在应有的info.plist声明
否则会被系统强行干掉。
AVAudioPlayer 不直接支持网络音频播放,可以先下载数据到一个NSData ,然后进行播放。
NSData *mydata=[[NSDataalloc]initWithContentsOfURL:[NSURLURLWithString:command]];
AVAudioPlayer *player=[[AVAudioPlayeralloc]initWithData:mydata error:nil];
[player prepareToPlay];
[player play];
但这种对于流媒体就无能为例了。因此可以使用更新的AVPlayer,它能直接播放(但是底层接口较少)
AVPlayer * _player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"]];
AudioPlayer 可以接收 线控及蓝牙耳机的按键控制,
前题的要真的有一首的歌曲播放时,才能捕获按键。
这个可以在AppDelegate 中remoteControlReceivedWithEvent来捕获各种按键。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
NSLog(@"remoteControlReceivedWithEvent %d",event.subtype);
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self postNotificationWithName:remoteControlPlayButtonTapped];
break;
case UIEventSubtypeRemoteControlPause:
[self postNotificationWithName:remoteControlPauseButtonTapped];
break;
case UIEventSubtypeRemoteControlStop:
[self postNotificationWithName:remoteControlStopButtonTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self postNotificationWithName:remoteControlForwardButtonTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self postNotificationWithName:remoteControlBackwardButtonTapped];
break;
default:
[self postNotificationWithName:remoteControlOtherButtonTapped];
break;
}
}
理论上,可以在后台不断播放一段音频(音量设为0)来实现在应用中捕获耳机按键。比如蓝点工坊就用这个方法把蓝牙耳机充当自拍器用。
但是这种用法是过不了App Store 的审核 ,所以只能用Ad hoc发行方法。
具体参考:
https://github.com/MosheBerman/ios-audio-remote-control
它是在后台不断播放一个网络电台节目来达到目的,实际蓝点工坊测试测放App自带一个声音文件,如caf效果是一样的。
文章浏览阅读1.1k次,点赞22次,收藏23次。本文通过在SSD移动硬盘中安装win10和macos双系统,实现操作系统随身携带小慢哥的原创文章,欢迎转载目录 目标 准备工作 Step1. 清空分区,转换为GPT Step2. 安装win10 Step3. 压缩win10分区容量 Step4. 创建2个分区 Step5. 将bootcamp驱动放置到exFAT分区中 Step6. 将macos分区..._mac移动硬盘装双机系统
文章浏览阅读14次。theme: cyanosishighlight: a11y-dark前言自从上次TransmittableThreadLocal框架作者评论我之后,我重新去看了下源码,终于在这个周天,我才把TransmittableThreadLocal解决线程池变量丢失的问题搞明白,而且发现我之前的认识有问题,久久孩子我之前是觉得,InheritableThreadLocal解决父子线...
文章浏览阅读366次。 距离上一篇《Exchange 2016部署实施案例篇-02.活动目录部署篇》博文更新已经过去快一周了,最近一直在忙项目上的事情和软考,整的真心有点身心俱疲啊,最近看了下上一篇博文不知道为什么访问量一直上不去,真心有点心寒啊。希望大家能多多提出宝贵意见,看看如何能让访问量上去。 废话就不多说了,开始今天的话题,Exchange的部署篇,我原定计划是把部署篇分上、下2个篇幅来写的,但最近发现好..._解决exchange2016部署先决条件
文章浏览阅读130次。原文:REACTIVE APPS WITH MODEL-VIEW-INTENT - PART4 - INDEPENDENT UI COMPONENTS作者:Hannes Dorfmann译者:却把清梅嗅这篇博客中,我们将针对如何 如何构建独立组件 进行探讨,我将阐述为什么在我看来 父子关系会导致坏味道的代码,以及为何这种关系是没有意义的。有这样一个问题时不时涌现在我的脑海中—— MVI...
文章浏览阅读662次。https://blog.csdn.net/qq_32466233/article/details/81075288_池化层后特征图尺寸
文章浏览阅读3.3k次。一、问题说明我是用的是官方示例中的这个饼状图。结果在应用到项目中后发现利用axios请求到的数据无法渲染到页面中去。并且其中value值已经改变。二、解决办法用$set改变value的值,并且重新绘制一遍表格。$set是全局 Vue.set 的别名。$set用法:向响应式对象中添加一个属性,并确保这个新属性同样是响应式的,且触发视图更新。它必须用于向响应式对象上添加新属性,因为..._vue echart初始化渲染过后无法重新渲染
文章浏览阅读3.7k次。今天在用Dev-C++ 的时候遇到一个错误“to_string is not a member of std” error解决方法:设置编译语言为ISO C++11 在菜单栏的Tool -> Compiler Option_devc++ [error] 'to_string' is not a member of 'std
文章浏览阅读1.1k次。Python 非常易学,强大的编程语言。Python 包括高效高级的数据结构,提供简单且高效的面向对象编程。Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑器(IDE)。这些 Python 开发工具帮助开发者加快使用 Python 开发的速度,提高效率。高效的代码编辑器或者 IDE 应该会提供插件,工具等能帮助开发者高效开发的特性。这篇文章收集了一些对开发者非常有_pydea兼容的
文章浏览阅读287次。一、makestrans()格式: str.maketrans(intab,outtab);功能:用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。注:两个字符串的长度必须相同,为一一对应的关系。注:Python3.6中已经没有string.maketrans()了,取而代之的是内建函数:bytearray...._python maketrance
文章浏览阅读5.7k次,点赞9次,收藏14次。set集合的简介,它的特点和遍历方式。介绍了HashSet重复元素存储底层原理,LinkedHashSet,TreeSet排序方法,SortedSet获取集合值的方法_set集合
文章浏览阅读3.6k次,点赞3次,收藏29次。随着城市规模的不断扩大和现代化程度的日益提高,城市排水管网越来越复杂,一些城市相继发生大雨内涝、管线泄漏爆炸、路面塌陷等事件,严重影响了人民群众生命财产安全和城市运行秩序。因此,摸清排水管网设施资产家底、建立排水管网地理信息系统,用现代化的技术手段对排水系统进行科学管理显得迫在眉睫。以时空信息为基础,充分利用感知监测网、物联网、云计算、移动互联网、工业控制和水力模型等新一代信息技术,全方位感..._污水处理智慧管理系统案列
文章浏览阅读5.7k次,点赞4次,收藏13次。上篇在详解FAT32文件系统中介绍了FAT32文件系统存储数据的原理,这篇就来介绍下NTFS文件系统。NTFS、用过Windows系统的人都知道,它是一个很强大的文件系统,支持的功能很多,存储的原理也很复杂。目前绝大多数Windows用户都是使用NTFS文件系统,它主要以安全性和稳定性而闻名,下面是它的一些主要特点。安全性高:NTFS支持基于文件或目录的ACL,并且支持加密文件系统(E_ntfs文件系统中,磁盘上的所有数据包括源文件都是以什么的形式存储