From acef22db050712af8e2c4b621146557eb7881b08 Mon Sep 17 00:00:00 2001 From: andybee Date: Mon, 7 Jun 2010 01:27:05 -0700 Subject: [PATCH] Added hintForMimeType: method which returns an appropriate AudioFileTypeID hint for a valid audio file format MIME type. Modified handleReadFromStream:eventType: to utilise this new method. Retained the old hintForFileExtension: method in the code base as it is perfectly functional and, in some cases, may be preferred to using the MIME type detection. --- Classes/AudioStreamer.m | 50 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Classes/AudioStreamer.m b/Classes/AudioStreamer.m index 6804833..224b908 100644 --- a/Classes/AudioStreamer.m +++ b/Classes/AudioStreamer.m @@ -602,6 +602,50 @@ + (AudioFileTypeID)hintForFileExtension:(NSString *)fileExtension return fileTypeHint; } +// +// hintForMIMEType +// +// Make a more informed guess on the file type based on the MIME type +// +// Parameters: +// mimeType - the MIME type +// +// returns a file type hint that can be passed to the AudioFileStream +// ++ (AudioFileTypeID)hintForMIMEType:(NSString *)mimeType +{ + AudioFileTypeID fileTypeHint = kAudioFileMP3Type; + if ([mimeType isEqual:@"audio/mpeg"]) + { + fileTypeHint = kAudioFileMP3Type; + } + else if ([mimeType isEqual:@"audio/x-wav"]) + { + fileTypeHint = kAudioFileWAVEType; + } + else if ([mimeType isEqual:@"audio/x-aiff"]) + { + fileTypeHint = kAudioFileAIFFType; + } + else if ([mimeType isEqual:@"audio/x-m4a"]) + { + fileTypeHint = kAudioFileM4AType; + } + else if ([mimeType isEqual:@"audio/mp4"]) + { + fileTypeHint = kAudioFileMPEG4Type; + } + else if ([mimeType isEqual:@"audio/x-caf"]) + { + fileTypeHint = kAudioFileCAFType; + } + else if ([mimeType isEqual:@"audio/aac"] || [mimeType isEqual:@"audio/aacp"]) + { + fileTypeHint = kAudioFileAAC_ADTSType; + } + return fileTypeHint; +} + // // openReadStream // @@ -1274,14 +1318,12 @@ - (void)handleReadFromStream:(CFReadStreamRef)aStream if (!audioFileStream) { // - // Attempt to guess the file type from the URL. Reading the MIME type - // from the httpHeaders might be a better approach since lots of - // URL's don't have the right extension. + // Attempt to guess the file type from the httpHeaders MIME type value. // // If you have a fixed file-type, you may want to hardcode this. // AudioFileTypeID fileTypeHint = - [AudioStreamer hintForFileExtension:[[url path] pathExtension]]; + [AudioStreamer hintForMIMEType:[httpHeaders objectForKey:@"Content-Type"]]; // create an audio file stream parser err = AudioFileStreamOpen(self, MyPropertyListenerProc, MyPacketsProc,