·您现在的位置: 江北区云翼计算机软件开发服务部 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS第三方语音-微信语音

iOS第三方语音-微信语音

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

网址链接:http://PR.weixin.QQ.com/

里面包含了微信语音和图像,集成很简单,下载方demo后会有个文档,按照流程来(因为它只提供了真机的.a文件,所以只能用真机哦,不然会报错)

先用个有UI界面的sdk

1.装上sdk,引入相关包

2.设置 Build Settings
C++ Standard Library: libstdc++ 或 Compiler Default
Compile Sources As: Objective-C++ 或 将使用 SDK 的文件扩展名改为.mm

随便把一个文件后缀改成.mm不然会报错

3.添加代码

#import "CustomNewViewControl.h"
#import "WXSpeechRecognizerWithUI.h"
@interface CustomNewViewControl ()<WXVoiceWithUIDelegate> {
    WXSpeechRecognizerWithUI *_wxssui;
    __weak IBOutlet UILabel *label;
}
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation CustomNewViewControl

- (void)viewDidLoad {
    [super viewDidLoad];
        _wxssui = [[WXSpeechRecognizerWithUI alloc] initWithDelegate:self andUserKey:@"248b63f1ceca9158ca88516bcb338e82a482ecd802cbca12"];
}
//点击事件
- (IBAction)buttonPressed:(UIButton *)sender {
    label.text = @"";
    [_wxssui showAndStart];
}

//代理 WXVoiceWithUIDelegate
- (void)voiceInputResultArray:(NSArray *)array{
    WXVoiceResult *result=[array objectAtIndex:0];
    [label setText:result.text];
}

无UI界面的sdk也差不多

注意:使用无界面UI需要遵守以下规则

微信语音开放平台免费为你的应用提供语音识别服务,你可以根据自己的风格自由制定 UI,但需在语音采集识别的窗口正确、完整的标注“Powered by 微信智能”或“语音技术由 微信智能提供”的字样。参考如下弹窗:

集成和上面一样,就不再重复

//
//  ViewController.m
//  weixinyuyin2wuui
//
//  Created by apple on 15/8/18.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "ViewController.h"
#import "WXVoiceSDK.h"

@interface ViewController ()<WXVoiceDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // SDK
    WXVoiceSDK *speechRecognizer = [WXVoiceSDK sharedWXVoice];
    //可选设置
    speechRecognizer.silTime = 1.5f;
    //必选设置
    speechRecognizer.delegate = self;
    [speechRecognizer setUserKey:@"248b63f1ceca9158ca88516bcb338e82a482ecd802cbca12"];
}


#pragma mark -----------WXVoiceDelegate------------

- (void)voiceInputResultArray:(NSArray *)array{
    //一旦此方法被回调,array一定会有一个值,所以else的情况不会发生,但写了会更有安全感的
    if (array && array.count>0) {
        WXVoiceResult *result=[array objectAtIndex:0];
        _label.text = result.text;
    }else{
        _label.text = @"";
    }
}
- (void)voiceInputMakeError:(NSInteger)errorCode{
    _label.text = [NSString stringWithFormat:@"错误:%ld",(long)errorCode];
}
- (void)voiceInputVolumn:(float)volumn{
//    [_speechRecognizerView setVolumn:volumn];
}
- (void)voiceInputWaitForResult{
//    [_speechRecognizerView finishRecorder];
}
- (void)voiceInputDidCancel{
//    [_speechRecognizerView didCancel];
}


#pragma mark - 点击事件

- (IBAction)buttonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _label.text = @"录音中...";
        [[WXVoiceSDK sharedWXVoice] startOnce];
       [_button setTitle:@"完成" forState:UIControlStateNormal];
    }else {
        [[WXVoiceSDK sharedWXVoice] finish];
        [_button setTitle:@"录音" forState:UIControlStateNormal];
    }
    
}

- (IBAction)cancelButtonPressed:(UIButton *)sender {
     [[WXVoiceSDK sharedWXVoice] cancel];
    [_button setTitle:@"录音" forState:UIControlStateNormal];
}

@end