I'm currently trying to give my matlab game sound. To stop the sounds from overlapping I found a solution and changed the data accordingly.
SOUND_FILE_NAMES = {'.\sounds\pacman_beginning.wav', '.\sounds\pacman_chomp.wav', ... '.\sounds\pacman_death.wav', '.\sounds\pacman_eatfruit.wav', ... '.\sounds\pacman_eatghost.wav', '.\sounds\pacman_extrapac.wav', ... '.\sounds\pacman_intermission.wav', '.\sounds\pacman_moving.wav'};SOUND_KEYS = {'Beginning', 'chomp', 'death', 'eatfruit', 'eatghost', ... 'extrapac', 'intermission', 'moving'};SOUND_SAMPLES = [5,5,5,3,3,2,6,5];obj.audioPlayers = struct(); for i = 1 : length( SOUND_FILE_NAMES ) [y, Fs] = audioread(SOUND_FILE_NAMES{i}); str = 'audioplayer(y,Fs)'; for j = 2 : SOUND_SAMPLES(i); str = sprintf('%s, audioplayer(y,Fs)', str ); end eval( sprintf('obj.audioPlayers.(SOUND_KEYS{i}) = {%s};', str )); endPlaySound(obj,'Beginning');
and the function
function PlaySound( obj, soundName ) done = false; i = 1; while ~done && i < length(obj.(soundName)) if ~obj.(soundName){i}.isplaying() obj.(soundName){i}.play(); done = true; end i = i + 1; end end
But I only get the error
Reference to non-existent field 'Beginning' Error in PlaySound (line 7) while ~done && i < length(obj.(soundName))
I don't know how to solve this problem.
Best Answer