Main Content

stop

Stop playing and/or recording

Since R2025a

    Description

    stop(as) stops both playing and recording for the audiostreamer.

    example

    stop(as,mode) stops the specified mode of the audiostreamer.

    Examples

    collapse all

    Create an audiostreamer object with the "recorder" mode.

    as = audiostreamer("recorder");

    Call record to start recording with the audiostreamer. The audiostreamer will record continuously until you pause or stop the recording. Use the MATLAB pause function to wait for three seconds, then call stop on the audiostreamer object to stop recording.

    record(as)
    pause(3)
    stop(as)

    Call read to get all of the available recorded audio from the audiostreamer.

    x = read(as);

    Alternatively, you can call record with a specified number of samples after which the recording will automatically stop. Calling read with the specified number of samples will block MATLAB execution until that number of samples has been recorded.

    N = 3*as.SampleRate; % 3 seconds of samples
    record(as,N)
    y = read(as,N);

    You can use a callback function to asynchronously provide audio to the audiostreamer to play. Define your callback as a function that generates pink noise and plays it on the audiostreamer. The callback function takes in the audiostreamer object and a struct containing information about the event that triggered the callback. Create an audiostreamer object and set the PlayerFcn callback to the myPlayerFcn function handle.

    function myPlayerFcn(obj,ev)
        y = randn(1024,1);
        play(obj,y)
    end
    
    as = audiostreamer;
    as.PlayerFcn = @myPlayerFcn;

    Now calling play on the audiostreamer will call the PlayerFcn callback whenever the number of samples in the output queue drops below PlayerMinSamples.

    play(as)
    pause(3)
    stop(as)

    Input Arguments

    collapse all

    The audiostreamer object that is playing and/or recording the audio.

    The audiostreamer mode to stop, specified as "player", "recorder", or "both".

    Data Types: char | string

    Version History

    Introduced in R2025a