I am getting Exc_bad_Access exception while reading a file with AVAssetReader .
The File is located in document directory and is in m4a format. Here is the code i am using. Not able to figure out where m getting it wrong:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:#"My_Recording2.m4a"];
NSURL *newURL=[NSURL URLWithString:filepath];
if ([[NSFileManager defaultManager] fileExistsAtPath:newURL.absoluteString])
{
NSLog(#"File exists");
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:newURL options:nil];
NSError *assetError;
if(asset)
NSLog(#"all ok");
self.assetReader = [AVAssetReader assetReaderWithAsset:asset error:&assetError];
I am getting the exception at last line i.e.
self.assetReader = [AVAssetReader assetReaderWithAsset:asset error:&assetError];
I think your [NSURL URLWithString:filepath] is not right.
Can you try with fileURLWithPath method. Hopefully that will solve your issue.
Related
I tried to play mp3 file using AVP player. But i couldn't play it..my code is
NSString *fileurl=[FILE_URL_Array objectAtIndex:indexPath.row];
NSArray *parts = [fileurl componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
NSLog(#"file name is %#",filename);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,filename];
//NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString* foofile = [documentsDirectory stringByAppendingPathComponent:filename];
NSLog(#"foofile %#",foofile);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
NSLog(#"boolean value is %hhd",fileExists);
if (fileExists)
{
NSURL *audioURL = [NSURL fileURLWithPath:filePath];
NSLog(#"existig url is%#",audioURL);
NSData *urlData =[NSData dataWithContentsOfURL:audioURL];
// NSString *sourcePath =[audioURL path];
//UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,filename];
[urlData writeToFile:filePath atomically:YES];
audioPlayer = [[AVAudioPlayer alloc]initWithData:urlData error:NULL];
audioPlayer.delegate = self;
[audioPlayer play];
}
but i am unable to play that audio file....Please give your valuable solution.Thanks in advance...
You're currently appending documentsDirectory twice.
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,filename];
//NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString* foofile = [documentsDirectory stringByAppendingPathComponent:filename];
Once in filePath, and again in foofile.
Additionally, you seem to be doing a whole bunch of extra processing that you don't need to be doing. You should be able to create the audio player from the initial file url:
NSString *fileurl=[FILE_URL_Array objectAtIndex:indexPath.row];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL urlWithString:fileUrl] error:&error];
if (error != nil) {
NSLog(#"Error creating player: %#", error);
}
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/audio.mp3",documentsDirectory];
NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];
if ([filemanager fileExistsAtPath:filePath])
{
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}
This can not read the audio file, It returns audioFile nil. For same code when I pass url from NSBundle like
NSString *path = [[NSBundle mainBundle] pathForResource:#"Audio" ofType:#"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
It works fine, Any sugestions. Thank you
Issue could be the "/" you are using for append filename to path. Try below change
NSString *filePath = [NSString stringWithFormat:#"%#audio.mp3",documentsDirectory];
Or try below alternative:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audio.mp3"];
The NSBundle class is used for finds thing in applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work with Documents directory.
Try as below:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audio.mp3"];
NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];
if ([filemanager fileExistsAtPath:filePath])
{
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}
I need to save all of audio and video url's into document directory which comes from the server. i have tried a sample code
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: "http://www.ebookfrenzy.com/ios_book/movie/movie.mp4"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath_ = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSString *newStr = [#"test" stringByAppendingString:[NSString stringWithFormat:#".mp4"]];
NSString *FilePath = [NSString stringWithFormat:#"%#/%#",dataPath_,newStr];
[urlData writeToFile:FilePath atomically:YES];
The folder is creating in document directory but the video is not saving into the folder.Can anyone help to sort this out.
You should build the path properly and you should do some basic error checking:
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: #"http://www.ebookfrenzy.com/ios_book/movie/movie.mp4"]];
if (urlData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"MyFolder"];
// Create folder if needed
[[NSFileManager defaultFileManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [dataPath stringByAppendingPathComponent:#"test.mp4"];
if ([urlData writeToFile:filePath atomically:YES]) {
// yeah - file written
} else {
// oops - file not written
}
} else {
// oops - couldn't get data
}
I am having some problems getting my code to work :(
i am trying to download .txt file from the internet and store it in my iphone for further use but it wont work :/
here is my code :
-(void)DownloadFile
{
//declerations
NSString* TheFile;
NSFileManager* FM = [NSFileManager defaultManager];
//downloading file
NSURL *url = [NSURL URLWithString:#"http://www.myfile.com/apple.txt"];
TheFile = [[NSString alloc] initWithContentsOfURL: url
encoding: NSUTF8StringEncoding
error: nil];
//nsstring to nsdata
NSData* data = [TheFile dataUsingEncoding:NSUTF8StringEncoding];
//saving file
[FM createFileAtPath:[DocumentsDir stringByAppendingString:#"/file.txt"] contents:data attributes:nil];
}
DocumentsDir is defined with this function :
-(void)SetDocuments
{
NSArray* doc;
doc = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
DocumentsDir = [doc objectAtIndex:0];
}
Then i use this function assigne the file to a variable:
-(void)SetFile
{
File = [[NSString alloc]
initWithContentsOfFile:[DocumentsDir stringByAppendingString:#"/file.txt"]
encoding:NSUTF8StringEncoding
error:nil];
}
i would appreciate any tips or corrections etc :) thank you for your time.
Change your SetDocuments like this
-(void)SetDocuments{
NSArray* doc;
//doc = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
DocumentsDir = [doc objectAtIndex:0];
}
I would like to play a sound file retrieved from my server, as a base 64 encoded file.
So far, I have decoded the file using base64 and written it to a file as so:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"voicemail.wav"]];
[fileManager createFileAtPath:filepath contents:[responseDict objectForKey:#"ResponseData"] attributes:nil];
However, I am having trouble actually playing the sound file. My code returns the error:
The operation couldn't be completed. OSStatus error 2003334207.
Please can you tell me where I am going wrong?
NSURL * url = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *sound = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err] autorelease];
if (! sound) {
NSLog(#"Sound had error %#", [err localizedDescription]);
} else {
[sound prepareToPlay];
[sound play];
}
#Himanshu
As suggested by the Mattias Wadman, I tried to play downloaded file using vlc/iTunes media player and the file were not playing.
So the problem may be in the encoding or in decoding of file, not in the play audio code. I checked for the same and there were some extra characters embedded while encoding.
We worked on encoding and resolved the issue.
Hope this helps.
NSData *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"FileName.mp3"]
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *sound = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err] autorelease];
if (! sound) {
NSLog(#"Sound had error %#", [err localizedDescription]);
} else {
[sound prepareToPlay];
[sound play];
}