I'm developing an iPhone application and i need to download and Save mp3 media on the iPhone and play it,
I fished the downloading work and i put the data in NSMutableData variable But How can i save these data as mp3 file
any advice :)
If you want to save it to your applications Documents directory with a .mp3 file extension you can use the following:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
[[NSFileManager defaultManager] createFileAtPath:mp3FileName
contents:mp3Data
attributes:nil];
Where mp3Data is your NSMutableData. I use code similar to the above for saving PDF documents, custom file formats, jpgs and all sorts. I've not had cause to try mp3 yet but there's no reason why it shouldn't.
To read the file back into NSData to send to AVAudioPlayer:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
avAudioPlayer.data = mp3Data;
Above para of code untested, never had cause to use sounds in my apps.
here is the correct soulution appending up on #Diziet comment
'//prepare data to save in file
NSData *fileData=[[NSData alloc]initWithData:(NSData *)receivedData];
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:#"MyFileName.mp3"];
//create the file
[[NSFileManager defaultManager] createFileAtPath:mp3FileName contents:receivedData attributes:nil];
//load the file for playing
NSString *FileNamePath = [[NSString alloc] initWithString:#"MyFileName.mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath ];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
player.delegate=self;
[player play];'
Related
If i go to the point then my problem is, i want to make a web view where it will load a video and when stream starts then i want to download/store that video data in the resource folder of the application.How can i do that?
Please somebody help me by any kinds of help.
BR
Emon
In the run time you can't save a video to resource folder but you can save files in to
NSDocuments and retrive them using NSFileManagers.Go through this link
http://www.techotopia.com/index.php/Working_with_Files_on_the_iPhone
Try with this Code to save a file
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
NSData* videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your Url"]];
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
[filemanager createFileAtPath:filePath contents:videoData attributes:nil];
To retrive:
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
BOOL exists = [fm fileExistsAtPath:newDirectory isDirectory:&isDir];
if (exists) {
//Play Video with contents of file path.
}
I'm beginning to store an NSArray in file, but I keep getting nil as a response...
The Apple docs say that nil means your file doesn't exist, but I'm pretty sure mine does (I'm probably not building my path correctly)...
Here is the code that I use to store my NSArray in a file...
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"scorecards" ofType:#"dgs"];
NSArray *myArray = [[NSArray alloc] initWithObjects:#"J", #"ANA", #"MY", #"GDON", nil];
[myArray writeToFile:filePath atomically:YES];
NSArray *mynewArray = [NSArray arrayWithContentsOfFile:filePath];
NSLog(#"%#", [mynewArray objectAtIndex:2]);
I also took a screenshot of Xcode to show you guys that my file does exist...
The file is clearly saved in the group ScorecardsSaved and the file is called scorecards with the extension dgs (something I made up for my specific application - Dot Golf Scorecard - dgs)
Indeed, your file path uses the pathForResource method, thus it will point to a location in your application bundle, which is not writable.
Instead, you have to write to the application document directory on the device / simulator.
NSURL *documentsDirectory =
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *fileURL = [documentsDirectory
URLByAppendingPathComponent:#"storecards.dgs"];
So if your bundle version of the document contains some seed data, copy it to the app documents directory first.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
[myArray writeToFile:filePath atomically:YES];
I'm new with iPhone development. I want to download a pdf using url and want to store it in a local folder.
Here is my code:
NSData *d = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://demo.com/ebookapplication/book_pdf/20111108094614hii.pdf"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myfile.pdf"];
[d writeToFile:path atomically:YES];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"pdf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:path error:NULL];
}
NSURL *pdfurl = [NSURL fileURLWithPath:path];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pdfurl];
[detail.bookweb loadRequest:requestObj];//to display data in web view
This code is working for display pdf in web view using url, but it doesn't work for storing pdf in our local system. Actually I want to store these downloaded pdf in one folder and and display it in library format. Please help.
-writeToFile:Atomically has already placed your file in the folder you've specified in the -copyItemAtPath:toPath: method, and is thus redundant. The document was already in the documents directory folder, you then copied it over to the same directory! I think that's your problem.
EDIT:
This Line HERE:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myfile.pdf"];
specifies your path.
You can NSLog it with this line: NSLog(#"Document Path = %#", path);
As for your library comment, it shouldn't be that hard. My app, which basically does this already, wires
AQGridView: https://github.com/AlanQuatermain/AQGridView
And Apple's Docwatcher class from DITableView: http://developer.apple.com/library/ios/samplecode/DocInteraction/Listings/Classes_DITableViewController_h.html
Together to create an app that trolls the documents directory and adds files to an NSArray which are then displayed by the grid. A simpler approach would just be to use the DITableViewController though, considering it solves your problems, it's just the grid was for a little more pizzaz.
I have an application where the user should be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album.But I want to bring them into my project just like the way we add required images for the project into the Resources folder in Xcode. At the same time I want them to converted into PNG format.
After you have taken image from Camera you can save this image to your application's document directory as,
UIImage *image = imageFromCamera;
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:#"MyImage.png"];
[imageData writeToFile:fullPathToFile atomically:YES];
Its tested code and working absolutely fine.
here is a great tutorial that you should check out http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
And here is the documentation from Apple for the UIImagePickerController that you should use https://developer.apple.com/documentation/uikit/uiimagepickercontroller
NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocsDir = [UsrDocPath objectAtIndex:0];
NSString *path = [DocsDir stringByAppendingPathComponent:#"image.jpg"];
//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
BOOL success = [FileManager fileExistsAtPath:zipPath];
if(success){
[FileManager removeItemAtPath:path error:&error];
}
[UIImageJPEGRepresentation(image,1.0) writeToFile:path atomically:YES];
I've been browsing around but couldn't find a solution to my problem. I'm trying to save some video files to my application's directory I do this as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"myFile"];
NSLog(#"%#",appFile);
[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:#"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];
My question is, is this the proper way of saving the movie file and if yes how do I convert the NSData to a file that can be played back via MPMoviePlayer? Thanks for your help.
PS. I really don't want to use the photo library as the app is likely to hold a fair amount of videos.
If you really want to save it in the documents of your app, you'll have to use NSFileManager
it would probably look like something like that
NSString* completeFileName = [NSString stringWithFormat:#"%#",movieName];
NSString* filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:completeFileName];
[[NSFileManager defaultManager] createFileAtPath:filename contents:yourMovieData attributes:nil];
You also might need to use NSFileHandle
To read the video in your MPMoviePlayer, if think you can make an NSURL like this
[NSURL fileURLWithPath:path isDirectory:NO];
then you give that url to your MPMoviePlayer.
I hope this will help you.