Main Content

audioplayer

Object for playing audio

Description

Use an audioplayer object to play audio data. The audioplayer object contains properties that enable additional flexibility during playback. For example, you can pause, resume, or define callbacks using the audioplayer object functions.

Creation

Description

example

player = audioplayer(Y,Fs) creates an audioplayer object for signal Y, using sample rate Fs. The function returns the audio player object, player.

player = audioplayer(Y,Fs,nBits) uses nBits bits per sample for signal Y.

player = audioplayer(Y,Fs,nBits,ID) uses the audio device identified by ID for output.

example

player = audioplayer(recorder) creates an audioplayer object using audio recorder object recorder.

player = audioplayer(recorder,ID) creates an object from recorder that uses the audio device identified by ID for output.

Input Arguments

expand all

Audio signal, specified as a vector or two-dimensional array of numeric data.

The value range of the input samples depends on the data type.

Data Type

Sample Value Range

int8

-128 to 127

uint8

0 to 255

int16

-32,768 to 32,767

single or double

-1 to 1

Data Types: single | double | int8 | int16 | uint8

Sample rate in hertz (Hz), specified as a positive scalar.

Valid values of the sample rate depend on the audio hardware of your system. Typical sample rates supported by most setups are 8000, 11,025, 22,050, 44,100, 48,000, 96,000, and 192,000 Hz.

This argument sets the SampleRate property.

Data Types: single | double

Bits per sample, specified as 16, 8, or 24. Specify nBits only when the signal contains floating-point values.

Valid values of nBits depend on the audio hardware of your system.

This argument sets the BitsPerSample property.

Audio device identifier, specified as an integer.

To obtain the ID of a device, use the audiodevinfo function.

This argument sets the DeviceID property.

Audio recorder object, specified as an audiorecorder object. Use the audiorecorder function to create the object.

Properties

expand all

Sample rate in hertz (Hz), specified as a positive scalar.

To set the SampleRate property initially, use the Fs input argument when creating the audioplayer object. After creating the object, you can modify its SampleRate property.

This property is read-only.

Bits per sample, returned as 8, 16, or 24.

To set the BitsPerSample property, use the nBits input argument when creating the audioplayer object.

This property is read-only.

Number of audio channels, returned as 1 (mono) or 2 (stereo).

This property is read-only.

Audio device identifier, returned as an integer.

To set the DeviceID property, use the ID input argument when creating the audioplayer object.

This property is read-only.

Sample currently playing on the audio output device, returned as a positive integer.

If the device is not playing, then CurrentSample is the next sample to play using the play or resume method.

This property is read-only.

Total length of the audio data in samples, returned as a nonnegative integer.

This property is read-only.

Audio player status, returned as 'off' or 'on'.

Function to execute at the start of playback, specified as a character vector containing the name of the function, or a function handle.

The first two inputs to your callback function must be the audioplayer object and an event structure. For more information, see Timer Callback Functions.

Function to execute at the end of playback, specified as a character vector containing the name of the function, or a function handle.

The first two inputs to your callback function must be the audioplayer object and an event structure. For more information, see Timer Callback Functions.

Function to execute repeatedly during playback, specified as a character vector containing the name of the function, or a function handle. To specify time intervals for the repetitions, use the TimerPeriod property.

The first two inputs to your callback function must be the audioplayer object and an event structure. For more information, see Timer Callback Functions.

Timer period, specified as a positive scalar.

TimerPeriod is the time in seconds between TimerFcn callbacks.

Label, specified as a string scalar or character vector.

User-defined data, specified as a value of any data type. Use this property to store any additional data with the object.

This property is read-only.

Object class name, returned as 'audioplayer'.

Object Functions

getQuery property values for audioplayer object
isplayingDetermine if playback is in progress
pausePause playback of audioplayer object or recording of audiorecorder object
playPlay audio from audioplayer object
playblockingPlay audio from audioplayer object; hold control until playback completes
resume Resume playback of audioplayer object or recording of audiorecorder object from paused state
setSet property values for audioplayer object
stopStop playback of audioplayer object or recording of audiorecorder object

Examples

collapse all

Load and play a sample audio file.

Load handel.mat into the workspace. The file contains a sample audio data array y and the sampling rate Fs.

load handel.mat
whos y Fs
  Name          Size             Bytes  Class     Attributes

  Fs            1x1                  8  double              
  y         73113x1             584904  double              

Create an audioplayer object to play the file.

player = audioplayer(y,Fs);

Play the audio object on the default audio device.

play(player);

Record audio data from a microphone and then play the recorded audio.

Create an audiorecorder object with default property values.

recObj = audiorecorder;

Record a 5-second sample of your voice with your microphone.

disp("Begin speaking.")
Begin speaking.
recDuration = 5; % record for 5 seconds
recordblocking(recObj,recDuration);
disp("End of recording.")
End of recording.

Create an audio player object from the recording and then play the recorded sample.

playerObj = audioplayer(recObj);
play(playerObj);

Limitations

  • These limitations apply when using audioplayer in MATLAB® Online™ or MATLAB Web App Server™:

    • You cannot create an audioplayer object from an audiorecorder object.

    • You cannot change the sample rate of an existing audioplayer object.

    • audioplayer ignores nBits. Instead, it plays audio data using the default number of bits per sample of the output audio device.

    • Valid values of the sample rate Fs may not be the same as they are in MATLAB Desktop.

Tips

  • The audioplayer function supports sound devices on all Windows® and most UNIX® platforms.

  • audioplayer in MATLAB Online and MATLAB Web App Server is supported in Google Chrome™.

  • Note

    Security Considerations When Using MATLAB Online or MATLAB Web App Server: When using audioplayer in MATLAB Online or MATLAB Web App Server, certain features and settings help you keep control of your privacy.

    To reduce the likelihood of unwanted applications using your microphone, turn off automatic access to your audio device. You can change these settings at any time. After granting permission to a website once, your browser may be able to access your microphone automatically for that site on future visits. Use the browser settings for Google Chrome to revoke access for specific sites after you have initially allowed access.

    You can also access the MATLAB Online website or MATLAB Web App Server using the private browsing mode in Google Chrome. When you do this, Chrome™ automatically asks you for permission to access your microphone every time you open the MATLAB Online site or MATLAB Web App Server and run audioplayer, regardless of your browser settings.

Version History

Introduced before R2006a