I'm receiving songs url from xml and saving them in sqlite as binary data.
Now I want to play those songs in AVAudioPlayer.
How would I achieve this?
Is this method is right for playing binarydata of song in AVAudioPlayer.?
Please have a look at this post:
AVAudioPlayer with external URL to *.m4p
AVAudioPlayer only works with local URL's. It must be a File URL (file://)
See Apple's Technical Q&A QA1634
You could get your binary data and write it to a file and then play it:
NSURL *url = [NSURL URLWithString:#"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:NULL];
NSLog(#"error %#", error);
Related
how to play a video in iPhone sdk using MPMoviePlayerViewController,here i capture video through device and save that particular video in documents directory .
please tell me any one....
Get Document Directory Path:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Then you can use any of the directory listing APIs of NSFileManager.
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
Now You have all contents at Directory Path (all videos stored) in array
NSURL *videoURL = [NSURL UrlWithString:[NSString stringWithFormat:#"%#/%#",documentsDirectory,[directoryContents objectAtIndex:0]]]; //check which url u want play as video name is in nsarray i have selected one random video name.
Use MovieControllerPlayer instance to play url:
MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:mpViewController];
[mpViewController.view setCenter:yourView.center];
//[mpViewController release];
Hope useful.
i am trying to play an mp3 audio file, the file will be uploaded by user on web server and i will get the url of that file and then i have to play that file but i am not succeeding in it. here is my code
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
newPlayer = [[AVAudioPlayer alloc] initWithData:theData error:nil];
[newPlayer play];
the url is like this
http://mqm.designers99.com/Notification/mp3_files/1331551313_1919.mp3
Plz. guide me coz i am stuck here, thanx and regards
Try 'reading' the error:
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
newPlayer = [[AVAudioPlayer alloc] initWithData:theData error:&error];
if (error == nil) {
NSLog(#"AVAudioPlayer start playing");
[newPlayer play];
} else {
NSLog(#"%#", error);
}
Any "luck" ?
Include AVFoundation and AudioToolBox frameworks
Import header for that framework in .h file
#import<AVFoundation/AVFoundation.h>
#import<AudioToolbox/AudioToolbox.h>
Instantiate AVAudioPlayer,
AVAudioPlayer *player
Use this code for play audio.It's working fine for me
NSString *url1=#"http://mqm.designers99.com/Notification/mp3_files/1331551313_1919.mp3";
url1=[url1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//Encode your url
NSLog(#"after encoding========:%#",url1);
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
player = [[AVAudioPlayer alloc] initWithData:theData error:nil];
[player play];
Edit:Without encode the url you given also played well.
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.
In my app app i have to parsing a xml file downloaded from internet. How to download this xml and save in documents on iphone ? and then how can i start the parsing of XML saved in documents??
You mean something like that
NSString *URLString = #"http://www.example.com/XML/note.xml";
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
NSData *dataXML = [NSData dataWithContentsOfURL:URL];
NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample.xml"];
[dataXML writeToFile:storePath atomically:TRUE];
NSData *contenuto = [NSData dataWithContentsOfFile:storePath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:contenuto]
Download the contents to an NSData object when it's loaded for the first time. Save it to disk using either one of
– writeToFile:atomically:
– writeToFile:options:error:
Which are described in the NSData class reference here: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:atomically:
When you want to load it from disk, use [NSData dataWithContentsOfFile:] to load it into an NSData object again.
As for parsing NSData into XML, look into other answers to this very, very common question, e.g. How do I parse an NSString containing XML in Objective-C?
My Problem is the following. I got this code and i guess a corrupt NSURL since the AVAudioPlayer is nil after initializing:
NSString *dummyURLString = #"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p";
NSError *error;
NSURL *url = [NSURL URLWithString:dummyURLString];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[player play];
Any suggestions what is going wrong here?
The &error shows this:
Error Domain=NSOSStatusErrorDomain Code=-43 "Operation could not be completed. (OSStatus error -43.)"
AVAudioPlayer only works with local URL's. It must be a File URL (file://)
See Apple's Technical Q&A QA1634
I tried this first but got error 2003334207:
NSData *soundData = [NSData dataWithContentsOfURL:URL];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:soundData error:&error];
Seems that AVAudioPlayer really wants a file. So I put the data into a file first:
NSURL *url = [NSURL URLWithString:#"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:NULL];
NSLog(#"error %#", error);
Instead of using the AVAudioPlayer you can use the AVPlayer. The AVPlayer works as well with remote URLs