In my app I imported 4 sound files. Now I want to list all the sound files in a View. When the user clicks any one of the sound, it needs to be selected and played as like in the Alarms app (choosing the sound for alarm). The difference here is I am getting the sound from my project. I have searched in SO and Google but I couldn't find a solution exactly for this problem.
Assuming that "In my app i imported 4 sound files" means that the files are in your app bundle (and also assuming that the extension of the files is MP3 - you can change it to whatever extensions they actually have):
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePat error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"mp3"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your MP3 file
DoSomethingWithFile(fullFilePath);
}
}
Related
I am making a video delivery application and sending a bunch of files in the following directory structure:
Home/Week 1/Day 1/123.mp4,abc.mp4,42343.mp4
Home/Week 1/Day 2/123.mp4,xyz.mp4
etc. I need to maintain the directory structure and play the appropriate file.
NOTE: there are multiple files with the same name in different folders.
Current code:
I dragged the Home folder into xcode into the other sources folder > the action copied the files into the project directory and added the references.
This is the code I wrote for playing the video "abc" from day1 in week1 in home.
NSString* p = #"home/Week 1/Day 1/abc";
NSString* moviePath2 = [[NSBundle mainBundle] pathForResource:#"Home/Week 1/Day 1/abc" ofType:#"mp4"];
NSURL* movieURL2=[NSURL fileURLWithPath:moviePath2];
In this case, I get moviePath2 as Empty.
If I run this code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:p];
if(![fileManager fileExistsAtPath:filePath])
{
NSLog(#"exists");
}
It prints the exists on console.
Can you please provide a solution for this?
Is my approach to package the directory structure with videos correct??
Thanks for the help.
Access your movie by simply movie name
NSURL* movieURL2=[NSURL fileURLWithPath: p];
Or you want to access it from main bundle then you have to copy your movie to that path
[fileManager copyItemAtPath:moviePath2 toPath:filePath error:nil];
In my application I want to implement a simple Alarm function. I know how to use UILocalNotifications, but I came across this source code with a like UI of the iPhone's native Clock app alarm area as well as it having a believe a type of data persistence. Two things I am not good at interface design and data persistence this source code has. But I downloaded it and started playing around with it to find the alarms are not persistent.
Download
Does anyone know how the source code can be adjusted so that it is persistent and the plist can be saved and read to and from? I am open to learning too, this area is somewhat unknown to me too. Thanks
I review your code and find issue that you not moved your "Alarms.plist" file form resource to document directory. we are not able to edit file which is in resource folder. so write following code in app delegate file.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *theFileName = #"Alarms.plist"; //Change this appropriately
NSString *oldPath = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];//[NSString stringWithFormat:#"%#/Inbox/%#", documentsDirectory, theFileName];
NSString *newPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, theFileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:newPath])
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
Perform save operation on file which is in Document directory folder.
try this code... to save plist from bundle to Document Directory
Notice that you will have "Unable to read... " just at the first app launch
- (NSMutableArray *)displayedObjects
{
if (_displayedObjects == nil)
{
NSString *path = [[self class] pathForDocumentWithName:#"Alarms.plist"];
NSArray *alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
if (alarmDicts == nil)
{
NSLog(#"Unable to read plist file: %#", path);
NSLog(#"copy Alarms.plist to: %#", path);
NSString *pathToSetingbundle = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];
[[NSFileManager defaultManager]copyItemAtPath:pathToSetingbundle toPath:path error:nil];
}
_displayedObjects = [[NSMutableArray alloc]
initWithCapacity:[alarmDicts count]];
for (NSDictionary *currDict in alarmDicts)
{
Alarm *alarm = [[Alarm alloc] initWithDictionary:currDict];
[_displayedObjects addObject:alarm];
NSLog(#"#disply obj %#", alarm);
}
}
return _displayedObjects;
}
I am creating an epub reader. In that I want to list out .epub files from iphone. So I want to know is there any possible way to list out the .epub files from iphone (not just from the project directory path but also anywhere else in the phone)?
No, this is not possible.
Since there is no filesystem access, except the the directory with in apps sandbox.
All apps have to store the files they use with there sandbox, you tell iOS that you app can op .epub files. Which will allow the user to open the file from, example an email in your app.
As answered by #rckoenes, it is not possible to access the filesystem other than your app bundle.
You can access the files in your app bundle like this:
NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
NSError *error;
NSArray *bundleContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePathName error:&error];
for (NSString *currentItem in bundleContents) {
if ([currentItem rangeOfString:#"." options:NSBackwardsSearch].location != NSNotFound) {
int tempIndex = (int)([fileName rangeOfString:#"." options:NSBackwardsSearch].location);
tempIndex++;
NSString *aStrExtension = [[fileName substringWithRange:NSMakeRange(tempIndex, [fileName length]-tempIndex)] lowercaseString];
if ([aStrExtension isEqualToString:#"epub"]) {
//Add this file to an array, to make it available for choosing and view its details
}
}
}
If you mean you want to open ePub files saved not inside your application bundle, then you cant, you will have access only to the files inside your app sandbox
As per the #rckoenes: Any files out of App bundle is not accessible,
So I retrieved .epub files like this way.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
while ((filename = [direnum nextObject] )) {
if ([filename hasSuffix:#".epub"]) { //change the suffix to what you are looking for
[arrayListofEpub addObject:[filename stringByDeletingPathExtension]];
}
}
Suppose, I have added one folder name "Images" in my project.How can I get the path to that folder? My main intention is to get the number of pictures in "Images" folder.
You should work a bit more on your question: it assumes a lot and requires the reader to guess.
I have added one folder name "Images" in my project
So I guess this means you added it as a folder reference
and I want to get it's path
And I guess that you want to do that at run time from your application, not at build-time from Xcode.
If so, you could do something like:
NSURL *containingURL = [[NSBundle mainBundle] resourceURL];
NSURL *imageURL = [containingURL URLByAppendingPathComponent:#"Images" isDirectory:YES];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:imageURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
[localFileManager release];
NSUInteger imageCount = [content count];
This code does not assume that all images are of the same kind.
[[[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"Images"] count];
This returns the number of jpg images from the Images folder. This is the case if you added the images to your application bundle.
Hi everyone I am working on an application which records the user sound and save that to NSDocumentDirectory.The data files are saving well.But the problem is that i want to play those files.I am using the table view for this to populate it with the number of files in the NSDocumentDirectory.But the table shows only one cell the current sound file.I am doing mistake somewhere please help me out.
I am using this code to play.
-(void)PlayAudio
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"strffdsa %#",documentsDirectory);
NSURL *fileURL = [NSURL fileURLWithPath:recorderFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
player.numberOfLoops = 0;
//[fileURL release];
[player play];
[player setDelegate: self];
}
Where recorderFilePath is the path of file.The path is new everytime with the help of NSDate frmattor.But not getting how to populate the table view with all the files residing in the DocumentDirectory.
Please-2 help me...I am new to this technology.
Thanks in advance to all ........
Use an enumerator to simplify iterating through the Documents directory to search for music files.
//Use an enumerator to store all the valid music file paths at the top level of your App's Documents directory
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];
for (NSString *path in directoryEnumerator)
{
//check for all music file formats you need to play
if ([[path pathExtension] isEqualToString:#"mp3"] || [[path pathExtension] isEqualToString:#"aiff"] )
{
[myMusicFiles addObject:path];
}
}
//the NSArray 'myMusicFiles' will be the datasource for your tableview
You need NSFileManager's contentsOfDirectoryAtPath:error: method to get an array of files in the request directory. Then iterate over that array to find the file name you're looking for.