MATLAB GUI CODING GUIDE
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
...
1 comentario
Jan
el 25 de Mzo. de 2021
The question is not clear yet. What is "audio sound in a listbox"? Plase post more details.
Respuestas (1)
Image Analyst
el 25 de Mzo. de 2021
In the listbox callback you'd do something like (untested)
listboxItems = handles.listbox1.String;
selectedIndex = handles.listbox1.Value;
fileName = listboxItems{selectedIndex};
PlaySoundFile(fileName)
Here is the PlaySoundFile() function. It's a bit long because it's robust and flexible.
% Play a wav file. Pass in the folder name, and the base file name separately.
% Or, you can pass in the string 'random' for baseWavFileName and it will pick one at random from the folder to play.
% PlaySoundFile('chime.wav'); % Tries to find it in the current folder, or on the search path.
% PlaySoundFile(handles.soundFolder, 'chime.wav');
% PlaySoundFile(handles.soundFolder, 'random');
function PlaySoundFile(varargin)
if nargin >= 2
soundFolder = varargin{1};
baseWavFileName = varargin{2};
else
[soundFolder, baseFileNameNoExt, ext] = fileparts(varargin{1});
baseWavFileName = [baseFileNameNoExt, ext];
end
try % Read the sound file into MATLAB, and play the audio.
% soundFolder = fullfile(soundFolder, 'Sound Files');
if isempty(soundFolder) || ~isfolder(soundFolder)
soundFolder = pwd;
end
fullWavFileName = fullfile(soundFolder, baseWavFileName);
if ~isfile(fullWavFileName)
% The sound does not exist in the folder.
% See if it's on the search path.
folder = fileparts(which(baseWavFileName));
if isempty(folder)
% Sound folder does not exist.
warningMessage = sprintf('Warning: sound folder not found:\n%s', soundFolder);
WarnUser(warningMessage);
return;
else
% Found it somewhere on the search path.
soundFolder = folder;
end
end
if strcmpi(baseWavFileName, 'random')
itWorked = false;
tryCount = 1;
while itWorked == false
% Pick a file at random.
filePattern = fullfile(soundFolder, '*.wav');
waveFiles = dir(filePattern);
numberOfFiles = length(waveFiles);
% Get a random number
fileToPlay = randi(numberOfFiles, 1);
baseWavFileName = waveFiles(fileToPlay).name;
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
try
if isfile(fullWavFileName)
[waveFileData, Fs] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
% It worked. It played because the audio format was OK.
itWorked = true;
catch
% Increment the try count and try again to find a file that plays.
tryCount = tryCount + 1;
if tryCount >= numberOfFiles
break;
end
end
end % of while()
else
% baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if isfile(fullWavFileName)
[waveFileData, Fs] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
end
catch ME
if strfind(ME.message, '#85')
% Unrecognized format. Play chime instead.
fprintf('Error in PlaySoundFile(): %s.\nUnrecognized sound format in file:\n\n%s\n', ME.message, fullWavFileName);
baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if isfile(fullWavFileName)
[waveFileData, Fs] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
end
end
errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from PlaySoundFile
function WarnUser(message)
fprintf('%s\n', message);
uiwait(warndlg(message));
2 comentarios
Image Analyst
el 25 de Mzo. de 2021
Uh, okay. What about it? Does it work? If not, attach your .fig file and .m file.
Image Analyst
el 26 de Mzo. de 2021
Your tag said you're using GUIDE. So attach both the GUIDE fig file, and it's associated .m file.
Ver también
Categorías
Más información sobre Audio I/O and Waveform Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!