3
4

3 回答 3

4

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.

于 2008-11-24T20:17:29.503 回答
3

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.

于 2008-11-21T17:15:00.277 回答
1

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;
}
于 2008-11-17T01:57:29.180 回答