How to play a NSData in video player - iphone

I have written an async call to download a video of mp4 format. The response result is an NSData. How can I open the NSData in any video player framework which should be supported in IOS 3.

An NSData is basically a container object, and thus not in a format you can play in a video player. Without seeing any code, or the results of your data, you can create an NSString from data with
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now that you have a string, you can create an NSURL with
NSURL *movieURL = [NSURL URLWithString:dataString];
From there, you should be able to pass this into an MPMoviePlayerController with
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

It's not possible to directly play the NSData into AVPlayer. What you can do is download the Data into Document folder and later fetch the file path and give that path to AVPlayer.

Related

Creating a AVAsset with a HTTP NSURL

I'm trying to merge two NSURLs that contain video references. One of the urls point to a video on AWS and the other points to a video that is stored locally. My exporting code works because I've tried it with two local videos, but whenever I try merge the HTTP url and the local url I get this error: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x155d2f20 {NSUnderlyingError=0x155b4f60 "The operation couldn’t be completed. No such file or directory", NSLocalizedDescription=The requested URL was not found on this server.}
This is the code to create the AVAssets:
AVAsset *firstAsset = [AVAsset assetWithURL:awsURL];
Does AVAssetExportSession require local urls to be used?
#MichaelScaria, many thanks for posting what you figured out, i was on this for about 3 days. below is my solution in full when i was trying to get AVAssets from both local urls and remote urls
+ (AVAsset*)getAVAssetFromRemoteUrl:(NSURL*)url
{
if (!NSTemporaryDirectory())
{
// no tmp dir for the app (need to create one)
}
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:#"temp"] URLByAppendingPathExtension:#"mp4"];
NSLog(#"fileURL: %#", [fileURL path]);
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToURL:fileURL options:NSAtomicWrite error:nil];
AVAsset *asset = [AVAsset assetWithURL:fileURL];
return asset;
}
+ (AVAsset*)getAVAssetFromLocalUrl:(NSURL*)url
{
AVURLAsset *asset = [AVAsset assetWithURL:url];
return asset;
}
I saved the online url to a temporary directory and used the temporary url to merge the video and it worked.
NSData *urlData = [NSData dataWithContentsOfURL:initalURL];
[urlData writeToFile:path options:NSAtomicWrite error:nil]
Maybe you need to use AVURLAsset or other subclasses instead? From the docs:
You often instantiate an asset using AVURLAsset—a concrete subclass of AVAsset—with NSURLs that refer to audiovisual media resources, such as streams (including HTTP live streams), QuickTime movie files, MP3 files, and files of other types. You can also instantiate an asset using other concrete subclasses that extend the basic model for audiovisual media in useful ways, as AVComposition does for temporal editing.

alert sound when start recording

First time i am capturing video in my App, but when i start capturing i want that button click sound as native application. I have searched a lot and came to know that it is a system sound. Can anyone please tell me how can i add that sound in my application when i start capturing video and when i stop capturing.
Currently i am using AVAudioPlayer to play sound as shown below
NSURL *url1 = [[NSBundle mainBundle] URLForResource:str withExtension:#"wav"];
_startAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
_startAudioPlayer.delegate= self;
[_startAudioPlayer prepareToPlay];
[_startAudioPlayer play];
Thanks in advance.....
Just import AudioToolbox.framework.
Create the URL for the source audio file
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
Play sound
AudioServicesPlayAlertSound (soundFileObject);
Here is a nice tutorial link you can follow this
When you want to start capturing video at that time call this bellow method like this...
[self playSound];
use my this bellow method for play sound...
-(void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"adriantnt_release_click" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
first store any mp3 or any audio file in your project folder For Ex: i add adriantnt_release_click.mp3 file.
Also add AVAudioPlayerDelegate in your .h file and add AudioToolbox.framework in your project..

sound is not playing in real device in iphone

I am new to iphone programming.Can any body tell me that below code is for playing recorded sound but its not playing sound in device .can any body tell me what mistake is there in this code.
if(soundID)
{
AudioServicesDisposeSystemSoundID(soundID);
}
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:audiopath isDirectory:NO];
NSLog(#"%#",filePath);
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
In the above code the audio path is the voice recored path. Actually in database path is storing like this
/Users/User/Library/Application Support/iPhone Simulator/5.0/Applications/C974262E-7658-45EE-8E8C-290B780E7C3E/Documents/2012-12-18 13:50:56 +0000.caf
After this while I am retrieving path is show like this:
file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/C974262E-7658-45EE-8E8C-290B780E7C3E/Documents/2012-12-18%2013:50:56%20+0000.caf
What is the mistake in this code? Please tell me.
`NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"your audio file name" ofType:#"wav"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];`
the names dont match the upper one has an additional SPACE which is encoded on ios
a) 2012-12-18 13:50:56 +0000.caf
b) 2012-12-18 13:50:56%20+0000.caf

How to play audio file on websites on iPhone?

I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?
NSURL *bgURL = [NSURL URLWithString:#"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
[bgPlayer play];
}
AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.
Just off the top of my head, I can think of two ways that would probably work:
1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls.
2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.
I have just written answer here.Check it and replace the url used there with yours..
you just replace
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"songname" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
with
NSString *soundFilePath = #"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
There is a proper documentation available at this link
But I think you should also check the error code you are getting by this way:
NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];
Hope this helps.

How to Get VDO File from Server

I want to load a file from a server (e.g. http://www.myname.com/myfile.mov) with my app and play it with VDO MPMoviePLayer. However it uses NSURLconnect to connect to the URL.
How can I convert data of the type NSdata to NSURL for use in VDO?
don't use NSURLConnection to download the data yourself, just pass the URL to the movie player.
NSURL *url = [NSURL URLWithString:#"http://www.myname.com/myfile.mov"];
MPMoviePlayerController* theMovie =
[[MPMoviePlayerController alloc] initWithContentURL: url];