WEBRTC How to capture video from camera library? - swift

Can't load video from a UIImagePickerController using WebRTC.
With saved in-app Bundle file, it works, but if I use UIImagePickerController
UIImagePickerControllerDelegate.imagePickerController(_:didFinishPickingMediaWithInfo:))
so I use mediaInfo like this:
(info[.mediaURL] as! URL).path
This code I use to start capturing a video file
public func startCaptureLocalVideoFile(name: String, renderer: RTCVideoRenderer) {
print("startCaptureLocalVideoFile")
stopLocalCapture()
localRenderer = renderer
videoCapturer = RTCFileVideoCapturer(delegate: videoSource)
guard let capturer = videoCapturer as? RTCFileVideoCapturer else {
print("WebRTCService can't get capturer")
return
}
capturer.startCapturing(fromFileNamed: name) { error in
print("startCapturing error ", error)
return
}
localVideoTrack?.add(renderer)
}
so I get this media info:
info [__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerMediaURL): file:///private/var/mobile/Containers/Data/PluginKitPlugin/5F7A4469-5006-4590-8F59-396CD86A083B/tmp/trim.B46C5878-BAF2-432B-B627-9787D74CE7B0.MOV, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerMediaType): public.movie, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.MOV?id=33EECFB7-514A-435A-AA19-26A055FB9F06&ext=MOV]
and this error:
startCapturing error Error Domain=org.webrtc.RTCFileVideoCapturer Code=2001 "(null)" UserInfo={NSUnderlyingError=File /private/var/mobile/Containers/Data/PluginKitPlugin/5F7A4469-5006-4590-8F59-396CD86A083B/tmp/trim.B46C5878-BAF2-432B-B627-9787D74CE7B0.MOV not found in bundle}
Seems like it works with Bundle.main only, but we can't write to it.
Am I doing it right? Maybe there is another way to accomplish this?
Thanks for the help!

Related

Swift AVMIDIPlayer play MIDI with sound bank doesn't play

I'm trying to play a MIDI file with a sound font but it doesn't play. On simulator it always do a standard font but it's correct in iOS device it gives me an error
the error
2023-02-19 06:27:07.545221+0100 BlindMusic[596:35171] DLSBankManager.cpp:99 BankEntry::LoadInstrument: Unable to find patch 0 bank 0x78/0
2023-02-19 06:27:07.545255+0100 BlindMusic[596:35171] [default] DLSBankManager.cpp:100 about to throw -10851: LoadInstrument: Failed to load patch from bank
2023-02-19 06:27:07.545310+0100 BlindMusic[596:35171] GlobalState.cpp:486 DLS/SF2 bank load failed
and here is the code very simple
in the class I declare
class MusicScoreViewModel: ObservableObject {
let soundBankURL: URL = Bundle.main.url(forResource: "Full Grand Piano" , withExtension: "sf2")!
let midiSoundURL: URL = Bundle.main.url(forResource: "for_elise_by_beethoven" , withExtension: "mid")!
var midiPlayer: AVMIDIPlayer!
init() {
}
func playMidi() {
do {
midiPlayer = try AVMIDIPlayer(contentsOf: midiSoundURL, soundBankURL: soundBankURL)
midiPlayer.prepareToPlay()
midiPlayer.play {
print("finished playing")
}
} catch {
print("could not create MIDI player")
}
}
}
I appreciate any help thank you

How to get image url from PHAsset for iOS 13

As no longer PHImageFileURLKey is given, how i can get image reference url for picked image!
Using PHImageFileUTIKey key i was able to get the file name.
I tried to play around PHAssetResource.assetResources(for: assets[0]) but still there is no luck!
asset.requestContentEditingInput(with: nil, completionHandler: { (input, info) in
if let input = input {
print(input.fullSizeImageURL) // file:///xxx
}
})
this is how i was able to get full imageURL.

How to print the current local network name in swift

I am creating an iOS app which displays the current local network name at the top of the screen, and so forth. I am trouble-shooting different ways to display this but I can't manage the current program. Can someone help me out?
I've looked at several GitHub, stack overflow, and youtube comments about this, but nome of them worked.
In the current Xcode I'm using which is Xcode(10.4.2) I'm using a label(correct me if I should use something else) to display the current Wifi named --> (WiFi: ......)
Please don't test on the simulator, use the iphone for testing.
Import SystemConfiguration :
import SystemConfiguration.CaptiveNetwork
In ViewDidLoad :
let wifiName = getWiFiName()
print("Wifi: \(String(describing: wifiName))")
Function :
func getWiFiName() -> String? {
var serviceSetIdentifier:String?
if let interfaces = CNCopySupportedInterfaces() as Array? {
interfaces.forEach { interface in
guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? else { return }
serviceSetIdentifier = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
return serviceSetIdentifier
}

Firebasestorage "downloadURL" function is not working properly, sometimes it works randomly

The below swift code is written to upload an image to firebase storage. Earlier it was working fine. But now onwards it works randomly. I waited 30mins to upload an image but didn't reach to success nor even in error.
I have tried different code to upload an image. Also, search a lot to analyze the problem. But sometimes it works randomly. I am not able to identify why this is happening.
To check if the image is uploading or not, I just put an observer to check the progress of an upload task, uploading works perfectly, but it didn't reach to completion block. I tried to generate download URL too after upload task done but it also didn't reach to completion block.
let storageRef = Storage.storage().reference().child("abc").child("myImage1234")
storageRef.putData(imageData, metadata: nil, completion: { (storageMetaData, error) in
if let error = error {
print(error.localizedDescription)
} else {
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error?.localizedDescription as Any)
return
}
if let photoUrl = url?.absoluteString {
self.imageUpload()
}
})
}
})
I am really stuck at this point. Any help would be appreciated.

Finder Share Extension: Getting a preview image from NSItemProvider

I'm creating a Finder Share extension to be used when selecting a file. I'd like to show an image preview of the file that you are sharing (like you see in the Messages and Twitter Finder Share extensions, for example).
In the loadView method of the view controller for my share extension, I'm doing this:
let item = self.extensionContext!.inputItems[0] as! NSExtensionItem
if let attachments = item.attachments as? [NSItemProvider] {
if let attachment = attachments.first {
attachment.loadPreviewImage(options: nil, completionHandler: { (item, error) in
if error != nil {
//handle error...
} else if let img = item as? NSImage {
//put image preview in my "share sheet"...
}
})
}
}
The problem is that when I keep hitting the error condition. The error that I'm getting is:
Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load preview." UserInfo={NSLocalizedDescription=Cannot load preview.}
For what it's worth, I am able to call attachment.loadItem() successfully and work with the file. But I don't really care to do anything with the file itself at this point, I just want a thumbnail image that represents the file which theoretically this method should give me...
Any ideas?
I am in the same situation. After read the manual, I think here is the reason.
"Loads the preview image for the item that is represented by the item provider."
However, it is not forced that the item provider must provides a preview. So the error simply says that there is no preview that the item provider can provide. You should do it yourself.