使用麦克风录音

Godot支持Windows, macOS, Linux, Android和iOS的游戏内音频录制.

一定要查看Viewport演示! 可以下载演示档案中的Viewport文件夹, 或: https://github.com/godotengine/godot-demo-projects/tree/master/audio/mic_record.

根节点的结构

该演示由单一场景组成, 这个场景包括两个主要部分:GUI和音频.

我们将重点介绍音频部分. 在本演示中, 创建了一个名为 Record 的总线, 效果为 Record, 用于处理音频录制. 用一个名为 AudioStreamPlayerAudioStreamRecord 进行录音.

../../_images/record_bus.png ../../_images/record_stream_player.png

GDScript

C#

  1. var effect
  2. var recording
  3. func _ready():
  4. # We get the index of the "Record" bus.
  5. var idx = AudioServer.get_bus_index("Record")
  6. # And use it to retrieve its first effect, which has been defined
  7. # as an "AudioEffectRecord" resource.
  8. effect = AudioServer.get_bus_effect(idx, 0)
  1. private AudioEffectRecord _effect;
  2. private AudioStreamSample _recording;
  3. public override void _Ready()
  4. {
  5. // We get the index of the "Record" bus.
  6. int idx = AudioServer.GetBusIndex("Record");
  7. // And use it to retrieve its first effect, which has been defined
  8. // as an "AudioEffectRecord" resource.
  9. _effect = (AudioEffectRecord)AudioServer.GetBusEffect(idx, 0);
  10. }

音频录制由 AudioEffectRecord 资源处理, 它有三个方法: get_recording(), is_recording_active(), 以及 set_recording_active().

GDScript

C#

  1. func _on_RecordButton_pressed():
  2. if effect.is_recording_active():
  3. recording = effect.get_recording()
  4. $PlayButton.disabled = false
  5. $SaveButton.disabled = false
  6. effect.set_recording_active(false)
  7. $RecordButton.text = "Record"
  8. $Status.text = ""
  9. else:
  10. $PlayButton.disabled = true
  11. $SaveButton.disabled = true
  12. effect.set_recording_active(true)
  13. $RecordButton.text = "Stop"
  14. $Status.text = "Recording..."
  1. public void OnRecordButtonPressed()
  2. {
  3. if (_effect.IsRecordingActive())
  4. {
  5. _recording = _effect.GetRecording();
  6. GetNode<Button>("PlayButton").Disabled = false;
  7. GetNode<Button>("SaveButton").Disabled = false;
  8. _effect.SetRecordingActive(false);
  9. GetNode<Button>("RecordButton").Text = "Record";
  10. GetNode<Label>("Status").Text = "";
  11. }
  12. else
  13. {
  14. GetNode<Button>("PlayButton").Disabled = true;
  15. GetNode<Button>("SaveButton").Disabled = true;
  16. _effect.SetRecordingActive(true);
  17. GetNode<Button>("RecordButton").Text = "Stop";
  18. GetNode<Label>("Status").Text = "Recording...";
  19. }
  20. }

在演示开始时, 录音效果是未激活. 当用户按下 RecordButton 时, 用 set_recording_active(true) 启用该效果.

在下一次按下按钮时, 由于 effect.is_recording_active()true, 可以通过调用 effect.get_recording() 将录制的流存储到 recording 变量中.

GDScript

C#

  1. func _on_PlayButton_pressed():
  2. print(recording)
  3. print(recording.format)
  4. print(recording.mix_rate)
  5. print(recording.stereo)
  6. var data = recording.get_data()
  7. print(data)
  8. print(data.size())
  9. $AudioStreamPlayer.stream = recording
  10. $AudioStreamPlayer.play()
  1. public void OnPlayButtonPressed()
  2. {
  3. GD.Print(_recording);
  4. GD.Print(_recording.Format);
  5. GD.Print(_recording.MixRate);
  6. GD.Print(_recording.Stereo);
  7. byte[] data = _recording.Data;
  8. GD.Print(data);
  9. GD.Print(data.Length);
  10. var audioStreamPlayer = GetNode<AudioStreamPlayer>("AudioStreamPlayer");
  11. audioStreamPlayer.Stream = _recording;
  12. audioStreamPlayer.Play();
  13. }

要回放录音, 你可以将录音指定为 AudioStreamPlayer 的流, 并调用 play().

GDScript

C#

  1. func _on_SaveButton_pressed():
  2. var save_path = $SaveButton/Filename.text
  3. recording.save_to_wav(save_path)
  4. $Status.text = "Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
  1. public void OnSavebuttonPressed()
  2. {
  3. string savePath = GetNode<LineEdit>("SaveButton/Filename").Text;
  4. _recording.SaveToWav(savePath);
  5. GetNode<Label>("Status").Text = string.Format("Saved WAV file to: {0}\n({1})", savePath, ProjectSettings.GlobalizePath(savePath));
  6. }

要保存录音, 你需要调用 save_to_wav(), 并提供文件的路径. 在本演示中, 路径由用户通过 LineEdit 输入框定义.