现在的位置: 首页 > 综合 > 正文

Iphone实现播放语音

2013年10月03日 ⁄ 综合 ⁄ 共 1121字 ⁄ 字号 评论关闭

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 allocinitWithData: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

抱歉!评论已关闭.