Find path for local video in Xcode Project - ios

I need to play locally stored Videos in my iOS app.
The videos are located in the directory: App/Resources/Videos/
When I try to get the path with:
NSBundle.mainBundle().pathForResource("myFile", ofType: "mp4", inDirectory: "App/Resources/Videos/")
I get nil. Any advice?

Problem solved: the error was I didn't add the Target-Membership for the Videos.

You can go ahead and remove inDirectory from the function call. Just search for your file in your app main bundle.It shall do the job for you.

Swift 5.4
Bundle.main.path(forResource: VideoHelper.vName, ofType: VideoHelper.vExtension)

Related

can't access ios bundle resource

I am adding my first .wav file to my iOS project using XCode 11.3.1. I have tried various ways to access this resource:
let fileURL = Bundle.main.url(forResource: "greetings.wav", withExtension: nil)
let fileURL = Bundle.main.url(forResource: "greetings", withExtension: "wav")
let fileURL = Bundle.main.path(forResource: "greetings", ofType: "wav")
In all of these cases, fileURL is nil. Other things I have tried:
When I click on the .wav in Project Navigator, I see that Target Membership is checked for my target.
I have ensured greetings.wav is in the same folder as my ViewController which is executing the above statement.
I have also tried both the "Copy to Group" and "Copy to Folder Reference" when adding this resource
Nothing as of yet has produced a non-nil fileURL. Any help/additional troubleshooting so that I can get a valid URL for my .wav file would be greatly appreciated! Thanks.
Check whether your file is available in Copy Bundle Resources under build settings
Just offering possible suggestions here:
Check if you have imported the AVFoundation Module
Check if your resource is called "greetings.wav"

Load .html into WebView from iOS app Resources?

I have an HTML file (webview.html, for now) with relative links to images and .js files. I need to load it, and those linked resources into an iOS app's WKWebView. All the files are stored in Resources/Non-Localized/.
I'm attempting to load the files using the following code during viewDidLoad():
print("loadWebView: Bundle.main = ", Bundle.main); // This prints
if let htmlUrl = Bundle.main.url(forResource: "webview", withExtension: "html") {
print("htmlUrl = ", htmlUrl) // Doesn't print
webView.load(URLRequest.init(url: htmlUrl))
}
The app loads without error, but also without the contents in the webview.
I get the first print statement, but not the second, indicating something is wrong with my URL or resource bundle configuration.
What am I doing wrong? Do I need to do anything in XCode to add these files to the project? Where should I be looking for error messages that will hint me in the correct direction in the future?
I needed to add the file to the Project. I did this by simply dragging the Finder file icon to the project tree in Xcode.
Alternatively (and my final solution), I dragged the parent directory's icon, making a folder reference, and then added subdirectory parameter to .url(forResource...).
I'm still working on getting informative errors out of the WebView.

Determine the path to a video file

Okay what should be a simple step is resulting in null results? This Swift code results in "no good" everytime. The mp4 is in the assets.xcassets folder in the project. I have tried multiple different mp4s that are there, and still comes up with same result. I know I am missing something simple?
if let path = Bundle.main.path(forResource: "Pow", ofType: "mp4") {
print("all good")
} else {
print("no good")
}
Yup, something simple. Move Pow.mp4 out of assets and place it with your other swift files.
Whether the mp4 file is in the root directory or inside Assets.xcassets it should work , you have to open the assests and drag the mp4 file to it below app icons
make sure target is checked
then here is the log of the file path
Thanks guys, that did it. Moving it outside the assets and ensuring that it is a 'target' made it happen. Thankyou!

Play downloaded Widevine Classic files from app document folder in iOS?

I am trying to play wvm files from the app documents folder since 3 days but without success...
I removed "file://" from my path but I still get 1013 error (as discussed here), does someone have some sample code or at least the procedure to follow to make it works properly?
WV_RegisterAsset(myFilePath) always return WViOsApiStatus(rawValue: 1013)
The file should exists because when I try this:
let fileManager = NSFileManager()
print(fileManager.fileExistsAtPath(myFilePath))
It returns true
Thanks in advance for your help!
Widevine Classic on iOS uses an "asset root" directory. When you initialize the library, you pass it in the settings dictionary using the key WVAssetRootKey.
After that, all local asset pathnames you pass to the library are relative to the AssetRoot.
One simple option is to use NSHomeDirectory() as the AssetRoot, and then trim it from the beginning of absolute pathnames.

Playing Video from Online URL works but from Local Path doesn't

I am having trouble playing a local video on my computer. I used a library called Player which is quite straight forward, and in the example project, it's using a Vine video with supplying a link at the top, and it's working:
let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")!
Thus, in ViewDidLoad, self.player.setUrl(videoUrl) works.
I tried to download the Vine video to my local. I saved it as aaa.mp4.
I dragged the mp4 file in xCode project.
I added the file in Copy Bundle Resources
Then I used;
let path = NSBundle.mainBundle().pathForResource("aaa", ofType:"mp4")!
let videoUrl = NSURL(string: path)!
viewDidLoad() {
// print(path)
// print(videoUrl)
self.player.setUrl(videoUrl)
}
The video doesn't get played, but
print(path) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Application/5456FF89-904F-4AE6-A90C-747B6A371745/Player.app/aaa.mp4
and print(videoUrl) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Appl ... /aaa.mp4
Please note that, if I give the path a filename that doesn't exists, it throws an error, thus I know the file is there (and exists) in my case.
What may be the problem? What am I missing? I don't receive any warnings or errors
Use
NSURL(fileURLWithPath: path)!
instead.

Resources