问题
I'd like to play sound file which loaded from internet, so I tried to start from iPhone SDK SpeakHere sample. I recorded the sound, then saved and uploaded to the internet, I could download that file and play without problem from sound tools. But when I tried to play that URL from SpeakHere, I am getting error Program received signal: “EXC_BAD_ACCESS”
.
After trace around, I found that the in -[AudioPlayer calculateSizesFor:]
, it set bufferByteSize
to a huge number 806128768, which caused buffer allocation failed.
And that because in
AudioFileGetProperty(audioFileID, kAudioFilePropertyPacketSizeUpperBound, &propertySize, &maxPacketSize);
The maxPacketSize
returned is 806128768.
I am wondering how to make AudioFileGetProperty
work.
My sound file is here, you could right-click and download from here.
I am using this way to set the URL in the -[AudioViewController playOrStop]
method:
// AudioPlayer *thePlayer = [[AudioPlayer alloc] initWithURL: self.soundFileURL];
AudioPlayer *thePlayer = [[AudioPlayer alloc] initWithURL:
[NSURL URLWithString: @"http://chineseresume.com/localbiz/uploads/myfilename_7_Recording.caf"]];
Any suggestion is highly welcome.
回答1:
Yes, you need to use Audio File Stream Services to play directly from the internet.
I found the "AudioFileStreamExample" example useful, which should be installed in
/Developer/Examples/CoreAudio/Services/AudioFileStreamExample/
Also, Matt Gallagher has an article called "Streaming and playing an MP3 stream" that includes iPhone example code. I haven't looked at it carefully, but it seems like it may be helpful to you.
回答2:
I think the main problem is that the code you are using deals with AudioFile objects (local audio files on disk) but when you load audio from the network you want to use AudioFileStream, which has its own set of API calls to use such as AudioFileStreamOpen
, AudioFileStreamParseBytes
, etc.
回答3:
I figured out a workaround. That is copy the content from internet to a local file, then play sound from that local file. And appears solve the problem.
Not perfect, but just works. If anyone has real solution, please let me know.
Here is the code to copy from internet to local file.
-(CFURLRef)saveURL:(NSString *)urlString {
NSURL *url = (NSURL*)[[NSURL alloc] initWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSArray *filePaths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *recordingDirectory = [filePaths objectAtIndex: 0];
CFStringRef fileString = (CFStringRef) [NSString stringWithFormat: @"%@/BufferFile.caf", recordingDirectory];
NSLog(@"fileString = %@" , fileString);
// create the file URL that identifies the file that the recording audio queue object records into
CFURLRef fileURL = CFURLCreateWithFileSystemPath ( NULL, fileString, kCFURLPOSIXPathStyle, false);
SInt32 err = 0;
CFURLWriteDataAndPropertiesToResource (
fileURL, (CFDataRef)data, nil, &err);
return fileURL;
}
来源:https://stackoverflow.com/questions/293929/speakhere-sample-cant-play-sound-which-loads-from-internet