[翻译] AFSoundManager
AFSoundManager
iOS audio playing (both local and streaming) and recording made easy through a complete and block-driven Objective-C class. AFSoundManager uses AudioToolbox and AVFoundation frameworks to serve the audio. You can pick a local file or you can use a URL to stream the audio, the choice is up to you.
这是一个用block方式编写的简单易用的iOS音频播放(支持本地与流媒体方式)以及录制的类。AFSoundManager使用了AudioToolbox、AVFoundation框架来处理音频。你可以使用一个本地的文件,或者是一个网络上的URL流媒体音频,怎么用取决于你呢。
Installation
AFSoundManager is available on CocoaPods so you can get it by adding this line to your Podfile:
AFSoundManager支持CocoaPods,你可以添加以下一句话到Podfile中来使用:
pod 'AFSoundManager'
If you don't use CocoaPods, you will have to import these files into your project:
如果你不想使用CocoaPods,你可以导入以下几个头文件即可:
AFSoundManager.h
AFSoundManager.m
AFAudioRouter.h
AFAudioRouter.m
Also, you need to import the AudioToolbox
framework and te AudioFoundation
framework.
当然,你需要引入AudioToolbox与AudioFoundation框架。
Usage
First of all, make sure that you have imported the main class into the class where you are going to play audio.
首先,确保在你要使用的地方引入了以下头文件:
#import "AFSoundManager.h"
Then, you only need to call one method to start playing your audio.
然后,你只需要调用一个方法就可以播放音频了。
Local playing
If you need to play a local file, call -startPlayingLocalFileWithName:atPath:withCompletionBlock:
. If you want to use the default path, just set it as nil
.
如果你想播放以下本地的文件,调用-startPlayingLocalFileWithName:atPath:withCompletionBlock:方法,如果你想使用默认的路径,只需要将path设置成nil即可。
Example:
例如:
[[AFSoundManager sharedManager] startPlayingLocalFileWithName:@"filename.mp3" atPath:nil withCompletionBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error) {
if (!error)
//This block will be fired when the audio progress increases in 1%
} else {
//Handle the error
}
}];
Audio streaming
For remote audio, call -startStreamingRemoteAudioFromURL:andBlock:
如果要播放流媒体,调用-startStreamingRemoteAudioFromURL:andBlock:
Example:
例子:
[[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:@"http://www.example.com/audio/file.mp3" andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error) {
if (!error)
//This block will be fired when the audio progress increases in 1%
} else {
//Handle the error
}
}];
Control
If you need to pause, resume or stop the current playing, guess what, there's a method for that!
如果你想要暂停,恢复或者停止当前播放的音频,猜猜怎么弄?用以下方法即可!
[[AFSoundManager sharedManager]pause];
[[AFSoundManager sharedManager]resume];
[[AFSoundManager sharedManager]stop];
[[AFSoundManager sharedManager]restart];
For going back or forward, you have to specify the second where to continue playing the audio by calling -moveToSecond:
如果要快进或者倒退,你需要指定在什么地方来继续播放,调用方法-moveToSecond:
For example, if you need to move the audio to the second 288, call
例如,你需要将音频快进到288s,调用
[[AFSoundManager sharedManager]moveToSecond:288];
If you are using a UISlider, for example, and you need to work with values between 0.000000 and 1.000000, don't you worry, we got it:
如果你想用一个UISlider,其取值范围是介于0.00000与1.00000之间,不用急,你可以这么用:
[[AFSoundManager sharedManager]moveToSection:0.345680]; //That will move the audio to the 34.568% of its total progress
You can also change the speed rate of the playing.
你也可以修改播放的速度。
[[AFSoundManager sharedManager]changeSpeedToRate:2.0];
The normal rate would be 1.0, while the half-speed playback would be 0.5 and the double speed playback 2.0
默认的播放速度是1.0,你也可以让播放速度变快(设置成2.0)或者是播放速度变慢(设置成0.5)
In order to change the volume, call -changeVolumeToValue:
by passing a decimal number between 0.000000 (mute) and 1.000000 (maximum volume). Example:
修改音量的话,调用-changeVolumeToValue:,你传入的值介于0.00000-1.00000即可:
[[AFSoundManager sharedManager] changeVolumeToValue:0.750000]; //This will put the volume at 75%
Playing status
In order to get noticed of the playing status changes, you need to implement theAFSoundManagerDelegate by adding it to your class, just like other delegates.
为了获取到播放状态的变更,你需要实现theAFSoundManagerDelegate代理,就像使用其他代理方法一样。
Then, just implement the currentPlayingStatusChanged:
method in the class you want to get notified about the status changes in.
然后,你只需要实现以下的方法currentPlayingStatusChanged:,在这个方法里面,你可以获取到状态值的变化。
-(void)currentPlayingStatusChanged:(AFSoundManagerStatus)status {
switch (status) {
case AFSoundManagerStatusFinished:
//Playing got finished
break;
case AFSoundManagerStatusPaused:
//Playing was paused
break;
case AFSoundManagerStatusPlaying:
//Playing got started or resumed
break;
case AFSoundManagerStatusRestarted:
//Playing got restarted
break;
case AFSoundManagerStatusStopped:
//Playing got stopped
break;
}
}
Handle the change in each case.
你可以在每种状态中获取到事件。
Background playing
If you want to enable background playing, make sure you have Background Modes enabled on your project, under the Capabilities section:
如果你想要支持后台播放,确保你已经开启了后台播放,请在Capabilities这个地方进行设置:
Also, add this information to your info.plist file:
然后,在你的info.plist文件中进行对应的设置:
Output manage
AFSoundManager also lets you choose which device do you want to use to play the audio. I mean, even if you have your headphones plugged in, you can force the audio to play on the built-in speakers or play it through the headphones.
AFSoundManager支持让你选择用什么设备来播放音频,我的意思是,即使你插入了耳机,你也可以强制用你的喇叭来播放音频:
If the headphones (or any external speaker) are plugged in and you want to play it on the built-in speakers, call:
如果耳机插入了(或者是其他外放设备),这时候你还是想用内置的喇叭,调用:
[[AFSoundManager sharedManager] forceOutputToBuiltInSpeakers];
If you want to play it through the default device (in this case the headphones or the external speaker) call
如果你想使用默认的设备(这种情况下的意思是外部设备),调用:
[[AFSoundManager sharedManager] forceOutputToDefaultDevice];
And if you want to check if the headphones, or a external speaker, are currently plugged in on the device, check it with -areHeadphonesConnected
. Example:
如果你想检测一下这个当前外部设备时候已经插入进来,用这个方法来检测:
if ([[AFSoundManager sharedManager] areHeadphonesConnected]) {
//Headphones connected
} else {
//Headphones NOT connected
}
Recording audio
Start recording audio from the device's microphone is easy peasy!
从设备录音是非常简单的呢!
[[AFSoundManager sharedManager] startRecordingAudioWithFileName:@"recording" andExtension:@"mp3" shouldStopAtSecond:25];
If you don't want recording to stop automatically, set shouldStopAtSecond as 0 or nil.
如果你不想录音的时候自动停止,将shouldStopAtSecond设置成0或者nil。
Control the recording
AFSoundManager let's you perform several actions with your current recording:
AFSoundManager给你提供了几种方法来控制当前的音频录制:
AFSoundManager
[[AFSoundManager sharedManager] pauseRecording]; // Pauses the current recording
[[AFSoundManager sharedManager] resumeRecording]; // Resumes the current recording (if it's paused)
[[AFSoundManager sharedManager] stopAndSaveRecording]; // Stops the current recording and closes the file
[[AFSoundManager sharedManager] deleteRecording]; // Delete the current recording (perform this before stoping it)
Lastly, to get the current recording duration, call -timeRecorded
which will return a NSTimeInterval object.
最后,如果要获取当前的录音的时间,调用-timeRecorded这个方法,他会给你返回NSTimeInterval的对象。
License
AFSoundManager is under MIT license so feel free to use it!
AFSoundManager支持MIT协议,所以,你可以免费的使用!
Author
Made by Alvaro Franco. If you have any question, feel free to drop me a line at alvarofrancoayala@gmail.com
作者是Alvaro Franco。有任何问题, 给我发邮件( alvarofrancoayala@gmail.com )吧。
[翻译] AFSoundManager的更多相关文章
- 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?
0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
随机推荐
- springboot 常用插件
热部署 使用run as -java application, 把spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,然后把IDEA的run参数里VM参数设 ...
- 数据序列化导读(1)[JSON]
所谓数据序列化(Data Serialization), 就是将某个对象的状态信息转换为可以存储或传输的形式的过程. 那么,为什么要进行序列化? 首先,为了方便数据存储: 其次,为了方便数据传递. 在 ...
- Linux 文件流管理
1. 打开/关闭文件 1). 打开文件 / fopen 作用: 打开一个文件,将其与文件流联系起来,方便后续的操作 头文件: #include <stdio.h> 函数原型: FILE * ...
- [转]ORA-28001: the password has expired解决方法
本文转自:http://blog.csdn.net/btt2013/article/details/54862420 参考文献:http://www.zhetao.com/content259 后台报 ...
- 用PDMReader工具生成数据库设计文档(转载)
来源:http://blog.csdn.net/xinglun88/article/details/19987719 第一步:下载并安装PDMReader,资源网站: http://www.pdmre ...
- web前端开发教程系列-3 - 前端开发调试工具分享
前言 一. Bug出现的原因 1. 主观原因或者是出现这么多浏览器的原因 2. 另外客观原因: 二. 调试工具 1. Firebug 2. Chrome 3. IE11 4. IETester 5. ...
- javascript 单双引号(转载)
来源:http://blog.csdn.net/irxiang/article/details/50164945 最近在使用javascript编程,遇到使用字符串的情况 写了一点测试代码 var s ...
- element ui 时间控件 多个日期
前言:工作中用到 vue+element ui 的前端框架,需要使用时间控件来选择多个日期,已月日的形式,且有默认值,所以记录一下.转载请注明出处:https://www.cnblogs.com/yu ...
- git fork后提交pull request到原作者,别人的pull request我们要怎样merge融合
首先要记住,pull request 不是随便提交的,这是建立在你对原作者的项目有fork,并对项目中的代码有修改,并提交到了你的GitHub上,才能进行下面的操作. 若不知怎样fork项目,请看我的 ...
- Mybatis中的jdbcType的作用
使用MyBatis框架做更新操作时,在该字段需要更新的内容为空时,就会出现1111错误,也就是无效的列类型,这个时候你就要使用jdbcType.至于什么时候要使用到javaType我还没遇到过,而且我 ...