2013-03-12 185 views
0

早上好,AVAudioPlayer不播放聲音IOS

我有以下iPhone應用程序,使用googleTTS將字符串轉換爲音頻,並堅持實際播放下載的數據。有人能幫我弄清楚爲什麼我什麼都聽不到。

viewcontroller.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import "RJGoogleTTS.h" 


@interface ViewController : UIViewController <CLLocationManagerDelegate> 

@property (weak, nonatomic) IBOutlet UILabel *userLabel; 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@property (strong, nonatomic) CLGeocoder *geoCoder; 

@property (strong, nonatomic) RJGoogleTTS *googleTTS; 

- (IBAction)geoCodeLocation:(id)sender; 

- (IBAction)rjGoogleButton:(id)sender; 
@end 

viewcontroller.m

- (IBAction)rjGoogleButton:(id)sender { 

    googleTTS = [[RJGoogleTTS alloc]init]; 

    [googleTTS convertTextToSpeech:@"How are you today user?"]; 

} 

rjgoogletts.h

#import <Foundation/Foundation.h> 

@protocol RJGoogleTTSDelegate <NSObject> 

@required 
- (void)receivedAudio:(NSMutableData *)data; 
- (void)sentAudioRequest; 

@end 

@interface RJGoogleTTS : NSObject { 
    id <RJGoogleTTSDelegate> delegate; 
    NSMutableData *downloadedData; 
} 

@property (nonatomic, retain) id <RJGoogleTTSDelegate> delegate; 
@property (nonatomic, retain) NSMutableData *downloadedData; 

- (void)convertTextToSpeech:(NSString *)searchString; 

@end 

rjgoogletts.m

#import "RJGoogleTTS.h" 
#import <AVFoundation/AVFoundation.h> 

@implementation RJGoogleTTS 
@synthesize delegate, downloadedData; 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     // Initialization code here. 
    } 

    return self; 
} 

- (void)convertTextToSpeech:(NSString *)searchString { 

    //NSString *search = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?q=%@", searchString]; 
    NSString *search = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=en&q=%@", searchString]; 

    search = [search stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSLog(@"Search: %@", search); 
    self.downloadedData = [[NSMutableData alloc] initWithLength:0]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:search]]; 
    [request setValue:@"Mozilla/5.0" forHTTPHeaderField:@"User-Agent"]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
    //[delegate sentAudioRequest]; 




} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"did you receive response user"); 
    [self.downloadedData setLength:0]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"did you receive data user"); 
    [self.downloadedData appendData:data]; 



} 

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

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

    NSLog(@"it worked user!!!"); 
    [delegate receivedAudio:self.downloadedData]; 
    NSLog(@"here are the bytes downloaded, %d", [self.downloadedData length]); 


    NSString *txt = [[NSString alloc] initWithData:downloadedData encoding: NSASCIIStringEncoding]; 
    NSLog(@"%@hello",txt); 

    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithData:self.downloadedData error:nil]; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 

} 

@end 
+0

'[[AVAudioPlayer alloc] initWithData:self.downloadedData error:nil];'我會在這裏檢查錯誤的初學者。 – 2013-03-12 15:53:02

回答

2

您使用ARC嗎?如果是這樣,你將需要保留音頻,因此它不會自動設置爲零,請將以下屬性添加到rjgoogletts的頭文件中,並且瞧! @property (nonatomic, strong) AVAudioPlayer *audioPlayer; 希望這會有所幫助。 Jim

+0

天才......謝謝/ !!!!那工作。 – solarissf 2013-03-12 17:58:52

+0

我已經在.h文件中聲明瞭屬性,即使它在iPad ios6中沒有聲音。 IT在其他設備上播放聲音。 – 2014-07-01 07:33:51