I am trying to use Apple App Thinning feature (available from iOS 9) that let you differentiate resources based on device architecture and features. In my case what I would like to do is to have a different video file in the application bundle (in .mp4 format) one for the iPhone and one for the iPad using Xcode .xcassets Data Set.
To retrieve a file from a .xcassets Data Set Apple provides the NSDataAsset class, but: since AVPlayer needs a URL to play a video and NSDataAsset only provides its contents using Data format, I'm unable to play the video.
What I would like to do is to retrive the NSDataAsset .data URL. Is it possible?
You can try:
NSDataAsset *videosDataAsset = [[NSDataAsset alloc] initWithName:#"AssetName"];
NSData *data = videosDataAsset.data;
NSString *filename = #"FileToSaveInto.mp4";
NSURL *URL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:filename];
if ([data writeToURL:URL atomically:YES]) {
// run player
}
Related
This question already has answers here:
How to check if an ALAsset still exists using a URL
(4 answers)
Closed 6 years ago.
I'm getting paths in an iOS app like:
AVURLAsset* urlAsset = (AVURLAsset*)asset;
NSString *filePath = [urlAsset.URL path];
NSURL *videoUrl = [[NSURL alloc] initFileURLWithPath:filePath];
Result:
file:///var/mobile/Media/DCIM/105APPLE/IMG_5255.MOV
This app uploads files, in this case a video, but once I delete the video and go back to the app, this causes issues at the time that the app tries to upload the erased file, is there a way to check if the file exists to prevent issues while running the app? And also does the path changes when it's moved to "Recently Deleted" folder using iOS > 10?
In android is simple as
File file = new File(filePath);
if(file.exists())
//Do something
else
// Do something else.
Use either checkResourceIsReachableAndReturnError: or fileExistsAtPath:.
if([videoUrl checkResourceIsReachableAndReturnError:NULL] == YES) {
//Use file
}
or
if([[NSFileManager defaultManager] fileExistsAtPath:videoUrl.path] {
//Use file
}
I am working on an app where I need to upload videos to server.Now here I have 2 things:
Shoot video using UIImagePickerController,generate a thumbnail and then upload to server
Pick video from Photos gallery, generate thumbnail and then upload to server.
Now the only difference between the two is:
When I use 'generateImageAsynchronouslyForTimes:completionHandler:' method,I get a call in its completionHandler block and I get an AVAsset.Now I am using below code to get its URL:
NSURL *path_url = [(AVURLAsset*)asset URL];
This is where I think things are getting messed up because I am getting something like this in case 2(when I pick video from gallery):
file:///var/mobile/Media/DCIM/102APPLE/IMG_2439.mp4
So I can't upload it while case 1 is is working fine.Is it something related to sandbox?
What's the difference between these 2 paths?
file:///private/var/mobile/Containers/Data/Application/DA4632E3-FA25-4EBE-9102-62495BF105BF/tmp/trim.07786CFE-2477-4146-9EA0-0A04042A8D05.MOV"
file:///var/mobile/Media/DCIM/102APPLE/IMG_2439.mp4
I guess its appSandbox path in 1)
In iOS, every app is like an island and there is a sandbox environment for it.So if you like to upload your video that is not in your sandbox,you will have to copy that video to your sandbox and then you can upload it.This is how you can do this:
NSURL *path_url = [(AVURLAsset*)asset URL];
PHAssetResource *asset_resource = [[PHAssetResource assetResourcesForAsset:[fetchResult lastObject]]firstObject];
PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
options.networkAccessAllowed = YES;
NSURL *newURL = [self getSandboxURLFromURL:path_url];
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:asset_resource toFile:newURL options:options completionHandler:^(NSError * _Nullable error) {
//here you will get the newURL that you will use...
}];
//method to get sandbox URL
-(NSURL*)getSandboxURLFromURL:(NSURL*)photos_gallery_url{
NSString *last_path_component = [photos_gallery_url lastPathComponent];
NSString *pathToWrite = [NSTemporaryDirectory() stringByAppendingString:last_path_component];
NSURL *localpath = [NSURL fileURLWithPath:pathToWrite];
return localpath;
}
I want to import data from my iPhone to my application . up till now i have successfully imported Photo Library and music , but i also want to import pdf and other documents in my iOS app? How can i do that?
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePath error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"pdf"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your pdf file
// DoSomethingWithFile(fullFilePath);
NSLog(#"file: %#",fullFilePath);
}
}
NSURL *url = [[NSBundle mainBundle] URLForResource:#"" withExtension: #"pdf"];
NSLog(#"File: %#",url);
You can't do it in iOS 8 (I haven't checked 9 though). You can't even enlist your app (adding corresponding URL schemes) in the sharing menu of iBooks.
You have to associate your app with the PDF types which you want to open. You can do this by adding some parameters to your Info.plist.
There's a well-answered post which explains this:
How do I associate file types with an iPhone application?
You should also consider reading Apple's documentation and a blog post written by me.
I'm following some examples of how to compress a video to reduce file size prior to uploading it to a server. Specifically, I've looked at this question here: How can I reduce the file size of a video created with UIImagePickerController?
However, I'm wondering where the best place to save the output is.
Even the AV foundation programming guide only provides this as an example:
NSError *outError;
NSURL *outputURL = <#NSURL object representing the URL where you want to save the video#>;
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:outputURL
fileType:AVFileTypeQuickTimeMovie
error:&outError];
BOOL success = (assetWriter != nil);
Again, looking for some guidance on exactly where it is I want to save the video!
Cheers,
Brendan
This is documents directory URL, just append your filename:
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
Almost the same for all others: library (NSLibraryDirectory)
You can write file in document directory using below code
defile document directory path
NSString *DocumentsDirectoryPath() {NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectoryPath = [paths objectAtIndex:0];return documentsDirectoryPath;}
and then
NSURL * outputURL = [NSURL fileURLWithPath:[DocumentsDirectoryPath() stringByAppendingPathComponent:#"yourfilename.mp4"]];
here mp4 is your file extention.
I wanna play mms protocol audio, any idea about this? I heard libmms is a choice. But i do not know how. Anyone can share sample codes? it's better introduce from scratch.
Thanks in advance!!!
You need to look into the DocumentInteractionController here: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/DocumentInteraction_TopicsForIOS.pdf The Document Interaction Controller is the "Apple" way to do it and likely your safest path now and in future versions of the iOS.
If you own the file in your bundle you can get the default app to handle the file type from the OS:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFileCompletePath]]){
NSString *filePath = [[NSString alloc] initWithFormat:#"%#", [self getFileCompletePath]];
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
if ([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL: url];
} else {
NSLog(#"Not supported application to open the file %#", filePath);
}
[url release];
[filePath release];
}
Good Luck!
You basically have two options:
Libmms: this library will only handle the mms protocol. So it's up to you to do whatever you want with the data.
FFmpeg: this library can handle the mms protocol and also has audio/video decoding capabilities. Since you'll be using the mms:// protocol for audio/video streaming, choosing FFmpeg will be a better choice.