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.