0
votes

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

https://d5e5ca34-f670-4a7f-98c7-1643d83ecc1d:[email protected]/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:[email protected]/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];
2
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
@SamB, I have tried adding the file to my project - but it doesn't play that way either. However, no error is presented.etayluz

2 Answers

1
votes

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");
}
0
votes

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];