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:[urlString stringByReplacingOccurrencesOfString:@" " 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

运行既可。

ilinxiao -
共有0个回答