How to play NSData audio or video in iOS? - 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.

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

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.

Related

Finding video type from NSData

I am loading an video from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a video player and display it fine.
I want to persist this data to a file. However, I don't know what format the data is in (mp4, wav)? I assume it is mp4 (since it's an video from the web) but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything. I just wanted to know the file extension whether it is an image or video.
You should check the content type returned by the server:
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)theResponse
{
NSString* content_type = [[(NSHTTPURLResponse*)theResponse allHeaderFields] valueForKey:#"Content-Type"];
//content_type might be image/jpeg, video/mp4 etc.
}

send audio with MFMessageComposeViewController

does anyone know i this is possible by doing something like
[compose addAttachmentData:data typeIdentifier:(NSString *)kUTTypeAudio filename:#"test"];
The resulting message isn't of any playable format so not sure if its possible at all? The data file does play in an audio player so I know thats correct.
Thanks
Jules
You need to set the filename to include a proper file extension, e.g. #"test.caf", instead of just #"test". See this answer for a similar question:
MFMessageComposeViewController iOS7 addAttachmentData:typeIdentifier:filename: not working

CocoaHTTPServer on iOS: set up server so user can download NSData as file

I want to make the following webpage using CocoaHTTPServer: there should be a link to download a file, but the source file must be NSData object in memory.
As far as I see in samples, there is an easy way to link some file on iPhone to the hyperlink. Is it possible to "link" NSData?
Would be very thankful for examples.
All you need to do is to return HTTPDataResponse in your HTTPConnection subclass.
If you want an example have a look at the CocoaHTTPServer sample called DynamicServer and replace - httpResponseForMethod: URI: in MyHTTPConnection with something similar to the following:
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
// Before returning you can analyze the passed path argument and select the correct data object to return...
return [[HTTPDataResponse alloc] initWithData:placeYourDataInstanceHere];
}

Quicktime metadata APIs and iTunes

I'm trying to set some metadata in a .mov file with the quicktime metadata APIs and have it show up in iTunes. I've got it working for most of the properties, but I can't get the description field to populate. Here is the code I'm using (shortened to only show what I think is the relevant portion).
const char* cString = ([#"HELLO WORLD" cStringUsingEncoding:NSMacOSRomanStringEncoding]);
QTMovie* qtMovie = [[QTMovie alloc] initWithFile:filename error:&error];
Movie movie = [qtMovie quickTimeMovie];
QTMetaDataRef metaDataRef = NULL;
OSStatus err = noErr;
err = QTCopyMovieMetaData(movie, &metaDataRef);
QTMetaDataItem outItem;
QTMetaDataAddItem(metaDataRef,
kQTMetaDataStorageFormatiTunes,
kQTMetaDataKeyFormatCommon,
(const UInt8 *)&key,
sizeof(key),
(const UInt8 *)cString,
strlen(cString),
kQTMetaDataTypeUTF8,
&outItem);
I found the following link, stating that for the information and description properties, I should be using kQTMetaDataStorageFormatQuicktime, but that doesn't seem to make any difference. Has anyone else had any success getting the description column to populate when importing metadata into iTunes videos?
http://lists.apple.com/archives/quicktime-api/2006/May/msg00115.html
I ended up using AtomicParsley http://atomicparsley.sourceforge.net/ without any issues which also has the benefit that it supports mp4 and m4v files and not just mov files which is also something I needed. With that the descriptions showed up fine. It was also much easier to use than the QTMetaData api.
Edit: Argh.. Just found out that this app doesn't work with mov files. This will work with mp4 and m4v files, but I guess the original question still stands because I would like to support mov files as well.
Figured it out finally with the help of this post and some deep debugging into the contents of my tagged media.
Retrieving the key name on AVMetadataItem for an AVAsset in iOS
I set the data format to kQTMetaDataStorageFormatiTunes and the key format to kQTMetaDataKeyFormatiTunesShortForm. And then the tags I use are the encoded id3 tags like in the post above. The common keys (kQTMetaDataCommonKeyArtist, kQTMetaDataCommonKeyComment) will generally not work if your goal is to view the data in iTunes. It seems a couple of them still do work, but in general they don't map over properly to their id3 counterparts.

How to extract the song name from a live audio stream on the Blackberry Storm?

HI
I am new to Blackberry.
I am developing an application to get the song name from the live audio stream. I am able to get the mp3 stream bytes from the particular radioserver.To get the song name I add the flag "Icy-metadata:1".So I am getting the header from the stream.To get the mp3 block size I use "Icy-metaInt".How to recognize the metadatablocks with this mp3 block size.I am using the following code.can anyone help me to get it...Here the b[off+k] is the bytes that are from the server...I am converting whole stream in to charArray which is wrong, but how to recognize the metadataHeaders according to the mp3 block size..
b[off+k] = buffers[PlayBuf]PlayByte];
String metaSt = httpConn.getHeaderField("icy-metaint");
metaInt=Integer.parseInt(metaSt);
for (int i=0;i<b[off+k];i++)
{
metadataHeader+=(new String(b)).toCharArray();
System.out.println(metadataHeader);
metadataLength--;
Blackberry has no native regex functionality; I would recommend grabbing the regexp-me library (http://code.google.com/p/regexp-me/) and compiling it into your code. I've used it before and its regex support is pretty good. I think the regex in the code you posted would work just fine.

Resources