【问题标题】:Rename a recorded file every time I save a record in xamarin每次我在 xamarin 中保存记录时重命名记录的文件
【发布时间】:2020-02-25 13:37:18
【问题描述】:

我正在使用此代码保存我的记录:


        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();                    
                }

我希望我的记录有一个带有我的 RecEditor 标记的特定名称


         using (var streamReader = new StreamReader(fileName))
         {
            File.Move("sample.wav", RecEditor.Text + ".wav");
         }

因此,每次我单击保存按钮时,它都会将“sample.wav”重命名为“RecEditor text.wav”。 但是当我点击保存时,它给了我这条记录


System.IO.FileNotFoundException: 'Could not find file '/sample.wav'.'


记录存储在/storage/emulated/0/sample.wav

sample.wav 是在我的设备中创建的,但我不知道为什么它给我“找不到文件 '/sample.wav'。”错误。我在这里做错了什么?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我相信你正在寻找的是这样的:

    if(File.Exists(fileName))
    {    
        var newFileName = Path.Combine(path, $"{RecEditor.Text}.wav");
        File.Move(fileName, newFileName);
    }
    

    您不需要像现在这样打开一个新的Stream。此外,您需要放置完整的文件路径,而不仅仅是文件名。

    在将 newfileName 的值用于 newfileName 之前,您可能需要验证 RecEditor.Text 不为空

    希望这会有所帮助。-

    【讨论】:

    • 这就是我要找的东西 :D :D 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多