Iphone实现播放语音

Iphone sdk已经实现了语音播放功能,我们只需调用相应的组建既可。下面实现一个用户输入单词,点击播放语音,能够播放出相应的语音的例子。

源代码:http://easymorse-android.googlecode.com/svn/trunk/Say/

引入的组建:

201008171012.jpg

 

声明:

#import <UIKit/UIKit.h>

 

@interface SayViewController : UIViewController {

 

IBOutlet UITextField *word;

 

}

 

@property (nonatomic,retain) UITextField *word;

 

-(IBAction)say;

 

@end

 

 

实现:

#import “SayViewController.h”

#import <AVFoundation/AVFoundation.h>

 

@implementation SayViewController

 

@synthesize word;

 

 

-(IBAction)say

{

NSString* urlString = [@"http://translate.google.com/translate_tts?q="stringByAppendingString:word.text];

NSURL* url = [[NSURL alloc] initWithString:[urlStringstringByReplacingOccurrencesOfString:@" " withString:@"+"]];

NSData* mp3Data = [[NSData alloc] initWithContentsOfURL:url];

[url release];

AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithData:mp3Data error:nil];

[mp3Data release];

[player play];

 

 

}

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn’t have a superview.

[super didReceiveMemoryWarning];

 

// Release any cached data, images, etc that aren’t in use.

}

 

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (void)dealloc {

[super dealloc];

}

 

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值