AVAudioPlayer Doesn't play the songs inside the folders? - ios

I used this code to play songs it is not playing the songs inside the folders or folders of folders except the Documents directory.
NSString *songname=[NSString stringWithFormat:#"%#",songArray[songIndex]];
NSLog(#"songname:%#",songname);
NSString* saveFileName = songname;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"github.mp4"];
NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path];
avPlayer = [AVPlayer playerWithURL:url1] ;
[self.avplayer play];

Related

Open video file from Assets folder and play it

I am trying to load a .mp4 file that I added inside assets/videos folder but I fail. This is my code so far:
- (IBAction)showVideo:(id)sender
{
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths1 objectAtIndex:0];
NSString *strPath = [NSString stringWithFormat:#"%#/%#",documentsDirectory,#"myVideoName.mp4"];
NSLog(#"strPath %#",strPath);
NSURL *videosURL = [NSURL fileURLWithPath:strPath];
AVPlayer *player = [AVPlayer playerWithURL:videosURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
controller.player = player;
[player play];
}
I think I am doing something wrong with the video file path.
Can you help me? Thanks!
I found the solution. I replaced this:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths1 objectAtIndex:0];
NSString *strPath = [NSString stringWithFormat:#"%#/%#",documentsDirectory,videoName];
NSLog(#"strPath %#",strPath);
with this:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: #"videoName" ofType: #"mp4"];

AVAudioFile can not read file from document directory

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];
}

How to check the audio is stored locally and run it without downloading?

In this code, the audio is downloaded every time when I click on a button. I want to do check audio stored locally. If stored as run it without loading. How can I do this?
- (void) song{
NSString *stringURL = #"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"music.mp3"];
[urlData writeToFile:filePath atomically:YES];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"music.mp3"];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
}
NSFileManager has the following methods:
- (BOOL)fileExistsAtPath:(NSString *)path;
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(nullable BOOL *)isDirectory;
- (BOOL)isReadableFileAtPath:(NSString *)path;
- (BOOL)isWritableFileAtPath:(NSString *)path;
- (BOOL)isExecutableFileAtPath:(NSString *)path;
- (BOOL)isDeletableFileAtPath:(NSString *)path;
So we check like this:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"music.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO];
if (!fileExists) {
// start the download process here then save
[urlData writeToFile:filePath atomically:YES];
}

save pdf locally in IOS

hi i am created pdf file using Quartz framework in IOS.i need to download that pdf file in local folder like saving images in simulator.any one tell me how to do this.
my code for creating pdf
-(NSString*)getPDFFileName
{
NSString* fileName = #"Invoice.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
return pdfFileName;
}
-(void)showPDFFile
{
NSString* fileName = #"Invoice.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSURL *url = [NSURL fileURLWithPath:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
}
- (void)viewDidLoad
{
NSString* fileName = [self getPDFFileName];
[PDFRenderer drawPDF:fileName];
[self showPDFFile];
[super viewDidLoad];
}
thanks in advance..
This will do what you need.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *pdfDirectoryString = [NSString stringWithFormat:#"%#/YOUR_FILE_NAME", documentsDirectoryPath];
NSError *error = nil;
if (![YOUR_PDF_DATA writeToFile:pdfDirectoryString options:NSDataWritingAtomic error:&error])
NSLog(#"Error: %#", [error localizedDescription]);
Try this
For saving PDF in Document Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *data = [PDFImageConverter convertImageToPDF:screenShot]; // convert PDF to data
NSString *path = [NSString stringWithFormat:#"%#/%#",[docArr objectAtIndex:0],[NSString stringWithFormat:#"userDetails.pdf"]];
[data writeToFile:path atomically:NO];
To retrieve and display in web view
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPathString =[NSString stringWithFormat:#"%#",[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"userDetails.pdf"]]];
NSURL *targetURL = [NSURL fileURLWithPath:pdfPathString];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[contentWebview loadRequest:request];

How to Read a file from documents directory in iOS

I need to search a file in documents directory of iOS but i don't know the extension of that file. How to search the file without extension.
Below is the code if you know the filename.extension(abc.txt). How can i search for the same file with jsut filename(abc)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"abc.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:NULL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *fileWithExt = nil;
for (NSString *file in directoryContent )
{
if([file stringByDeletingPathExtension] isEqualsTo:#"abc"]
{
fileWithExt = file;
break;
}
}
P.S. : The fileWithExt will contain the first file with name abc

Resources