I am following the doc here: https://docs.agora.io/en/Voice/rtc_recording_apple?platform=iOS and implementing a basic recording. This is my code:
func startRecording(){
let filename = getDocumentsDirectory().appendingPathComponent("\(APP_NAME)\(now()).WAV")
let str = String(describing: filename)
self.recordingPath = str
agoraKit?.startAudioRecording(str, quality: .high)
}
func stopRecording(){
agoraKit?.stopAudioRecording()
// get audio file
guard let audioUrl = URL(string: self.recordingPath) as? URL else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0 ) { [weak self] in
// getdata
do {
let myData = try Data(contentsOf: audioUrl)
print(myData.count, myData)
} catch {
print(error)
}
}
}
private func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
But I am getting error: The file “Whisper1608949569.WAV” couldn’t be opened because there is no such file
Full message:
file:///var/mobile/Containers/Data/Application/1F682ABD-153C-4DFD-BFF4-
02C1CE6F9A4C/Documents/Whisper1608949569.WAV
Error Domain=NSCocoaErrorDomain Code=260 "The file “Whisper1608949569.WAV” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/1F682ABD-153C-4DFD-BFF4-02C1CE6F9A4C/Documents/Whisper1608949569.WAV, NSUnderlyingError=0x281e33f60 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Am I not accessing the file correctly?
This is how I have initialized the agora client:
self.agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: AppID, delegate: self)
agoraKit?.delegate = self
agoraKit?.enableWebSdkInteroperability(true)
// sample loudest speaker every second
agoraKit?.enableAudioVolumeIndication(1000, smooth: 3, report_vad: true)
agoraKit?.enableAudio()
// config for livecast to start
agoraKit?.setChannelProfile(.liveBroadcasting)
// set framrate and HD/SD
agoraKit?.setVideoEncoderConfiguration( CONFIG_PRODUCTION )
//agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
I just checked out the documentation referenced from the doc you're using and it says the method startAudioRecording(filepath, quality: quality) is now deprecated, and you should instead use this method with the additional sampleRate parameter:
https://docs.agora.io/en/Voice/API%20Reference/oc/Classes/AgoraRtcEngineKit.html#//api/name/startAudioRecording:sampleRate:quality:
Also check that the returned value of startAudioRecording and stopAudioRecording returns 0, meaning success.
If your channel name contains special characters (colons, slashes) recording will silently fail and no file will be produced.
It seems Agora uses the channel name when creating the temporary file.
Related
I am trying to get the FileAttributesKey using FileManager in iOS using Swift. When I try that, I am getting the following error"
Error Domain=NSCocoaErrorDomain Code=260 "The file “Personal%20Narrative%20Essay.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///private/var/mobile/Library/Mobile%20Documents/com~apple~TextEdit/Documents/Personal%20Narrative%20Essay.txt, NSUnderlyingError=0x283e720a0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
It says that the file doesn't exist but I know that it does exist because it is appearing in the UIDocumentPicker and I am able to get the contents of the file using try! Data(contentsOf: URL).
My code for getting the attributes of the file is:
func processURL(url: URL) -> Bool {
let newPath = url.absoluteString
print(newPath)
do {
let attributes = try FileManager.default.attributesOfItem(atPath: newPath)
let fileSize = attributes[FileAttributeKey.size]
print("Calculated File Size: \(fileSize!)")
return true
}
catch {
print(error)
self.isShowingSpinner = false
self.isShowingURLErrorAlert = true
return false
}
return true
}
I just need to be able to get the file size for now which I am doing using the above function. I do not understand if I am doing something wrong. Thanks in advance for the help.
I could reproduce your issue with a simple Playground where I placed a resource called "hello.text".
Here are the results:
let filePath = Bundle.main.path(forResource: "hello", ofType: "txt")!
// Prints as /var/folders/blah-blah-blah/hello.txt
let fileURL = Bundle.main.url(forResource: "hello", withExtension: "txt")!
// Prints as file:///var/folders/blah-blah-blah/hello.txt
The issue is that the file:// prefix is specific to file's URL and is not expected for path arguments of type String.
try FileManager.default.attributesOfItem(atPath: fileURL.absoluteString) // ❌
try FileManager.default.attributesOfItem(atPath: fileURL.path) // ✅
I'm making a game on iPad (v10.2.1) with Xcode(version 8.2.1) and while I'm trying to save some data in documents folder in the sandbox, I found that all ways I tried failed: createDirectory and createFile(using file manager), and writeToUrl(using NSData). I ran the program on 2 iPads and 1 iPhone and no success.
I was able to read from a plist I created, and convert it to a dictionary, using the function convertDataFrom().
I'm new to data persistence or file manipulation and my game stuck here. would appreciate any help!
func loadData()->NSDictionary{
let fm = FileManager()
let sourceUrl = Bundle.main.url(forResource: "Data", withExtension: "plist")!
var appSupportDir = fm.urls(for: .documentDirectory, in: .userDomainMask).first! as? NSURL
appSupportDir = appSupportDir!.appendingPathComponent("UserData", isDirectory: true) as NSURL?
let urlForSave = (appSupportDir!.appendingPathComponent("Data.plist"))!
//load data, or if file doesn't exist yet, create one
var isDirectory: ObjCBool = false
if fm.fileExists(atPath: urlForSave.absoluteString, isDirectory: &isDirectory) {
let data = convertDataFrom(url: urlForSave)
print("data file exists")
print(isDirectory)
return data as NSDictionary
} else {
do { try fm.createDirectory(atPath: appSupportDir!.absoluteString!, withIntermediateDirectories: true, attributes: nil) }catch let Error {
print("create directory failed")
print(Error)
}
let originalData = NSData.init(contentsOf: sourceUrl)
print("\(urlForSave)")
do { try originalData?.write(toFile: urlForSave.absoluteString, options: .atomic) } catch let Error {
print(Error)
}
if (!fm.createFile(atPath: urlForSave.absoluteString, contents: originalData as Data?, attributes: nil)) {
fatalError("file creation failed")
}
let data = convertDataFrom(url: urlForSave)
print("\(data)")
print(isDirectory)
return data as NSDictionary
}
}
func convertDataFrom(url: URL)->Dictionary<String, Any> {
let dictionary = NSDictionary.init(contentsOf: url) as! Dictionary<String, Any>
return dictionary
}
Error messages for "createDirectory": Error Domain=NSCocoaErrorDomain
Code=513 "You don’t have permission to save the file “UserData” in the
folder “Documents”."
UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/,
NSUnderlyingError=0x174053f50 {Error Domain=NSPOSIXErrorDomain Code=1
"Operation not permitted"}}
file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/Data.plist
and for "createFile" (fatalError if createFile fails) Error
Domain=NSCocoaErrorDomain Code=4 "The file “Data.plist” doesn’t
exist."
UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/Data.plist,
NSUnderlyingError=0x174055bd0 {Error Domain=NSPOSIXErrorDomain Code=2
"No such file or directory"}} fatal error: file creation failed: file
/Users/chanwu/Desktop/Math_DownStairs/Math_DownStairs/
Your code checks to see if urlForSave (the final file) exists, and if it doesn't, you then try to create the UserData directory.
If the final file doesn't exist, but the UserData directory does, I would expect the exact outcome you describe.
You should probably change your test to see if the UserData directory exists instead of testing for the final file. That way if the directory doesn't exist you'll create it, and then call write(toFile:), which will create the file if it doesn't exist.
Alamofire 3.5.0, Swift2.2
I'm downloading ZIP files with the Alamofire download method, I've noticed that when I'm starting download process and apps goes background than the download fails with following error:
----------------------
error Optional(Error Domain=NSCocoaErrorDomain Code=4
"“CFNetworkDownload_pZ56nc.tmp” couldn’t be moved to “courses”
because either the former doesn't exist, or the folder containing
the latter doesn't exist." UserInfo=
{NSSourceFilePathErrorKey=
/private/var/mobile/Containers/Data/Application/[UUID]/tmp/CFNetworkDownload_pZ56nc.tmp,
NSUserStringVariant=(
Move
),
NSDestinationFilePath=
/var/mobile/Containers/Data/Application/[UUID]/Library/courses/course_302.zip,
NSFilePath=
/private/var/mobile/Containers/Data/Application/[UUID]/tmp/CFNetworkDownload_pZ56nc.tmp,
NSUnderlyingError=0x13f52f990 {Error Domain=NSPOSIXErrorDomain
Code=2 "No such file or directory"}})
----------------------
this is the code to download a file:
//...
var userLibraryPath:String = {
return NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
}()
//...
let _coursePath:NSURL = NSURL(string: "file://\(userLibraryPath)/)")!
//...
let zipURL = _coursePath.URLByAppendingPathComponent("course_\(courseId).zip")
//if file exists destroy it
if let zipPath = zipURL?.path where NSFileManager.defaultManager().fileExistsAtPath(zipPath) {
do {
try NSFileManager.defaultManager().removeItemAtPath(zipPath)
} catch let error as NSError {
print(error)
}
}
//
let downloadRequest = Alamofire.download(Router.download(courseId), destination: { (url:NSURL, urlResponse:NSHTTPURLResponse) -> NSURL in
//
return zipURL!
//
}).progress({ (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
//
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
dispatch_async(GlobalMainQueue, {
self.notifyDownloadProgress(courseId, progress: progress)
})
}).response(completionHandler: { (request:NSURLRequest?, response:NSHTTPURLResponse?, data:NSData?, error:NSError?) in
self.removeFromQueue(courseId)
print("response")
print("----------------------")
print("error \(error)")
print("----------------------")
//here I would try to extract it
})
UPDATE I've just tested on iPhone 5 fresh install of the app and it doesn't have to go to background (e.g. via home button) to fail, it fails on the very first load (and any subsequent) untill after the app is killed and reopened.
Why is the "/private" bit added to the path? What am I doing wrong here?
And indeed it was a "No such file or directory" error.
When I've added:
//
let downloadRequest = Alamofire.download(Router.download(courseId), destination: { (url:NSURL, urlResponse:NSHTTPURLResponse) -> NSURL in
let course = zipURL!.URLByDeletingLastPathComponent!.path!
let fm = NSFileManager.defaultManager()
var isDir:ObjCBool = false
if(fm.fileExistsAtPath(path, isDirectory: &isDir) == false){
//doesnt exist
do {
try fm.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
//
print(error)
}
}
return zipURL!
//
})
After transferring a file from iPhone to Apple Watch I get the Error
Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"
What do I do wrong? These are the code snippets:
iPhone ViewController
func makeAction () {
let url = NSURL.fileURL(withPath: fileArray[0].object(at: 2) as! String)
var applicationDict = Dictionary<String, Array<AnyObject>>()
applicationDict["fileArray"] = fileArray
WCSession.default().transferFile(url, metadata: applicationDict)
}
Watch InterfaceController
func session(_ session: WCSession, didReceive file: WCSessionFile) {
DispatchQueue.main.async(execute: { () -> Void in
print("RECEIVED")
var applicationDict = Dictionary<String, Array<AnyObject>>()
applicationDict = file.metadata as! Dictionary<String, Array<AnyObject>>
self.fileArray = applicationDict["fileArray"]!
self.fileList = self.fileArray
let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let tempDocsDir = dirPaths[0] as String
let docsDir = tempDocsDir.appending("/")
let filemgr = FileManager.default
do {
let fileName = self.fileArray[0].object(at: 1) as! String
try filemgr.moveItem(atPath: file.fileURL.path, toPath: docsDir + fileName)
} catch let error as NSError {
print("Error moving file: \(error.description)")
}
self.loadTableData()
})
}
Full Error Message
Error moving file: Error Domain=NSCocoaErrorDomain Code=4
"“5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3” couldn’t be moved to “Documents” because either the former doesn't
exist, or the folder containing the latter doesn't exist."
UserInfo={NSSourceFilePathErrorKey=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/Inbox/com.apple.watchconnectivity/FCE7E6CB-2452-4E0A-9AFF-F5B3A51A0DE8/Files/0B96CCB0-A2E1-418B-9859-97C22238A5F5/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSUserStringVariant=(
Move ), NSFilePath=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/Inbox/com.apple.watchconnectivity/FCE7E6CB-2452-4E0A-9AFF-F5B3A51A0DE8/Files/0B96CCB0-A2E1-418B-9859-97C22238A5F5/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSDestinationFilePath=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSUnderlyingError=0x7b776110 {Error Domain=NSPOSIXErrorDomain Code=2
"No such file or directory"}}
The documentation for didReceiveFile notes:
File: The object containing the URL of the file and any additional information. If you want to keep the file referenced by this parameter, you must move it synchronously to a new location during your implementation of this method. If you do not move the file, the system deletes it after this method returns.
So make sure to not async in this method before moving the file to a location your app has access to.
Alright, got it. putting this in an async dispatch was wrong. Without ist -> work perfectly. Please go ahead, nothing to see here :)
I am using a sequence of three functions to play a video using AVPlayerViewController on iOS :
first I add the video files to my project by copying them to my project root folder and then drag and drop them in xcode project root
First Function that copies the file to the application's document folder :
a. Get the document path
class func getPath(fileName: String) -> String{
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent(fileName)
return fileURL.path!
}
b. COPY THE FILE
class func copyFile(fileName: NSString) {
let dbPath: String = getPath(fileName as String)
let fileManager = NSFileManager.defaultManager()
if !fileManager.fileExistsAtPath(dbPath) {
let documentsURL = NSBundle.mainBundle().resourceURL
print(documentsURL)
let fromPath = documentsURL!.URLByAppendingPathComponent(fileName as String)
var error : NSError?
do {
try fileManager.copyItemAtPath(fromPath.path!, toPath: dbPath)
} catch let error1 as NSError {
print(error1.localizedDescription)
}
}
}
In my app delegate application(...) function I call Util.copyFile("EVGS_VID1.MOV")
in a view controller I have a function playVideo(fileName:String) :
func playVideo(fileName:String){
let videoPath:String = Util.getPath(fileName)
var fileSize : UInt64 = 0
//Check if file exists
do {
let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(videoPath)
if let _attr = attr {
fileSize = _attr.fileSize();
}
} catch {
print("Error: \(error)")
}
let playerViewController:AVPlayerViewController = AVPlayerViewController()
var url:NSURL = NSURL(fileURLWithPath: videoPath,isDirectory: false)
let player = AVPlayer(URL: url)
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
but when I call the playVideo in my view controller it shows the player with no video and I got an exception :
Error: Error Domain=NSCocoaErrorDomain Code=260 "The file “EVGS_VID1.mov” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/61169756-1DCE-49B1-99F4-27854DDF4929/Documents/EVGS_VID1.mov, NSUnderlyingError=0x1572bbbf0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
I am using the same functions copyFile and getPath to copy SQLite database and it's working