问题
I'm trying to create a MATLAB program to play a sound over and over again every couple of minutes. Right now I have it set to play every couple of seconds, just to get some bugs out of the system. However, when my program tries to replay the sound I get this error:
??? Error using ==>
audioplayer.audioplayer>audioplayer.resume at 710
A given audio device may only be opened once.
Error in ==> audioplayer.play at 88
obj.resume();
Error in ==>
audiorecorder.audiorecorder>audiorecorder.play at
779
play(player, varargin{:})
Error in ==> Voice at 17
play(voice);
My code so far is this:
clc;
clear all;
clear main;
%File to play my voice to tell me to keep studying
voice = audiorecorder;
disp('Start speaking.');
recordblocking(voice, 3);
disp('DONE');
while i ~= 100;
pause(1);
play(voice);
i = 0;
end
Basically my question is is there a way to keep writing my same sound file to a different name, and then continuously play the new file? I thought about making a matrix, and then just use a "for" loop to traverse it then play the new file, but I don't know how to make a matrix out of sound files. Is there a different command I can use instead of "play" that will let me keep playing it?
EDIT: Doing more research on this now, since I've never actually learned about this in class, but would it be best to just create a new file containing my sound and then repeadetly access the file?
回答1:
Regarding the code you posted, I think the issue is that you only pause(1)
which I suspect is shorter than the sound so you try and play(voice)
while it is still playing from the previous loop. Instead of play
use playblocking as this should stop the loop until the sound is finished.
回答2:
this error "A given audio device may only be opened once" it mean...
when you run your code, if another program or function call "audioplayer".
The system will have a problem, so you need to include some code for check "Running" property of "audioplayer" before playing loop.
such as
.....
.....
A=get(hplay,'Running');
if(strcmp(A,'off')
......player your code....
end
来源:https://stackoverflow.com/questions/16226309/keep-playing-a-sound-over-and-over-again-in-matlab