Main Content

play

Play audio on device

Since R2025a

    Description

    play(as,audioOut) plays the audio data on the audiostreamer device. By default, this function blocks MATLAB® execution until the device finishes playing the signal.

    example

    play(as,audioOut,"non-blocking") plays the audio without blocking MATLAB execution.

    play(as) begins playing audio provided asynchronously by the audiostreamer PlayerFcn callback function. You can pause or stop the audio playback using pause or stop, respectively.

    Examples

    collapse all

    Read in an audio signal from a file. Create an audiostreamer object for playing audio with the same sample rate as the signal.

    [x,fs] = audioread("Counting-16-44p1-mono-15secs.wav");
    as = audiostreamer("player",fs);

    Call play with the audiostreamer and the data to play the audio. By default, this function blocks MATLAB execution until the audio is finished playing.

    play(as,x)

    You can also call play with the "non-blocking" argument to play the audio without blocking MATLAB execution.

    play(as,x,"non-blocking")

    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 used to play the audio.

    Audio data to play on the device, specified as a column vector or matrix where the columns correspond to independent audio channels.

    Data Types: single | double

    Version History

    Introduced in R2025a