MusicSequenceFileLoad Works in Playgrounds but not in Project - swift

what am i doing wrong?
i'm trying to open a midi file and read it's contents. so far i can open the file in playgrounds but i get "Open::fopen failed" in my project
override func viewDidLoad() {
super.viewDidLoad()
let path = NSURL.fileURL(withPath: "/users/me/file.mid")
var midfile: MusicSequence?
NewMusicSequence(&midfile)
MusicSequenceFileLoad(midfile!, path as CFURL, .midiType , .smf_ChannelsToTracks)
}
i do have import AudioToolbox and CoreMIDI
EDIT: some progress
I'm able to open midi files from bundle with CFBundleCopyResourceURL, but i want to be able to open files from anywhere in finder
fileManager also fails, it returns nil when used inside a project and works in playgrounds.

figured it out.
code fails because the app runs sandboxed and the NSURL can't be accessed

Related

(half solve)Why copy file reference to clipboard programmatically only working for system folder in MacOS?

Solution
add this code pasteboard.data(forType: .fileURL) after pasteboard.writeObjects(emptyArray), it will work.
But I don't know why?
System Verison: MacOS Ventura 13.0
copy.swift
#!/usr/bin/swift
import Cocoa
private func copyToClipBoard(path :String) {
let pasteboard = NSPasteboard.general
var emptyArray = [NSPasteboardWriting]()
emptyArray.append(NSURL(fileURLWithPath: path))
pasteboard.clearContents()
pasteboard.writeObjects(emptyArray)
}
copyToClipBoard(path: CommandLine.arguments[1])
If I run swift copy.swift /Users/USER/Downloads/FILE, paste to chat window for third-party applications is working.
If I run swift copy.swift /Users/USER/tmp/FILE, cannot paste to chat window for third-party applications, application occur no permission error, I already give full access disk to this application.
What's the difference between Downloads and tmp?
How can I make copy file reference programmatically working for any folder?

Unable to find resource located in assets.xcassets

Hey guys I am still kind of new to Stackoverflow so please bear with me if I am doing something wrong, I am trying my best.
I am trying to make a simple app with the new apple watchOS6 and swiftUI in the new Xcode11 beta that plays a sound file from assets.xcassets.
I think I am doing something wrong when trying to find the file I have in the assets, here's the error code:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Model/AudioPlayer.swift, line 21
2019-08-28 15:06:58.396058+0200 WatchOS6Demo WatchKit Extension[12691:794523] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Model/AudioPlayer.swift, line 21
My code is built like this:
I have a ContentView that is a list that just displays my button that calls the my audioplayer when its pressed. The code for ContentView is not included.
The AudioPlayerButton struct looks like this and it basically just calls play() on the player:
struct AudioPlayerButton: View {
#State var player = AudioPlayer()
var body: some View {
Button(action: player.play) {
Image("newPlayButton")
}
}
}
This is my AudioPlayer class where I get the error:
import Foundation
import AVFoundation
import Combine
import SwiftUI
class AudioPlayer {
var didChange = PassthroughSubject <Void, Never>()
var isPlaying: Bool = false
var AudioPlayer: AVAudioPlayer?
let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "Glad", ofType: ".wav")!)
//let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "Glad", ofType: ".wav", inDirectory:"/Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Assets.xcassets/" )!)
func play() {
do {
AudioPlayer = try AVAudioPlayer(contentsOf: url)
AudioPlayer?.play()
} catch {
print(error)
}
}
}
I noticed that there are multiple asset.xcassets folders, one inside watchOS6 WatchKit App and one inside of WatchOS6 WatchKit Extension, to be sure I hadn't put it in the wrong place I just added the file to both of the folders.
Here is what I have tried:
1: I noticed that there is multiple asset.xcassets folders and I have tried to add the file to both of them.
2: I have tried to set both the ForResource and ofType parameters as nil to kind of try to "hit more targets" as the Documentation says: "If you specify nil, the method returns the first resource file it finds that matches the remaining criteria".
3: I tried to give a value to the the 3rd parameter in path(forResource:ofType:inDirectory:) as you can see in my AudioPlayer class with the outcommented line.
4: I took an old sound from an old project that I knew worked. I also tested if that project could run and it could.
Anything that could send me in the right direction are very much appreciated! Thanks.
I uploaded the project to GitHub if anybody wanted to look at it.
Update: IT WORKS I ran the normal iPhone simulator in another project. I did not do anything but make a new project and start the simulator. Then I opened the browser went on YouTube and played a video. It still didn't work so I let the video play in the simulator and continued to google around. Then when the video ended and YouTube had to play an add the sound magically started to work - there was an add even before the video started but that one didn't make it work, it was at the videos end. I was speechless. my GUESS is that the iPhone simulator made a configuration but I have no idea. Any theories about how this work or could work is very much appreciated

