目录
一、音频
1.音频播放
2.录音
3.讯飞语音的使用二、视频
1.视频播放
2.视频开发
一、音频
1.音频播放
iOS开发中的音频播放大致分为两类:一种是短音频(通常称为音效,时长不超过30s),一种是长音频(通常称为音乐)。前者通常可以通过AudioToolbox.framework进行操作(由一系列C语言的函数构成),后者要使用AVFoundation.framework(用Objective-C封装好的一系列的类)。 播放音效(短音频)通常打包成.caf、.aif和.wav格式。下面为大家封装了一段播放音频的代码,由于音频播放的AudioToolBox底层都是C函数,所以使用了函数指针,不熟悉的可以翻翻之前C的东西
- (void)playSoundEffect:(NSString *)name withCallback:(void (*)(SystemSoundID, void *)) callback {
NSString *audioFile = [[NSBundle mainBundle] pathForResource:name ofType:nil];
NSURL *fileUrl = [NSURL fileURLWithPath:audioFile];
SystemSoundID soundID;
// 在系统中创建一个音效对象并获得其唯一ID
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
// 注册在播放完之后执行的回调函数
// 第二个和第三个参数跟循环播放有关
// 第五个参数是指向传给回调函数的数据的指针
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, callback, NULL);
// 播放音效
AudioServicesPlaySystemSound(soundID);
// 播放音效并震动
// AudioServicesPlayAlertSound(soundID);
}
播放音乐可以使用AVAudioPlayer类:
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"后会无期" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.volume = 5;
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
Demo代码如下:
![](https://img.haomeiwen.com/i1389082/051d202ea47e67e0.png)
![](https://img.haomeiwen.com/i1389082/d7d81e4b894b2017.png)
![](https://img.haomeiwen.com/i1389082/ebb143590600c6b8.png)
![](https://img.haomeiwen.com/i1389082/5f15d18b329d2128.png)
2.录音
使用AVAudioRecorder可以实现录音功能。
代码如下:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityLow], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt:2],AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,
nil
];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.caf", [[NSBundle mainBundle] resourcePath]]];
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:dict error:nil];
[self.recorder prepareToRecord];
Demo代码如下:
![](https://img.haomeiwen.com/i1389082/3cd6bc78c478fd43.png)
![](https://img.haomeiwen.com/i1389082/fab9cd3c251d0b4e.png)
![](https://img.haomeiwen.com/i1389082/a15458b1e96ed1e6.png)
3.讯飞语音的使用
在移动应用中使用语音识别功能后,原来需要键盘输入的地方可以直接使用语音进行输入,从而解放用户的双手提供更好的用户体验。目前讯飞MSC在中文语音识别中用得较多,下面就以讯飞语音识别为例讲解如何实现语音搜索功能。可以在讯飞开放平台的SDK下载中心下载讯飞提供的语音听写功能,当然需要注册账号并获得使用SDK的APPID,下载后的文件夹中包含了文档、库文件和样例程序,可以直接阅读文档或参考样例程序来实现自己的语音搜索功能。创建项目后,需要向项目中加入讯飞的库文件以及它依赖的其他库文件,可以在下图所示的位置进行添加,需要添加的库文件也如下图所示,其他的库文件可以通过Xcode的自动链接器自动加入。
可以对讯飞提供的SDK进行二次封装,使其变成更容易使用的API(只用一行代码就可以使用它的功能),代码如下所示:
![](https://img.haomeiwen.com/i1389082/18269e3021af8f02.png)
![](https://img.haomeiwen.com/i1389082/63749b46a75099ba.png)
![](https://img.haomeiwen.com/i1389082/7f40a78d0366e56b.png)
![](https://img.haomeiwen.com/i1389082/c3af96d912d63d28.png)
上面的代码在讯飞提供的样例程序中可以找到,无需自行编写,但是可以对其中的一些参数进行设置,例如language(语种)、vadEos(语音结束后的超时点)、vadBos(语音开始前的超时点)、speechTimeout(语音长度达到多少时间超时)等。
.h文件
#import <Foundation/Foundation.h>
#import "iflyMSC/IFlyMSC.h"
typedef void(^CDVoiceHandler)(NSString *);
@interface CDVoice : NSObject <IFlyRecognizerViewDelegate> {
IFlyRecognizerView *rView;
}
@property (nonatomic, copy) CDVoiceHandler voiceHandler;
+ (instancetype) sharedInstance;
- (void) voiceToTextString:(CDVoiceHandler) handler;
@end
.m文件
#import "CDVoice.h"
#import "IATConfig.h"
#define LEFT_FOR_RV 50#define TOP_FOR_RV 50
@implementation CDVoice
- (instancetype) init {
@throw [NSException exceptionWithName:@"CDVoiceException" reason:@"不允许使用初始化方法" userInfo:nil];
}
- (instancetype) initPrivate {
if (self = [super init]) {
// Do some initialization here!!!
} return self;
}
+ (instancetype) sharedInstance {
static CDVoice *instance = nil;
if (!instance) {
instance = [[self alloc] initPrivate];
} return instance;
}
- (void) voiceToTextString:(CDVoiceHandler) handler{
if (!rView) {
rView = [[IFlyRecognizerView alloc] initWithOrigin:CGPointMake(LEFT_FOR_RV, TOP_FOR_RV)];
[rView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
//设置听写模式
[rView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
rView.delegate = self;
IATConfig *instance = [IATConfig sharedInstance];
//设置最长录音时间
[rView setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//设置后端点
[rView setParameter:instance.vadEos forKey: [IFlySpeechConstant VAD_EOS]];
//设置前端点
[rView setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
//设置采样率,推荐使用16K
[rView setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
if ([instance.language isEqualToString:[IATConfig chinese]]) {
//设置语言
[rView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
//设置方言
[rView setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
} else if ([instance.language isEqualToString:[IATConfig english]]) {
//设置语言
[rView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
}
//设置是否返回标点符号
[rView setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];
}
// 绑定语音识别完成后做回调的Block
self.voiceHandler = handler;
[rView start];
}
// 语音识别完成的回调
- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast {
NSMutableString *result = [[NSMutableString alloc] init];
NSMutableString * resultString = [[NSMutableString alloc]init];
NSDictionary *dic = resultArray[0];
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
NSString * resultFromJson = [self stringFromABNFJson:result];
if (! [resultString isEqualToString:@" "]) {
[resultString appendString:resultFromJson];
}
}
if (!isLast && self.voiceHandler) {
self.voiceHandler(resultString);
}
}
// 语音识别出错的回调方法
- (void)onError: (IFlySpeechError *) error {
if (error.errorCode) {
NSLog(@"%@", error);
}
}
// 从JSON格式的数据中提取语音数据(该方法可以在官方Demo的代码中找到)
- (NSString *)stringFromABNFJson: (NSString *) params {
if (!params) {
return nil;
}
NSMutableString *tempStr = [[NSMutableString alloc] init];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:[params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSArray *wordArray = [resultDic objectForKey:@"ws"];
for (int i = 0; i < [wordArray count]; i++) {
NSDictionary *wsDic = [wordArray objectAtIndex: i];
NSArray *cwArray = [wsDic objectForKey:@"cw"];
for (int j = 0; j < [cwArray count]; j++) {
NSDictionary *wDic = [cwArray objectAtIndex:j];
NSString *str = [wDic objectForKey:@"w"]; [tempStr appendString: str];
}
}
return tempStr;
}
@end
到此为止,我们已经封装好了自己的API,要使用它非常简单,代码如下所示:
#import "AppDelegate.h"
#import "iflyMSC/IFlyMSC.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 将自己申请的APPID通过IFlySpeechUtility工具类进行注册
[IFlySpeechUtility createUtility:@"appid=自己申请的APPID"];
return YES;
}
@end
举例 - 下面完成了一个非常简单的应用来使用我们封装好的语音识别功能:
![](https://img.haomeiwen.com/i1389082/2c4b8bb0fc25a9e5.png)
运行效果如下图所示,点击OK按钮开始进行语音识别,完成后将识别的内容输入文本框中:
我们可以对搜索栏中的书签按钮进行定制,在点击通过调用我们封装好的代码后产生语音识别的视图,然后将识别的结果填入搜索栏中:
- (void) customizeSearchBar {
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
mySearchBar.placeholder = @"请输入要查找的名字";
mySearchBar.showsCancelButton = YES;
mySearchBar.showsBookmarkButton = YES;
[mySearchBar setImage:[UIImage imageNamed:@"Dudu.jpg"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
mySearchBar.delegate = self;
[myTableView setTableHeaderView:mySearchBar];
}
// 点击书签按钮的回调方法
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
[[CDVoice sharedInstance] voiceToTextString:^(NSString *str) {
mySearchBar.text = str;
[mySearchBar becomeFirstResponder];
}];
}
二、视频
1.视频播放
![](https://img.haomeiwen.com/i1389082/b3389e4fcbe5808f.png)
![](https://img.haomeiwen.com/i1389082/9a86d851ce7655d4.png)
![](https://img.haomeiwen.com/i1389082/0ea93ca933291bf7.png)
2.视频开发
相关链接:https://pan.baidu.com/s/16Hn_8ryr6F9AquCHD7hKhg 提取码: fjni
ZSPlayerDemo:https://github.com/wuyubeichen/ZSPlayerDemo
网友评论