So I'm just trying to get into iOS Programming by going through some Online-Video-Courses. The problem is, that those are not up-to-date with Swift 3.
In the project I am at the moment it is required to import a Sound-File via the NSBundle-Framework using the following:
let path = NSBundle.mainBundle().pathForResource(name, ofType:)
The problem is, that I won't get this running in Swift 3 and I'm not able to get the right pieces off the Apple Documentation. So my Code looks the following:
import AVFoundation
import Foundation //don't know if this is right by 100 per cent
let path = NSBundle.mainBundle().pathForResource("btn", ofType: "wav")
But it seems like It doesn't even know the Class NSBundle, because Auto-Completion isn't even offering me any functions of NSBundle. I can't seem to find the right pieces which changed in Swift 3 to get this thing working. Anybody here who's able to help me with this?
I believe this is the ticket:
let path = Bundle.main.path(forResource: "btn", ofType: "wav")
Related
At the moment I've got
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: "flac")
I want to have the audioPath find songs with multiple audio extensions like .mp3, .wav, etc but I can't figure out how to do this?
I looked in the bundle programming guide (https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW7) but still couldn't figure it out.
Thanks heaps
edit: I know the names of the files, but in a situation where the folder has mp3's and flacs and wav files and all those other extensions, I want the code to be able to play all of those types of audio files and not just one.
edit: maybe this bit of code is also relevant where I tell it to find only flac files (also wasn't sure how to extend this to other audio file types):
for song in songPath
{
var mySong = song.absoluteString
if mySong.contains (".flac")
People of Earth,
During a recent forest foray, Boober Bunz came across an abandoned print-out of the docs for the AVAudioPlayer object on IOS.
I noticed that there is more than one type of constructor:
init(contentsOf: URL)
and...
init(contentsOf: URL, fileTypeHint: String?)
A URL object seems to contain any and all information needed in order to retreive a file:
let quack:URL? = Bundle.main.url(forResource: "Quack", withExtension: "wav")
See? So Boober Bunz is confused because... what is the point of "hinting" about a file if the computer already has all the information it needs in order to find it?
This would be like saying... "Can I please get a Big Mac Meal super size? Oh and here's a hint... it's food!"
What is a fileTypeHint? who needs one and why?
Can you explain zat?
Thanks everyone!
Im starting to get really frustrated, as every example I can find is coded in swift versions older than 3..
But really what I want, is to store / update a value in my custom .plist file.
I managed to get my view to read my value from the .plist into a label like this:
if let path = Bundle.main.path(forResource: "DIREKT", ofType: "plist") {
if let dic = NSDictionary(contentsOfFile: path){
cityLabel.text = dic.object(forKey: "City") as! String?
}
}
but I also want to update the value in my "settings" view as well
I have no clue whatsoever as to how this should be done, so I could really use some help to get started. I tried to look for solutions here, and on YT as well, but all I get is errors. Now i have spend 7 hours trying to solve this.
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)
I'm up-skilling on swift. I'm writing a framework that will depend on a file that will not be part of the framework. It will be created by the app that imports the framework.
It works fine when running on the simulator but the live rendering in interface builder craps out because it can't find the file. I've done the same thing previously in Objective-c and I know the solution is that the interface builder is using a different bundle. The solution was to use:
[[NSBundle bundleForClass:[self class]] pathForResource:<fileName> ofType:<fileType>]
I've searched online and I believe the equivalent in swift is:
let path:String? = NSBundle(forClass: self.dynamicType).pathForResource(<fileName>, ofType: <fileType>)
however it always returns nil inside the live rendering. While:
let path:String? = NSBundle.mainBundle().pathForResource(<filename>, ofType: <fileType>)
works fine in the simulator, meaning the file is clearly there and part of the finished app. Anyone have any idea why this might be? is there a setting somewhere i'm missing also?
In Swift the code looks like this:
return NSBundle.init(forClass: <ClassName>.self).pathForResource(<FileName>, ofType: <FileType>)
for example, to get localization resources in Arabic from another bundle your code would look like this:
return NSBundle.init(forClass: <ClassName>.self).pathForResource("ar", ofType: "lproj")
I think part of your problem might be that you are trying to hit another bundle but you are accessing the bundle for the current class with your call to [[NSBundle bundleForClass:[self class]].
I haven't written much objective-C but I suspect you will be able to translate this on my behalf.