0

I have a wav file that plays fine in the browser:

https://d5e5ca34-f670-4a7f-98c7-1643d83ecc1d:N8UAQQPXw10Z@stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&voice=en-US_MichaelVoice&text=Hello%20world

However, it is not playing on my simulator. Is there some limitation on support for wav files in iOS?

  NSString *urlString = @"https://d5e5ca34-f670-4a7f-98c7-1643d83ecc1d:N8UAQQPXw10Z@stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&voice=en-US_MichaelVoice&text=Hello%20world";
  NSURL *url = [NSURL URLWithString:urlString];
  NSData *audioData=[NSData dataWithContentsOfURL:url];

  NSError *error;
  self.player = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
  [self.player setVolume:1.0];
  [self.player setDelegate:self];
  [self.player setNumberOfLoops:0];
  [self.player prepareToPlay];
  [self.player play];
etayluz
  • 15,920
  • 23
  • 106
  • 151
  • 1
    wav is a container. it can contain audio in ANY format for which a codec exists in windows. therefore while the .wav file format may be supported, the actual audio within may NOT be. – Marc B Jun 30 '16 at 16:00
  • @SamB, I have tried adding the file to my project - but it doesn't play that way either. However, no error is presented. – etayluz Jun 30 '16 at 16:07

2 Answers2

1

There is something wrong with your wav audio file. Its not compatible with Apple codecs. I tried a different online wav file and it worked. Here's my working code. Make sure to add "App Transport Security Settings" in your info.plist file

In *.h

#import <UIKit/UIKit.h>
#include <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface v1MyCustomAudioPlay : UIViewController <AVAudioPlayerDelegate>

@property (nonatomic, retain) AVAudioPlayer *myAudioPlayer1;

In *.m

@implementation v1MyCustomAudioPlay

@synthesize myAudioPlayer1;

-(IBAction)playAudio
{
    NSLog(@"playAudio ...");

    NSString *urlString = @"http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/Samples/AFsp/M1F1-Alaw-AFsp.wav";
    NSURL *fileURL = [NSURL URLWithString:urlString];

    NSData *soundData = [NSData dataWithContentsOfURL:fileURL];
    myAudioPlayer1 = [[AVAudioPlayer alloc] initWithData:soundData  error:NULL];
    myAudioPlayer1.delegate = self;
    [myAudioPlayer1 play];

}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    [myAudioPlayer1 stop];
    NSLog(@"Finished Playing");
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Error occurred");
}
Sam B
  • 27,273
  • 15
  • 84
  • 121
0

WAV is supported. Please read answers here in the similar question: Supported Audio file formats in iPhone

Regarding your code, I would not use NSData methods to load the file and instead use something more clear like shown in this answer: https://stackoverflow.com/a/14424418/883738

Point is that

It's not recommend to use dataWithContentsOfURL: as it's synchronous and will block your app will working; if the server is not responding it could block your app for quite a long time.

Hope this helps.

Update: executing code in sample iOS project plays no sound, so the question remains unsolved.

BTW, if you want to use speech synthesizer, you can use iOS built-in

NSString *string = @"Hello, World!";
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:string];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
[synthesizer speakUtterance:utterance];
Community
  • 1
  • 1
Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
  • Thanks, can you please try to run the code and see if it works for you? I still think there is something wrong with the WAV file. I am not concerned about how the file is downloaded at the moment. – etayluz Jun 30 '16 at 16:10
  • It doesn't work and I see following errors in the log: 2016-06-30 12:30:21.322 so[61431:4778248] CFNetwork SSLHandshake failed (-9824) 2016-06-30 12:30:21.323 so[61431:4778248] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824) – Alex Sorokoletov Jun 30 '16 at 16:31
  • Adding ATS security exception to allow arbitrary loads still doesn't help – Alex Sorokoletov Jun 30 '16 at 16:56
  • 1
    Thanks for the effort! – etayluz Jun 30 '16 at 17:07