0

I wish to play a short audio clip (wav) that is hosted using php.

Currently i am using NSURLRequest & NSURLConnection to get the NSData, then attempt to play the audio using AVAudioPlayer.

I am able to get the data fine, but i cannot hear any audio when it comes to the AVAudioPlayer. I have other audio usage elsewhere which confirms that the simulator/mac's audio is working.

-(void) playCurrent{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://example.com/stream.php?file=%@", self.audioID]]];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [conn start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"ConnectionDidFinishLoading");
    NSData *data = _responseData;
    AVAudioPlayer *playA = [[AVAudioPlayer alloc] initWithData:data error:nil];
    [playA play];
}

Update:


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"ConnectionDidFinishLoading");
    NSData *data = _responseData;

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
                                                                  stringByAppendingPathComponent:@"Documents"
                                                                  ]];

    NSString *filePath = [resourceDocPath
                          stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_audio.wav", self.fileID]];

    NSError *error;
    [data writeToFile:filePath options:NSDataWritingAtomic error:&error];


    AVAudioPlayer *playA = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:&error];
    NSLog(@"error %@", error);
    [playA play];
}


error Error Domain=NSOSStatusErrorDomain Code=1954115647 "The operation couldn’t be completed. (OSStatus error 1954115647.)"

Multiple suggests of saving the audio file to a dir, then reading it using AVAudioPlayer appeared to work fine for people. However, i still am unable to audio to play. The data is stored fine, and i have confirmed using safari (quicktime) that the audio file, from stream is fine.

Simon.
  • 1,886
  • 5
  • 29
  • 62
  • can you try to call ...initWithData:data error:&error] to determine what error you get? – Mert Buran Mar 20 '15 at 13:26
  • OSStatus error 1954115647 - http://stackoverflow.com/questions/4901709/iphone-avaudioplayer-unsupported-file-type – Simon. Mar 20 '15 at 15:21

0 Answers0