[翻译] 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 [email protected]

作者是Alvaro Franco。有任何问题, 给我发邮件( [email protected] )吧。

时间: 2024-10-09 21:00:05

[翻译] AFSoundManager的相关文章

在GlassFish应用服务器上创建并运行你的第一个Restful Web Service【翻译】

前言 本人一直开发Android应用,目前Android就业形势恶劣,甚至会一路下滑,因此决定学习服务器开发.采用的语言是java,IDE是Intellij,在下载Intellij的同时看到官网很多优秀的guide文章,于是按照guide成功完成了一个RESTful的demo.官方文档非常简洁,给我带来了很大的帮助,于是翻译之,希望对其他不愿意看原文的人有所帮助.由于水平有限,读者发现错误请指正,谢谢. 原文地址: https://www.jetbrains.com/help/idea/2016

Java 7 Concurrency Cookbook 翻译 序言

在日常的Java代码开发过程中,很难免地有对多线程的需求,掌握java多线程和并发的机制也是Java程序员写出更健壮和高效代码的基础.笔者找寻国内已出版的关于Java多线程和并发的的中文书籍和翻译书籍,大家一致推荐的是<Java Concurrency in Practice>,笔者暂时还没有看英文原版,笔者看的是它的翻译版<Java并发编程实战>,笔者读起来感觉并不通畅,不知道是翻译的问题还是原版本来就写得不够流畅,同时感觉知识的深度也超过了入门的需求. 笔者在机缘巧合之下,发现

[翻译] ORMLite document -- How to Use Part (二)

前言 此文档翻译于第一次学习 ORMLite 框架,如果发现当中有什么不对的地方,请指正.若翻译与原文档出现任何的不相符,请以原文档为准.原则上建议学习原英文文档. ---------------------------------------------------------------------------------------------- 二.如何使用 2.7 表的创建 ORMLite 提供了一些工具类为您存储在数据库中的类创建 table 和 schema. 2.7.1 Tabl

Reveal常用技巧(翻译来自Reveal官网blog)

翻译来自官网:http://revealapp.com/blog/reveal-common-tips-cn.html 以下基于Reveal 1.6. 用于快速上手的内置应用 刚刚下载Reveal,啥都还没配置呢,想先随便玩玩看,怎么办? 我们花了不少时间开发这个复杂程度类似与实际场景的Sample应用──Soundstagram(音频分享版的Instagram, ¯\_(ツ)_/¯),就是为了让大家能最快速地上手Reveal,尝试它的各种强大功能. 在 Help 菜单项中,点击 Inspect

qt 国际化(翻译时会触发changeEvent)

1. 修改工程文件 .pro ,加入翻译源文件 hello_world.ts: TRANSLATIONS += \        Resource/translations/hello_world.ts 写代码时需要注意下面两点:  2. 源码用 utf-8 字符集3. 源码中需要翻译的字符串必须用英文(不能有中文或其它语言),则需要用 tr()  函数.  例如: QMessageBox::information(this,tr("Information"),tr("Hel

还在吐槽翻译的外版书质量差吗?谈谈我个人的理解

很难想象哪个学习计算机技术的人是没看过这方面书籍的,如果只是在网上看看技术贴,那样得来的知识绝对是离散的,不系统的.而要真正学好一门学问(比如一门计算机语言或者一门技术),一本好书的作用是不言而喻的.很多人抱怨国人在技术图书方面抄来抄去,不求甚解,虽然出版图书者甚众,但最终成为精品者却凤毛麟角.于是,更多读者热衷于外版书.但显然,并非所有国人的外语水平都足以在阅读原版书籍时毫无障碍.那么退而求其次,寻求翻译版就成为一种看似不得已的选择. 不幸的是,网上对于翻译版书籍的吐槽可以说从未消停.我也看过

VLD 1.0 ReadMe翻译尝试

近期想学习下VLD的实现,打算从最简单的V1.0版本看起.以下是V1.0版本自己尝试翻译下,最新的2.x版本似乎强大了很多. 简介 Visual C++提供了内置的内存检测机制,但其充其量只满足了最小定位需求.VLD工具定位为内置内存泄漏的替代,提供了如下特性: 泄漏内存块的全调用栈回溯,包括文件及其行号: 泄漏内存完整转储(hex和ascii格式): 可定制的泄漏报告等级(报告的详细程度可配置) 相对于Purify和BoundsChecher工具其是免费的,而其他免费工具,往往需要入侵式代码.

如何调用有道翻译API(Java,HTTP)

申请Key 首先如图进入有道翻译,在下方点击"有道翻译API". 紧接着来调用数据接口,按提示完成下列输入框. 如下图所示,已经申请成功了. 下图是官方给的示例,可以有xml和json.jsonp等三种数据格式. 代码(Java) public class TestPost { public static void main(String[] args) { new ReadByPost().start(); } } class ReadByPost extends Thread{ @

Android studio project文件结构翻译

Android studio project文件结构翻译 个人翻译,用作备忘. 链接地址:https://developer.android.com/tools/projects/index.html#ApplicationModules Android Project Files Studio的项目文件和设置,设置的作用范围包含所有的module. 以以下的demo为例. .git:git版本控制的文件存放目录. .gradle:gradle执行一些编译所生成的目录 Idea: 由Intell