I am capturing a five seconds long mp4 file. I would for the User to be able to play and review the video before uploading it to Parse. I don't want to save the video to the local photo library, I just want to replay. Should I be saving the file to the temporary directory like so:
NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
NSError *error;
if ([fileManager removeItemAtPath:outputPath error:&error] == NO){
NSLog(#"File removed from outputPath");
};
}
[MovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath]
recordingDelegate:self];
Can I play the video using this method?
- (IBAction)playVideo:(id)sender {
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"Output.mp4"];
NSURL *fileURL = [NSURL URLWithString:filePath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[_avPlayer play];
}
Related
I have an URL which I get from JSON response, When I hit the URL in browser the file is downloaded, Now am wondering how can I play such URL which is a Download URL.I've been looking around many post But none of them could help me.
type of url am getting : http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1
Below is the code I tried.
NSString *audio=[NSString stringWithFormat:#"%#.mp3",cell.url];
NSLog(#"url:%#",audio);
NSURL *url = [NSURL fileURLWithPath:audio];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:&error];
[_audioPlayer play];
NSLog(#"error %#", error);
Try this:
//download your audio file
NSString *urlString = #"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
NSURL *url = [NSURL fileURLWithPath:urlString];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
// get the file from directory
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:#"sound.caf"];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
{
soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
}
// play audio file
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
You should download your audio file asynchronously.
Thank You Dipankar Das for you help.
I am posting the whole solution here.
which worked for me absolutely.
NSString *audio=#"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
NSURL *url = [NSURL URLWithString:audio];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
// get the file from directory
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:#"sound.caf"];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
{
soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
}
// plau audio file
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
[_audioPlayer setVolume:5.0];
try use AVPlayer or AVPlayerViewController, you can use direct URL to play mp3 files
even with local file as you will first download it to device, then play from local URL
do not forget that object AVPlayer has to be as property of your Class not local object because of auto-releasing, so declare your player variable in class and set in function not just locally in function and try to play ...
[update]
more details how to do it -> How to play video with AVPlayerViewController (AVKit) in Swift
Here is the mistake
NSString *audio=[NSString stringWithFormat:#"%#.mp3",cell.url];
NSURL *url = [NSURL fileURLWithPath:audio];
I assume that it is a remote url and you cannot treat it as a file url. So you must use
NSURL *url = [NSURL urlWithString:audio];
I just wasted like 3 hours on this and I can't see where I'm going wrong.
I'm trying to play a video I have stored locally using AVPlayer. This is how I launch the player:
- (void)openVideo:(NSURL *)videoURL {
NSLog(#"Playing video with the url:\n%#", videoURL);
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
And this is the NSURL I'm passing:
+ (NSURL *)getURL:(NSString *)itemKey withType:(NSString *)itemType {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: [NSString stringWithFormat:#"%#.%#", itemKey, itemType]]];
NSURL *itemURL;
if ([[NSFileManager defaultManager] fileExistsAtPath: dataFilePath]){ // if data exists
itemURL = [NSURL fileURLWithPath:dataFilePath];
}
else {
NSLog(#"The data requested does not exist! Returning an empty url file...");
itemURL = nil;
}
return itemURL;
}
When I run openVideo, the output I get is:
Playing video with the url:
/var/mobile/Containers/Data/Application/72C35DC4-9EF1-4924-91F4-EDA4BDB6AAD3/Documents/sample.vid
But I keep getting a disabled video player..
What am I doing wrong?
Finally figured the issue out. I was downloading my files and storing them as NSData, and had to use writeToFile and specify the correct extension to read them correctly.
NSString *saveFilePath; = [NSTemporaryDirectory()stringByAppendingPathComponent:#"temp.mp4"];
[fileData writeToFile:saveFilePath atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:saveFilePath];
AVPlayer *player = [AVPlayer playerWithURL:filepath];
Hopefully this helps someone out there.
I'm trying to stream an radio in my app.
I'm using AVAudioPlayer, but I have an error I don't understand :
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&error];
if (self.audioPlayer == nil){
NSLog(#"Error loading clip: %#", [error localizedDescription]);
}
else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}
And I have the following error :
Error loading clip: The operation couldn’t be completed. (OSStatus error -43.)
Does anyone see what's wrong ?
EDIT :
This is what the Apple docs say:
The AVAudioPlayer class does not provide support for streaming audio
based on HTTP URL's. The URL used with initWithContentsOfURL: must be
a File URL (file://). That is, a local path.
Now what ? :(
download file to document directoty then play file
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSData *audioData = [NSData dataWithContentsOfURL:myURL];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , fileName];
[audioData writeToFile:filePath atomically:YES];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (self.audioPlayer == nil) {
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
} else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}
Hi I have a setup that records audio (with AVAudioSessionCategoryPlayAndRecord category), now I want to playback and MP3 file as usual with AVAudioPlayer instance, but no any sound can be hear.
AVAudioPlayer do not throw any error, nor in its delegate callbacks.
Has anybody similar experience? How to overcome this?
Please use this -
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , #"Welcome"];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (avPlayer == nil)
{
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
}
else
{
[avPlayer play];
avPlayer.delegate=self;
}
And tell me if it is working or not.
I've been trying to get this to work all day. I've tried everything I could think of to get this to work. I have a song which is an m4a located on a server. I download it and save it to the documents directory and then play it with AVAudioPlayer. When I tap the UIButton to play the song, it doesn't do anything. I am logging the NSError but its returning a (null). Here is my code.
NSError *error;
NSURL *url = [NSURL URLWithString:#"http://a2.mzstatic.com/us/r30/Music/43/49/2e/mzi.ittnbeif.aac.p.m4a"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:#"%#/in-my-life.m4a", documents];
[data writeToFile:file atomically:YES];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:&error];
[player setVolume:1.0];
[player setDelegate:self];
[player setMeteringEnabled:YES];
[player play];
NSLog(#"Play Song: %#", file);
NSLog(#"Error: %#", error);
This is what the log shows.
2011-08-01 17:34:09.659 APP[1275:707] Play Song: /var/mobile/Applications/B1F04C1F-1541-4EDB-B898-264061292EDB/Documents/in-my-life.m4a
2011-08-01 17:34:09.661 APP[1275:707] Error: (null)
Any ideas?