How to save audio recorded file by user input using Swift?












3














My scenario, I am trying to create audio record and save file into iPhone documentdirectory.



I have done recording functionality but I need to implement save file name based on user input. After audio record, If user click save button I am asking file name to user by alertviewcontroller with textfield.



Here, my audio file saving by static file name(audio.m4a) because inside viewdidload I implemented save document directory code but I dont know how to implement save file name based on user input within save action.



override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
try? session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try? session.overrideOutputAudioPort(.speaker)
try? session.setActive(true)
if let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let baseComponents = [basePath,"audio.m4a"]
if let audioURL = NSURL.fileURL(withPathComponents: baseComponents) {
var settings: [String: Any] = [:]
self.audioURL = audioURL
settings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC)
settings[AVSampleRateKey] = 44100.0
settings[AVNumberOfChannelsKey] = 2
audioRecorder = try? AVAudioRecorder(url: audioURL, settings: settings)
audioRecorder?.prepareToRecord()
}
}
}

@IBAction func record_click(_ sender: Any) {
if let audioRecorder = self.audioRecorder {
if (audioRecorder.isRecording) {
audioRecorder.stop()
} else {
audioRecorder.record()
}
}
}

// Within below action I am calling alertview with textfield for asking file name
@IBAction func save_click(_ sender: Any) {
self.savefileAlertView()
}









share|improve this question
























  • Show your attempt to prompt the user and to use that entered filename.
    – rmaddy
    Nov 11 at 16:45










  • Sorry Can't able to understand. Could you please provide some code @rmaddy
    – pastelsdev
    Nov 11 at 16:47










  • I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
    – rmaddy
    Nov 11 at 16:48










  • Sure I will. Thank you.@rmaddy
    – pastelsdev
    Nov 11 at 16:49
















3














My scenario, I am trying to create audio record and save file into iPhone documentdirectory.



I have done recording functionality but I need to implement save file name based on user input. After audio record, If user click save button I am asking file name to user by alertviewcontroller with textfield.



Here, my audio file saving by static file name(audio.m4a) because inside viewdidload I implemented save document directory code but I dont know how to implement save file name based on user input within save action.



override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
try? session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try? session.overrideOutputAudioPort(.speaker)
try? session.setActive(true)
if let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let baseComponents = [basePath,"audio.m4a"]
if let audioURL = NSURL.fileURL(withPathComponents: baseComponents) {
var settings: [String: Any] = [:]
self.audioURL = audioURL
settings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC)
settings[AVSampleRateKey] = 44100.0
settings[AVNumberOfChannelsKey] = 2
audioRecorder = try? AVAudioRecorder(url: audioURL, settings: settings)
audioRecorder?.prepareToRecord()
}
}
}

@IBAction func record_click(_ sender: Any) {
if let audioRecorder = self.audioRecorder {
if (audioRecorder.isRecording) {
audioRecorder.stop()
} else {
audioRecorder.record()
}
}
}

// Within below action I am calling alertview with textfield for asking file name
@IBAction func save_click(_ sender: Any) {
self.savefileAlertView()
}









share|improve this question
























  • Show your attempt to prompt the user and to use that entered filename.
    – rmaddy
    Nov 11 at 16:45










  • Sorry Can't able to understand. Could you please provide some code @rmaddy
    – pastelsdev
    Nov 11 at 16:47










  • I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
    – rmaddy
    Nov 11 at 16:48










  • Sure I will. Thank you.@rmaddy
    – pastelsdev
    Nov 11 at 16:49














3












3








3







My scenario, I am trying to create audio record and save file into iPhone documentdirectory.



I have done recording functionality but I need to implement save file name based on user input. After audio record, If user click save button I am asking file name to user by alertviewcontroller with textfield.



Here, my audio file saving by static file name(audio.m4a) because inside viewdidload I implemented save document directory code but I dont know how to implement save file name based on user input within save action.



override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
try? session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try? session.overrideOutputAudioPort(.speaker)
try? session.setActive(true)
if let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let baseComponents = [basePath,"audio.m4a"]
if let audioURL = NSURL.fileURL(withPathComponents: baseComponents) {
var settings: [String: Any] = [:]
self.audioURL = audioURL
settings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC)
settings[AVSampleRateKey] = 44100.0
settings[AVNumberOfChannelsKey] = 2
audioRecorder = try? AVAudioRecorder(url: audioURL, settings: settings)
audioRecorder?.prepareToRecord()
}
}
}

@IBAction func record_click(_ sender: Any) {
if let audioRecorder = self.audioRecorder {
if (audioRecorder.isRecording) {
audioRecorder.stop()
} else {
audioRecorder.record()
}
}
}

// Within below action I am calling alertview with textfield for asking file name
@IBAction func save_click(_ sender: Any) {
self.savefileAlertView()
}









share|improve this question















My scenario, I am trying to create audio record and save file into iPhone documentdirectory.



I have done recording functionality but I need to implement save file name based on user input. After audio record, If user click save button I am asking file name to user by alertviewcontroller with textfield.



Here, my audio file saving by static file name(audio.m4a) because inside viewdidload I implemented save document directory code but I dont know how to implement save file name based on user input within save action.



override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
try? session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try? session.overrideOutputAudioPort(.speaker)
try? session.setActive(true)
if let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let baseComponents = [basePath,"audio.m4a"]
if let audioURL = NSURL.fileURL(withPathComponents: baseComponents) {
var settings: [String: Any] = [:]
self.audioURL = audioURL
settings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC)
settings[AVSampleRateKey] = 44100.0
settings[AVNumberOfChannelsKey] = 2
audioRecorder = try? AVAudioRecorder(url: audioURL, settings: settings)
audioRecorder?.prepareToRecord()
}
}
}

