【问题标题】:Can't save sound from microphone无法保存麦克风的声音
【发布时间】:2015-01-12 11:35:27
【问题描述】:

我使用下面的代码部分来启用麦克风录音,然后我保存它并用 2 个按钮播放它。但是,如果在 savedialog 中我将 .wav 保存在桌面文件夹中,一切都很好,它会创建 test.wav(example) 但如果我尝试将它保存在任何其他目录/路径中,它不会做任何事情。

我想它无法创建或保存声音。但我不知道为什么。

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


//voice recorder

namespace recorder
{
   public partial class Form1 : Form
   {   
      [DllImport("winmm.dll")]
      private static extern int mciSendString(string MciComando, string MciRetorno, int MciRetornoLeng, int CallBack);

      string musica = "";

      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         label1.Visible = false;         //label1.Text=recording
      }


//starts recording what I say to mic

      private void btnStart_Click(object sender, EventArgs e)


      {
         label1.Visible = true;
         mciSendString("open new type waveaudio alias Som", null, 0, 0);
         mciSendString("record Som", null, 0, 0);
      }

//stops recording and shows a save form
//This is where the problem is.
/* I don't want the program to ask the user for location and file name and format to be    
saved as. I just want the file to save in c:\ with the filename "recording1" with format 
as .wav */

      private void btnStopnSave_Click(object sender, EventArgs e)
      {


         label1.Visible = false;
         mciSendString("pause Som", null, 0, 0);
         SaveFileDialog save = new SaveFileDialog();
         save.Filter = "WAVE|*.wav";

         if (save.ShowDialog() == DialogResult.OK) // this is where it needs to be altered
         {
            mciSendString("save Som " + save.FileName,null,0, 0);
            mciSendString("close Som", null, 0, 0);
         }
      }

//lets user to open a WAV file by browsing files

      private void btnPlay_Click(object sender, EventArgs e)
      {

         if (musica == "")                          
         {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Wave|*.wav";
            if (open.ShowDialog() == DialogResult.OK) 
            {
                musica = open.FileName;
                mciSendString("play " + musica, null, 0, 0);
            }
         }

      } 
   }
}

我在这里找到了代码: saving .wav files without showdialog in C#

【问题讨论】:

    标签: c# audio save microphone


    【解决方案1】:

    只是不要打开文件选择框/SaveFileDialog。

    这样做:

      private void btnStopnSave_Click(object sender, EventArgs e)
      {
         label1.Visible = false;
         mciSendString("pause Som", null, 0, 0);
    
         string filename = "whatever";
         mciSendString("save Som " + filename, null,0, 0);
         mciSendString("close Som", null, 0, 0);
      }
    

    【讨论】:

    • 谢谢,现在它在我的项目的调试文件夹中创建.wav。我如何将它保存到我的调试文件夹中的特定文件夹中?例如在文件夹 MicMusic 中(它的文件夹存在于调试文件夹中)
    • 你必须在filename 中设置正确的路径名。不要使用相对路径,请使用绝对路径,例如 C:\temp\whatever.wav
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多