Stop playing audio using audioDeviceWriter.

7 views (last 30 days)
How to stop playing an audio in between when using audioDeviceWriter?
deviceWriter = audioDeviceWriter
deviceWriter(Speech_sample); % Speech_sample is the desired speech variable.
For audioplayer,
plays the audio while
stops it. Can an audio played by audioDeviceWriter be stopped in a similar way?

Answers (2)

Devineni Aslesha
Devineni Aslesha on 12 Aug 2019
Play and stop functions are applicable for audioplayer and audiorecorderobjects. An audio played by audioDeviceWriter can be stopped by using release function as audioDeviceWriter is a system object.
Use the below code for testing.
% Path for the below file is “C:\Program Files\MATLAB\R2019a\examples\audio\farspeech”
y = load('farspeech.mat')
deviceWriter = audioDeviceWriter('SampleRate',10000);
mySignal = y.x(1:20000);
for i = 1:10
Use the command release(deviceWriter) to stop the audio after some delay.
Doc Link:
Sudeep Surendran
Sudeep Surendran on 12 Aug 2019
Hi Devineni Aslesha,
I made the number of iterations to 1000 and did release(deviceWriter) . It doesn't stop in between and is still running.

Sign in to comment.

Charlie DeVane
Charlie DeVane on 9 Dec 2019
audioDeviceWriter is designed to play back audio incrementally, by repeatedly writing frames (small amounts of the signal) to the audio device. It sends one frame of audio each time you call its step function. This enables you, among other things, to play back an infinite stream of data. By contrast, you must give audioplayer the entire signal when you create it.
To make an audioDeviceWriter stop playing audio, you simply stop calling its step function.
A good example is in:
The heart of the example is this loop:
while ~isDone(fileReader)
audioData = fileReader(); % call fileReader step function to get a frame of audio from input file
deviceWriter(audioData); % call deviceWriter step function to play the frame of audio
This loop stops playing audio when the input file is exhausted (isDone(fileReader)).
You can change that logic to stop under whatever conditions make sense for your application. It might look something like this:
keepPlaying = true;
while ~isDone(fileReader) && keepPlaying
audioData = fileReader(); % get a frame of audio from input file
deviceWriter(audioData); % write the audio to the output device
keepPlaying = codeToDecideWhetherToKeepPlayingAudio; % whatever your application needs
hope this helps,


Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by