Swift Read in UTF8 String from MPMoviePlayerController metadata - swift

I need to read some metadata which can contain French or German characters & accents.
I think that I need to read the metadata as UTF8 String. But being a beginner in Swift and Apple in general I don't know how to do this.
I tried to implement some similar solutions found on stackOverflow but none worked.
What I'm working with:
let firstMeta: MPTimedMetadata = radioPlayer.timedMetadata.first as! MPTimedMetadata
let metaData = firstMeta.value as! String
print(metaData)
There I get
Antonin DvoÅák
instead of
Antonin Dvořák
Any ideas?
Easy solution for how to get UTF8 metadata from online audio stream using AVPlayer
https://stackoverflow.com/a/37831097/4249825

This is a problem in the library, you cannot fix it because the string is already decoded from data.
There is a reason why the whole set of classes was deprecated in iOS 9.
You should use AVPlayerViewController & AVPlayer & AVPlayerItem & AVMetadataItem.
They are solving this issue. Don't use MPMoviePlayerController.

let convertedString = String(data: metaData.dataUsingEncoding(NSISOLatin1StringEncoding)!, encoding: NSUTF8StringEncoding)!

Related

Open a short video using QuickTime Player

I have a short video on my local machine. I am trying to open it using NSWorkspace on QuickTime Player.
let stringUrl = "~/Desktop/myrec.mov"
let url = URL(string: stringUrl)!
let config = NSWorkspace.OpenConfiguration()
config.activates = true
NSWorkspace.shared.open(
[url],
withApplicationAt: URL(string: "~/Applications/QuickTime Player")!,
configuration: config
)
The error I am getting is:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file cap/cap.swift
Which has to do with withApplicationAt being the incorrect URL and returning nil.
I'm sure withApplicationAtURL is wrong, but I have no idea how to find the url for the quicktime player app.
I am very new to Swift and am having trouble reading the docs, especially since they don't seem up to date (e.g. my compiler says openFile is deprecated and led me to open).
I'm also not sure if this is the correct/best way to go about accomplishing what I am trying to do (open a small local video on quicktime player).
Any recommendations, tips, advice, or answers would be greatly appreciated!
Try this. Works on my Mac:
let stringUrl = "/Users/whoever/Desktop/myrec.mov"
let url = URL(fileURLWithPath: stringUrl)
let config = NSWorkspace.OpenConfiguration()
config.activates = true
NSWorkspace.shared.open(
[url],
withApplicationAt: URL(fileURLWithPath: "/System/Applications/QuickTime Player.app"),
configuration: config
)
Please note that that I replaced the initializer of URL with the correct one. Also note, that I've swapped the QuickTime Player path with the one that Finder gives me (right-click while holding option key).

Using AVSpeechSynthesizer for Chinese pinyin

In my Swift program, I use AVSpeechSynthesizer to pronounce Chinese characters. This is my method:
static var synth = AVSpeechSynthesizer()
static func speak(string: String)
{
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "zh-CN")
utterance.rate = 0.1
synth.speak(utterance)
}
Often, a single character can have multiple pronunciations. Is there a way to use AVSpeechSynthesizer to pronounce a character given the pinyin. In other words, instead of passing the character "高", we would pass the pinyin "gāo". So far, just passing the pinyin has not worked in this method.
Many thanks in advance,
Jon
I think I've found the answer, and am posting it in case anybody else might find it useful. The pinyin should be entered in the form "gao1", not "gāo". It appears that it must be lower-case: i.e., "GAO1" doesn't seem to work, but "gao1" does.
In my view, using the IPA notation (International Phonetic Alphabet) is the best way to reach your goal.
Put an attributedString as incoming parameter of the AVSpeechUtterance instance after declaring its appropriate attribute.
Take a look at this WWDC 2018 video detailed summary that should help to use AVSpeechSynthesizer for Chinese pinyin pronunciation : helpful implementation can be found in this answer as well.

swift: image is nil after converting to CIImage

I am new to swift and I am trying the CoreML feature using React Native.
I have a source file with URL as string. I convert that string to URL using let imageURL = URL(fileURLWithPath: source) and I tried printing out the source which is of type string and it contains the absolutePath to the file file:///var/mobile/Containers/Data/Application/9C4D58FD-C817-47CB-A418-04BA30863C24/Library/Caches/Camera/133F8168-356A-436C-B7AF-557FA1C4F68F.jpg"
After that, I try to make the CIImage by using let image = CIImage.init(contentsOf: imageURL);
But when I try to print image, it returns nil. I searched a bit and tried using guard but no luck. Any help will be appreciated. Thanks in advance.
The CIImage does not create the image from a server URL. You can achieve this by first downloading the file at local location. And use the local file URL to create CIImage and then set it. The referred question/answer achieves the same, please check Loading/Downloading image from URL on Swift
If you still face issues do let me know. All the best!

String.init(contentsOfFile:) replacement for Linux?

After deploying my Swift 3 app to Heroku, it crashed with the following error:
fatal error: init(contentsOfFile:usedEncoding:) is not yet implemented: file Foundation/NSString.swift, line 1255
What can I use instead of String.init(contentsOfFile:) on Ubuntu?
Seeing the latest source code of Swift Standard Library, String.init(contentsOfFile:) internally calls NSString.init(contentsOfFile:usedEncoding:). (NSStringAPI.swift)
And the Linux version of NSString.init(contentsOfFile:usedEncoding:), as you see, is not implemented yet. (NSString.swift)
Seems NSString.init(contentsOfFile:encoding:) is already implemented and String.init(contentsOfFile:encoding:) calls it. So, if you know the encoding of the file, use String.init(contentsOfFile:encoding:) like:
let fileContent = try? String(contentsOfFile: filePath, encoding: .utf8)
If you do not know the string encoding of the file, you may need to implement the functionality by yourself.

VLCKit Subtitle not showing

I use VLCKit in swift, so i created customize video player view
and i have external subtitles for movies from links, i read files from server and convert it so string
do {
let text = try NSString(contentsOfURL: NSURL(string: self.subtitleUrl)!, encoding: NSUTF8StringEncoding)
self.mediaPlayer.openVideoSubTitlesFromFile(text as String)
}catch {
print("Error")
}
and i called a function named "openVideoSubTitlesFromFile"
in player but not working
anyone can give me a solution
This method (which will be deprecated in the next major release of VLCKit) does only accept local file paths, no remote URLs. You need to download the subtitles file, cache it locally and provide the path of the stored file to the method. Additionally, you may only use this method after playback started.