【手机平台项目学习和分析】发音API及其使用

本文档提供了手机平台发音API的使用教程,通过提供的下载链接获取资源,并展示了简单的自写代码示例,帮助读者快速理解和应用发音功能。
摘要由CSDN通过智能技术生成

下载地址:点击打开链接


由于很简单,自写代码,我只写下代码。一看下就明白了。

//
//  LCSound.h
//  英汉翻译
//
//  Created by lichan on 14-1-3.
//  Copyright (c) 2014年 lichan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface LCSound : NSObject<NSURLConnectionDataDelegate,AVAudioPlayerDelegate>

@property (strong,nonatomic)NSString *string;

@property (strong,nonatomic)NSMutableData *data;


- (void )soundData:(NSString *)str;

@end

//
//  LCSound.m
//  英汉翻译
//
//  Created by lichan on 14-1-3.
//  Copyright (c) 2014年 lichan. All rights reserved.
//

#import "LCSound.h"

BOOL finished;

#import <AVFoundation/AVFoundation.h>

@implementation LCSound

- (void )soundData:(NSString *)str
{
    NSString *speechString = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?ie=UTF-8&oe=UTF-8&tl=zh&q=%@",str];
    

    
    
    
    NSURL *url = [NSURL URLWithString:[speechString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
 
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    

}

- (void )soundData1:(NSString *)str
{
    
    NSString *str1 =@"http://translate.google.com/translate_tts";
    
    
    
    NSURL *postURL = [NSURL URLWithString:str1];
    
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL];
    
    [postRequest setHTTPMethod:@"POST"];
    
    NSString *param = [NSString stringWithFormat:@"?ie=UTF-8&oe=UTF-8&tl=zh&q=%@",str];
    
    
    NSData *urlData = [param dataUsingEncoding:NSUTF8StringEncoding];
    
    [postRequest setHTTPBody:urlData];
    

    [NSURLConnection connectionWithRequest:postRequest delegate:self];
    
    
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
     self.data = [NSMutableData dataWithCapacity:10];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [self.data appendData:data];
    
  
   
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"%d",self.data.length);
    
    AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithData:self.data error:nil];
    player.delegate = self;
    [player play];
    
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值