在matlab中实现图形界面的录音和播放功能,点击开始录音和停止录音都没反应怎么回事?
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Label matlab.ui.control.Label
PlaySound2Button matlab.ui.control.Button
PlaySound1Button matlab.ui.control.Button
SeparateButton matlab.ui.control.Button
StopRecordingButton matlab.ui.control.Button
StartRecordingButton matlab.ui.control.Button
end
properties (Access = private)
Recorder audiorecorder
IsRecording double
SampleRate double
AudioData double
SeparatedAudio double
end
methods (Access = private)
% Button pushed function: StartRecordingButton
function StartRecordingButtonPushed(app, ~)
if app.IsRecording == 0
app.Recorder = audiorecorder(app.SampleRate, 16, 1); % Sample rate and channels
record(app.Recorder);
app.IsRecording = 1;
app.Label.Text = '录音中...';
end
end
% Button pushed function: StopRecordingButton
function StopRecordingButtonPushed(app, ~)
if app.IsRecording == 1
stop(app.Recorder);
app.IsRecording = 0;
app.Label.Text = '已停止录音.';
app.AudioData = getaudiodata(app.Recorder);
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create StartRecordingButton
app.StartRecordingButton = uibutton(app.UIFigure, 'push');
app.StartRecordingButton.ButtonPushedFcn = createCallbackFcn(app, @StartRecordingButtonPushed, true);
app.StartRecordingButton.Position = [46 362 130 23];
app.StartRecordingButton.Text = 'StartRecordingButton';
% Create StopRecordingButton
app.StopRecordingButton = uibutton(app.UIFigure, 'push');
app.StopRecordingButton.ButtonPushedFcn = createCallbackFcn(app, @StopRecordingButtonPushed, true);
app.StopRecordingButton.Position = [46 304 130 23];
app.StopRecordingButton.Text = 'StopRecordingButton';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
function SetData(app)
app.IsRecording = 0;
app.SampleRate = 44100;% 采样率(根据需要调整)
app.AudioData = [];
app.SeparatedAudio = [];
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end