HyBRiD
HyBRiD

Reputation: 708

How to play NSData audio or video in iOS?

I need to present an audio or video content in iOS.

Unfortunately, I do not receive the audio/video as URL, but as NSData with content-type (MIME type).

It seems UIWebView does not play the audio/video data properly when using the loadData:MIMEType:textEncodingName:baseURL: only when using the loadRequest: (thanks Apple for this wonderful controller)

So I wanted to save the NSData to a temp file, but I don't have the file extension (just MIME type).

What is the best way to accomplish this? Maybe implement an audio/video player using some other iOS controls?

Thanks in advance.

Upvotes: 3

Views: 1400

Answers (2)

HyBRiD
HyBRiD

Reputation: 708

If anyone wants to know here is the solution:

You need to use UTType functions declared in a framework called MobileCoreServices. The basic idea is to get the UTI from the MIME type, then get the extension from the UTI:

MIME-Type --> UTI --> file-extension.

An example of MIME-Type to UTI conversion can be found here.

Upvotes: 0

ahmad
ahmad

Reputation: 1252

make a method that mapps the MIME type to the suitable extension

-(NSString *)getExtenstionFromMimeType:(NSString *)mimeType
{

   if ( [mimeType isEqualToString:@""video/mp4"] )
       return @"mp4"; 
   else if  ( ..... ) 
         and so on 

}

you can also use the MPVideoPlayerController to play audio video but you will also need the source media as a url or a local file path

Upvotes: 0

Related Questions