@IBAction func record_click(_ sender: Any) {
if let audioRecorder = self.audioRecorder {
if (audioRecorder.isRecording) {
audioRecorder.stop()
} else {
audioRecorder.record()
}
}
}

// Within below action I am calling alertview with textfield for asking file name
@IBAction func save_click(_ sender: Any) {
self.savefileAlertView()
}






ios swift avfoundation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:53

























asked Nov 11 at 16:23









pastelsdev

708




708












  • Show your attempt to prompt the user and to use that entered filename.
    – rmaddy
    Nov 11 at 16:45










  • Sorry Can't able to understand. Could you please provide some code @rmaddy
    – pastelsdev
    Nov 11 at 16:47










  • I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
    – rmaddy
    Nov 11 at 16:48










  • Sure I will. Thank you.@rmaddy
    – pastelsdev
    Nov 11 at 16:49


















  • Show your attempt to prompt the user and to use that entered filename.
    – rmaddy
    Nov 11 at 16:45










  • Sorry Can't able to understand. Could you please provide some code @rmaddy
    – pastelsdev
    Nov 11 at 16:47










  • I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
    – rmaddy
    Nov 11 at 16:48










  • Sure I will. Thank you.@rmaddy
    – pastelsdev
    Nov 11 at 16:49
















Show your attempt to prompt the user and to use that entered filename.
– rmaddy
Nov 11 at 16:45




Show your attempt to prompt the user and to use that entered filename.
– rmaddy
Nov 11 at 16:45












Sorry Can't able to understand. Could you please provide some code @rmaddy
– pastelsdev
Nov 11 at 16:47




Sorry Can't able to understand. Could you please provide some code @rmaddy
– pastelsdev
Nov 11 at 16:47












I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
– rmaddy
Nov 11 at 16:48




I'm asking you to provide your code. Show your attempt to display the alert view prompting the user for a filename. Then show your attempt to make use of that filename to save the audio file. Then clearly explain what help you need.
– rmaddy
Nov 11 at 16:48












Sure I will. Thank you.@rmaddy
– pastelsdev
Nov 11 at 16:49




Sure I will. Thank you.@rmaddy
– pastelsdev
Nov 11 at 16:49












1 Answer
1






active

oldest

votes


















0














If you want to set name of file which has been already saved you can rename it.



You can create this function



func renameAudio(newTitle: String) {
do {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("audio.m4a")
let destinationPath = documentDirectory.appendingPathComponent("(newTitle).m4a")
try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
print(error)
}
}


And in your alert controller use it and as parameter pass text of text field inside alert.






share|improve this answer





















  • thank you @Robert Dresler. I will try your answer and update you here.
    – pastelsdev
    Nov 12 at 3:12












  • renaming I think it's not good idea. Do you have anyother idea?
    – pastelsdev
    Nov 14 at 17:47













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250732%2fhow-to-save-audio-recorded-file-by-user-input-using-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you want to set name of file which has been already saved you can rename it.



You can create this function



func renameAudio(newTitle: String) {
do {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("audio.m4a")
let destinationPath = documentDirectory.appendingPathComponent("(newTitle).m4a")
try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
print(error)
}
}


And in your alert controller use it and as parameter pass text of text field inside alert.






share|improve this answer





















  • thank you @Robert Dresler. I will try your answer and update you here.
    – pastelsdev
    Nov 12 at 3:12












  • renaming I think it's not good idea. Do you have anyother idea?
    – pastelsdev
    Nov 14 at 17:47


















0














If you want to set name of file which has been already saved you can rename it.



You can create this function



func renameAudio(newTitle: String) {
do {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("audio.m4a")
let destinationPath = documentDirectory.appendingPathComponent("(newTitle).m4a")
try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
print(error)
}
}


And in your alert controller use it and as parameter pass text of text field inside alert.






share|improve this answer





















  • thank you @Robert Dresler. I will try your answer and update you here.
    – pastelsdev
    Nov 12 at 3:12












  • renaming I think it's not good idea. Do you have anyother idea?
    – pastelsdev
    Nov 14 at 17:47
















0












0








0






If you want to set name of file which has been already saved you can rename it.



You can create this function



func renameAudio(newTitle: String) {
do {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("audio.m4a")
let destinationPath = documentDirectory.appendingPathComponent("(newTitle).m4a")
try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
print(error)
}
}


And in your alert controller use it and as parameter pass text of text field inside alert.






share|improve this answer












If you want to set name of file which has been already saved you can rename it.



You can create this function



func renameAudio(newTitle: String) {
do {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("audio.m4a")
let destinationPath = documentDirectory.appendingPathComponent("(newTitle).m4a")
try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
print(error)
}
}


And in your alert controller use it and as parameter pass text of text field inside alert.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:31









Robert Dresler

3,7351526




3,7351526












  • thank you @Robert Dresler. I will try your answer and update you here.
    – pastelsdev
    Nov 12 at 3:12












  • renaming I think it's not good idea. Do you have anyother idea?
    – pastelsdev
    Nov 14 at 17:47




















  • thank you @Robert Dresler. I will try your answer and update you here.
    – pastelsdev
    Nov 12 at 3:12












  • renaming I think it's not good idea. Do you have anyother idea?
    – pastelsdev
    Nov 14 at 17:47


















thank you @Robert Dresler. I will try your answer and update you here.
– pastelsdev
Nov 12 at 3:12






thank you @Robert Dresler. I will try your answer and update you here.
– pastelsdev
Nov 12 at 3:12














renaming I think it's not good idea. Do you have anyother idea?
– pastelsdev
Nov 14 at 17:47






renaming I think it's not good idea. Do you have anyother idea?
– pastelsdev
Nov 14 at 17:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250732%2fhow-to-save-audio-recorded-file-by-user-input-using-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini