每次我在 xamarin 中保存记录时重命名记录的文件 [英] Rename a recorded file every time I save a record in xamarin
问题描述
我正在使用此代码保存我的记录:
<预><代码>字符串路径 = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;公共字符串文件名 { 获取;放;}fileName = Path.Combine(path, "sample.wav");如果 (!recorder.IsRecording){recorder.StopRecordingOnSilence = TimeoutSwitch.IsToggled;//开始录制var audioRecordTask = await recorder.StartRecording();BtnDoneRec.IsEnabled = false;等待音频记录任务;RecEditor.IsEnabled = true;BtnDoneRec.IsEnabled = false;PlayButton.IsEnabled = true;var filePath = recorder.GetAudioFilePath();如果(文件路径!= null){var stream = recorder.GetAudioFileStream();使用 (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)){stream.CopyTo(fileStream);}}}别的{//停止录音...等待 recorder.StopRecording();}我希望我的记录有一个特定的名称,并用我的 RecEditor 标记
<预><代码>使用 (var streamReader = new StreamReader(fileName)){File.Move("sample.wav", RecEditor.Text + ".wav");}因此,每次单击保存按钮时,它都会将sample.wav"重命名为RecEditor text.wav".但是当我点击保存时,它给了我这个记录
<预><代码>System.IO.FileNotFoundException: '找不到文件'/sample.wav'.'记录存储在/storage/emulated/0/sample.wav
sample.wav 是在我的设备中创建的,但我不知道为什么它给我找不到文件 '/sample.wav'."错误.我在这里做错了什么?
我相信您正在寻找的是这样的:
if(File.Exists(fileName)){var newFileName = Path.Combine(path, $"{RecEditor.Text}.wav");File.Move(fileName, newFileName);}
您不需要像现在这样打开一个新的 Stream
.此外,您不仅需要输入文件名,还需要输入完整的文件路径.
在使用 newfileName
RecEditor.Text
不为空希望这会有所帮助.-
I am saving my records using this code:
string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
public string fileName { get; set; }
fileName = Path.Combine(path, "sample.wav");
if (!recorder.IsRecording)
{
recorder.StopRecordingOnSilence = TimeoutSwitch.IsToggled;
//Start recording
var audioRecordTask = await recorder.StartRecording();
BtnDoneRec.IsEnabled = false;
await audioRecordTask;
RecEditor.IsEnabled = true;
BtnDoneRec.IsEnabled = false;
PlayButton.IsEnabled = true;
var filePath = recorder.GetAudioFilePath();
if (filePath != null)
{
var stream = recorder.GetAudioFileStream();
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
}
}
}
else
{
//stop recording ...
await recorder.StopRecording();
}
I want my record to have a specific name which is labeled with my RecEditor
using (var streamReader = new StreamReader(fileName))
{
File.Move("sample.wav", RecEditor.Text + ".wav");
}
So it will rename "sample.wav" to "RecEditor text.wav" every time I click my save button. But when I click save, it gives me this record
System.IO.FileNotFoundException: 'Could not find file '/sample.wav'.'
The record is stored in /storage/emulated/0/sample.wav
The sample.wav is created in my device but I don't know why it give me 'Could not find file '/sample.wav'.' error. What am i doing wrong here?
I believe that what you're looking is something like this:
if(File.Exists(fileName))
{
var newFileName = Path.Combine(path, $"{RecEditor.Text}.wav");
File.Move(fileName, newFileName);
}
You don't need to open a new Stream
as you are doing. Also, you need to put the full file path not only the file name.
You might want to validate that RecEditor.Text
is not empty before using its value for the newfileName
Hope this helps.-
这篇关于每次我在 xamarin 中保存记录时重命名记录的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!