Localizing FirebaseUI Auth

I am a swift newbie so please bear with me. I have successfully integrated FirebaseUI into my app. I now wish to localize the strings for Dutch. My whole project is in Dutch so I do not have any localization in the project since it is all hard coded in the storyboard. The FirebaseUI docs say that you should:
// Swift
authUI?.customStringsBundle = NSBundle.mainBundle() // Or any custom bundle.
But I am not sure where to actually place the strings file in the project (or how) or if I should be using a custom bundle?
I have spent a whole day playing around with bundles but without success. Your help would be appreciated!
Here is how I got it to work eventually:
Add a strings file called FirebaseAuthUI.strings. I copied the content of this file from here
Use the following code when you create the controller to handle sign in:
Code:
if let bundlePath = Bundle.main.path(forResource: "FirebaseAuthUI", ofType: "strings") {
let bundle = Bundle(path: bundlePath)
authUI?.customStringsBundle = bundle;
}
Don't forget to translate the strings in step 1.
Good luck

What would cause Xcode MacOs app to point main bundle at "/Applications/Xcode.app"?

I'm struggling to write my first Swift3 app - a screensaver because I can't reference any local files. After hours of debugging and setting up a Playground I found that the Bundle.main.bundlePath is not pointing at my app but rather "/Applications/Xcode.app". Which explains why in the code below - video.mp4 is never found but I get nil back from selecting the Bundle by identifier as well.
Any suggestions on how to fix this? My one local file (video.mp4) is readable, copied as a Bundle Resource and has no spurious xattrs. I'm stuck! Any help appreciated.
Running 10.12.3/Xcode 8.2.1, Swift 3.0.2
Playground code below:
import Cocoa
import PlaygroundSupport
import AVKit
import AVFoundation
var frameRect = NSRect(x: 0, y: 0, width: 600, height: 600)
var mainView = NSView(frame: frameRect)
let bundle = Bundle(identifier: "com.modprods.koorigras-test.Playground")
debugPrint(bundle)
debugPrint(Bundle.main.bundlePath)
guard let path = Bundle.main.path(forResource: "video",ofType:"mp4") else {
debugPrint("not found")
exit(1)
}
As per Eric's comment, this is correct behavior for a Playground.
For other n00bs, creating a new Playground inside your workspace results in greyed out Sources and Resources folders that if you try to show in finder nothing happens (because they don't exist). To access local files from within a new Playground
create the Resources subfolder
copy the asset to Resource
make sure Copy Bundle Resources includes this asset for the target
It appears there is no way of accessing other bundles from within a Playground.

Where should I add my own Markdown file in Vapor project?

I want to add my own Markdown file, a file named profile.md. However, I don't know where I should add the file in my Vapor project, which is developed in Xcode.
I tried adding it to Sources/ or just the root directory, but it cannot be searched by Bundle.main.url(forResource:) function, like:
guard let url = Bundle.main.url(forResource: "profile", withExtension: ".md") else { ... }
Also, I found that I already added it under Copy Bundle Resources tab.
In the normal app (for iOS and macOS), the function above works properly.
How can I add my own file to be used in Vapor project developed in Xcode?
This is a very basic example of loading the contents of a file from a custom location using Foundation rather than Vapor's template view loading mechanism, and returning it as a text response.
drop.get { req in
let templateString = try String(contentsOfFile: drop.workDir + "Resources/profile.md")
return templateString
}
I'm not sure exactly what you mean by "parse, and embed it into another template file" but you'd put that bit in between the lines above.