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];
Related
I have downloaded a mp3 file but i'm not able to play it. The file is downloaded, i've checked but when i try to play the local file, nothing happens. If i directly play it without downloading it, it plays.
- (IBAction)downloadTrack:(id)sender {
NSData *soundData = [NSData dataWithContentsOfURL:self.song.ticket.url];
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:self.song.slug];
[soundData writeToFile:filePath atomically:YES];
NSURL *urlFile = [NSURL URLWithString:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlFile error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Download
NSString *urlString = #"https://oi.net/songs/5-dope-ski-128k.mp3";
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:#"5-dope-ski-128k.mp3"];
[soundData writeToFile:filePath atomically:YES];
// Play
NSString *filename = #"Ola.mp3";
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *yourSoundPath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:yourSoundPath])
{
soundUrl = [NSURL fileURLWithPath:yourSoundPath isDirectory:NO];
}
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer play];
}
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];
}
How can I check if a file is download and save on device? Help me please.
- (void) song{
if (_index1 == 0) {
NSString *stringURL = #"https://drive.google.com/uc?export=download&id=0B0EJbcHq3ZALWUo0a05LMWNzeDg";
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];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
}
}
}
If you want to see if the file music.mp3 is in your Documents folder:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent#"music.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
This only makes sense if that file is always the same file. If it can be different files then you need to associate the URL you use to download with the file you stored it in.
I tried till downloading but downloaded video is showing not supported plz help thanks for help.
Here is my code:
Downloading video from url code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSString *urlToDownload = #"http://cdn-fms.rbs.com.br/vod/hls_sample1_manifest.m3u8";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSLog(#"urldata %# ",urlData);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"thefifle2.mov"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(#"File Saved !%#",filePath);
_url = [NSURL URLWithString:filePath];
[self playv];
});
}
});
Play downloaded video using MPMoviepLayercontroller
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files array %#", filePathsArray);
NSString *fullpath;
for (NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSData *urlData1 = [NSData dataWithContentsOfURL:vedioURL];
NSLog(#" url data1 %# ",urlData1);
NSLog(#"vurl %#",vedioURL);
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
I tried with some mp4 Examples worked for me
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSString *urlToDownload = #"http://techslides.com/demos/sample-videos/small.mp4";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSLog(#"urldata %# ",urlData);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"demo2.mp4"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(#"File Saved !%#",filePath);
_url = [NSURL URLWithString:filePath];
});
}
});
You do realize that your URL doesn't actually point to a "video" file?
.m3u8 Files are not actual Asset files, rather, they are more akin to a playlist. They are used in HTTP Live Streaming and provide a location for "Segments" based on available bandwidth
NSURL *url = [NSURL URLWithString:#"PDFLINK"];
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:url];
NSString * name = #"First";
// Store the Data locally as PDF File
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newFilePath = [documentsDirectory stringByAppendingPathComponent:#"my.pdf"];
NSError *error;
if ([fileManager createFileAtPath:newFilePath contents:pdfData attributes:nil])
{
NSLog(#"Create Sucess");
[self loadPDF];
}
else
{
NSLog(#"Create error: %#", error);
}
}
-(void)loadPDF
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSArray *resultArray = [fileManager subpathsOfDirectoryAtPath:documentsDirectory error:nil];
if (![resultArray containsObject:arrObj])
{
// do something
}
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[resultArray objectAtIndex:0]];
// Now create Request for the file that was saved in your documents folder
//NSLog(#"The filePath %#",filePath);
NSURL *url = [NSURL fileURLWithPath:filePath];
NSLog(#"The url %#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
}
Here is my code.I have more 10 pdf and want to store them locally on device and display next time without download.
It saves the pdf again and i cannot check whether it is downloaded or not ?
Please help me
I am not sure what you mean but, to download a file , you need to use NSData:
NSString *stringURL = #"your file URL";
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,#"filename.extension"];
[urlData writeToFile:filePath atomically:YES];
}
Now the downloaded file exists at filePath and you can get it by using:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"filename with extension"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
you can try this to check if file exists at file path:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [documentsPath stringByAppendingPathComponent:#"filename"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
if(fileExists){
//dont download, just grab it and display it
}
else{
//download and display it
}