Why can't I find a resource when it's an ".mp4"?
My code is:
- (IBAction)babelFishButton:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"BabelFish" withExtension:#"mp4"];
MPMoviePlayerController *player =[[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
[player.view setFrame: self.imageView.bounds]; // player's frame must match parent's
[self.imageView addSubview: player.view];
[player play];
}
BabelFish.mp4 exists in the project navigator, and finder tells me it's in the app root folder, along with main.m, etc.
But the video does not play because url is nil (the file is not being found). I can paste in a copy of the file and rename it "BabelFish.txt", change the extension in the code and it finds the file.
Why won't it find "mp4" files? How do I get the URL of an mp4 file?
Jeff
Try this:
NSString *myUrlSTR = [[NSBundle mainBundle] pathForResource:#"BabelFish" ofType:#"mp4"];
Another thing make sure that your file is not only listed in your Project Explorer, but also included in your Copy Bundle Resources list.
Related
I am having an issue with UIDocumentInteractionController, here is my code:
NSURL *URL = [NSURL fileURLWithPath:#"http://www.domain.com/pdf/35.pdf"];
//NSURL *URL = [[NSBundle mainBundle] URLForResource:#"35" withExtension:#"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
}
my console log says this:
Couldn't issue file extension for path: /http:/www.domain.com/pdf/35.pdf
and in my app it displays a gray background and says 35.pdf Portable Document Format (PDF)
what am i doing wrong?
An http URL is not a file URL.
This:
NSURL *URL = [NSURL fileURLWithPath:#"http://www.domain.com/pdf/35.pdf"];
needs to be:
NSURL *URL = [NSURL URLWithString:#"http://www.domain.com/pdf/35.pdf"];
But note that UIDocumentInteractionController expects a URL to a local resource, not an Internet URL. So you can't use the above URL at all.
You would need to download and save the PDF file locally first and then create a proper file URL to the local file.
After implementing the HCDownload for iOS into my app I am successfully able to display the downloading progress of a file.
The problem is that the guy who wrote it, didn't use Xcode to write the module, so even though it should, it does not really work nice when trying to integrate with a project.
These are the main issues I have with it:
If you do a [self.navigationController pushViewController:dlvc animated:YES]; or a [self presentViewController: animated: completion:]; then it shows when it is initiated. I can get out of it, by navigating back, but then when I return to it, it is blank - so I cannot see what the progress is of the download. The download however keeps going, because it appears in my documents folder. ( I use ASIHTTP framework)
Now I am working with storyboard in this project, and because this does not come with a XIB file I thought (which has kinda worked in the past) I could just have a UITableViewController and have this as its Custom Class, but it does not play ball.
Is there a XIB based Download manager FrameWork that someone can point me too, or has anyone had luck with this one?
PS I know Storyboards do not use XIB files, but that way it is easier to integrate into storyboard than no UI files at all:-)
Kind thanks for the tips.
Edit
Here is the code where I implement it:
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
dlvc.delegate = self;
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
dlvc.downloadDirectory = documentDirectory;
NSString *fileSave = [_streamingURL lastPathComponent];
NSURL *url = [NSURL URLWithString:_streamingURL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[dlvc downloadURL:url userInfo:nil];
// Add your filename to the directory to create your saved pdf location
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:fileSave];
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your temp pdf location
NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:fileSave];
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[request setTemporaryFileDownloadPath:tempPdfLocation];
[request setDownloadDestinationPath:pdfLocation];
[request setDownloadProgressDelegate:progressBar];
[request setShowAccurateProgress:YES];
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadDocument:#"mykeynote.key.zip" inView:webView];
}
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView1
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView1 loadRequest:request];
}
I received a error message after run this code "Sorry, this document can't be viewed."I am currently working keynote 09.How do i open the keynote file in uiwebview? Help me!
There's some information about it here:
http://developer.apple.com/library/ios/#qa/qa1630/_index.html
Specifically what you're looking for is this:
iWork '09 documents do not use a package format and must not be ZIP compressed.
So you shouldn't be compressing your keynote file. This was only required for '08 and earlier.
Since older iWork documents are packages, they are directories. You can use NSFileManager's attributesOfItemAtPath:error: function along with the key NSFileType to see if it's a directory (value should be NSFileTypeDirectory).
If the file is a directory you can use a library like ZipArchive to compress it.
I have included an mp4 resource in my project navigator. I want to play it in an AVPlayer. I know how to create an AVPlayerItem for the AVPlayer object, and so now I'd like to load the appropriate AVAsset into it pointing to my mp4 resource. The problem is that AVAsset only has an assetWithURL: method to create an AVAsset. I want an assetWithName: method, but that method does not exist. How can I play my mp4 file if I don't have a URL for it? If it's not possible to play via name reference, how can I get a file URL for my mp4 file?
To get the URL, use
[[NSBundle mainBundle] URLForResource:#"my_video" withExtension:#"mp4"]
It will probably be in your main bundle, you can get its URL with
- (NSString*) saveFilePath: (NSString *) add
{
NSString *filename = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:add ofType:#"mp4"]isDirectory:NO]]];
return filename;
}
Copy this into your code and then do [self saveFilePath:#"Name of your file"] to get the URL.
Full code:
AVPlayer *player = [[AVPlayer alloc] initWithURL:[self saveFilePath:#"fileName"];
[player play];
I want to play the music from internet by url.
I create a simply project that has one button with the following code:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];
It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?
Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.
In your case:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];
From the QTMovie class reference:
The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.
If you can consider displaying a Quicktime window over your app, you can use this code :
NSString *url = #"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:#"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%#\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];
I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.