Mostafa - you could perhaps use a timer to periodically check the status of the audio player. If it isplaying then you would do nothing, but if it is not playing, then you could start the player again. Something like the following might be adapted to suit your needs for App Designer
function [player, audioPlayerStateTimer] = repeatedAudioPlayerExample
audiodata = load('handel.mat');
player = audioplayer(audiodata.y,audiodata.Fs);
% create the periodic timer to check state of player every 500 millisesconds
audioPlayerStateTimer = timer;
audioPlayerStateTimer.TimerFcn = @restartAudioPlayerIfStopped;
audioPlayerStateTimer.Period = 0.5;
audioPlayerStateTimer.ExecutionMode = 'fixedSpacing';
start(audioPlayerStateTimer)
function restartAudioPlayerIfStopped(hObject, eventdata)
if ~isplaying(player)
fprintf('Restarting audio player.\n');
play(player);
end
end
end
I had thought about perhaps using the StopFcn callback in the audio player object to restart itself when the player stops but that might mean you would have to destroy/clear the player in order to really stop playing.
function [player] = repeatedAudioPlayerExample2
audiodata = load('handel.mat');
player = audioplayer(audiodata.y,audiodata.Fs);
player.StopFcn = @restartAudioPlayerIfStopped;
play(player);
function restartAudioPlayerIfStopped(hObject, eventdata)
if ~isplaying(player)
fprintf('Restarting audio player.\n');
play(player);
end
end
end
Since calling stop(player) will cause the StopFcn callback to fire and so restart the audio, you would need to call delete(player) in order to permanently stop it.