cylhs 2023-09-25 14:47 采纳率: 0%
浏览 4

matlab图形界面编程

在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
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-25 17:20
    关注

    【以下回答由 GPT 生成】

    这个问题可能是由于未正确初始化音频录制器对象和样本率属性导致的。下面是解决方案的步骤:

    1. 请确保在类的构造函数中正确初始化 SampleRate 属性。通常,音频的采样率(即每秒钟采样的样本数)是一个常见的参数,需要根据你的需求设置一个合适的值。例如,将 SampleRate 属性设置为标准的 44100:
    app.SampleRate = 44100;
    
    1. 确认 app.Recorder 对象已正确创建。在 StartRecordingButtonPushed 方法中,需要添加创建录制器对象的语句:
    % 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
    
    1. 检查 StopRecordingButtonPushed 方法是否正确停止录制,并将录制的音频数据存储到 app.AudioData 属性中。请注意,在停止录制之前,必须调用 getaudiodata 函数以获取实际录制的音频数据。修改方法如下:
    % 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
    
    1. 确保在 appcreateComponents 方法中正确创建所有组件和按钮的回调函数。这里只展示了部分代码,请确保完成所有按钮的创建和回调函数的指定:
    % Create StartRecordingButton
    app.StartRecordingButton = uibutton(app.UIFigure, 'push');
    app.StartRecordingButton.ButtonPushedFcn = createCallbackFcn(app, @StartRecordingButtonPushed, true);
    app.StartRecordingButton.Position = [155 91 85 22];
    app.StartRecordingButton.Text = '开始录音';
    
    % Create StopRecordingButton
    app.StopRecordingButton = uibutton(app.UIFigure, 'push');
    app.StopRecordingButton.ButtonPushedFcn = createCallbackFcn(app, @StopRecordingButtonPushed, true);
    app.StopRecordingButton.Position = [255 91 85 22];
    app.StopRecordingButton.Text = '停止录音';
    

    请尝试以上步骤,并确认是否能够解决你的问题。如果还有其他问题,请随时提问。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月25日