How can I stop the graph , and how can it stip when the song stops?

2 views (last 30 days)
function MusicPlay(InfoSongName)
PlayPlaySong = InfoSongName +".mp3";
[VariableSong, Variable] = audioread(PlayPlaySong);
Song = audioplayer(VariableSong, Variable);
PlaySong = input(' Will you like to play the song Yes or No ','s');
while strcmpi(PlaySong, 'Yes') ~= 1 && strcmpi(PlaySong, 'No') ~=1
fprintf(' Selected Yes or No!!! \n')
PlaySong = input(' Will you like to play the song Yes or No ','s');
end
%%
if strcmpi(PlaySong, "Yes")
% NewVariable = audioread(PlayPlaySong);
% plot(NewVariable)
% soundsc(VariableSong, Variable);
filename = PlayPlaySong;
[Y,Fs] = audioread(filename);
TotalTime = length(Y) ./Fs;
t = 0:TotalTime/(length(Y)):TotalTime-TotalTime/length(Y);
sound(Y,Fs)
Y = Y';
var = 50000;
figure
a = 1;
for i = var:1000:length(Y(1, :))
subplot(2,1,1);
plot(t(a:i), Y(1,a:i));
title("Left")
subplot(2,1,2);
plot(t(a:i), Y(2,a:i));
title("Right")
a = i;
% fprintf("%f \n",a);
pause(.75);
end
StopSong = input(' Would you like to stop the song? (Type Yes to stop or anything else to continue) ','s');
while isempty(StopSong) || strcmpi(StopSong, 'Yes') ~= 1
pause(12)
StopSong = input(' Would you like to stop the song? (Type Yes to stop or anything else to continue) ','s');
end
if strcmpi(StopSong ,'Yes')
clear sound
end
elseif strcmpi(PlaySong, "No")
fprintf("\t \n");
fprintf('\t\t "Thank you for using MusicLive". \n');
fprintf('\t \n');
end
% It plays the song and the plots the grpah but it will not stop the
% plotting (graph) when the song ends.
% Also the following code is not printing in the command window!
%{
StopSong = input(' Would you like to stop the song? (Type Yes to stop or anything else to continue) ','s');
while isempty(StopSong) || strcmpi(StopSong, 'Yes') ~= 1
pause(12)
StopSong = input(' Would you like to stop the song? (Type Yes to stop or anything else to continue) ','s');
end
if strcmpi(StopSong ,'Yes')
clear sound
end

Answers (1)

Manish
Manish on 19 Sep 2024
Hi,
I understand that you want to interrupt the graph based on user input.
This can be achieved by making some modifications to the for loop.
Below is the revised version of the for loop:
for i = var:1000:length(Y(1, :))
% Plot left channel
subplot(2,1,1);
plot(t(a:i), Y(1,a:i));
title("Left");
% Plot right channel
subplot(2,1,2);
plot(t(a:i), Y(2,a:i));
title("Right");
% Update the index
a = i;
% Pause to allow song to play
pause(0.75);
% Check if the user wants to stop the song
StopSong = input('Would you like to stop the song? (Type Yes to stop or anything else to continue): ', 's');
if strcmpi(StopSong, 'Yes')
clear sound;
break;
else
fprintf("\t \n");
fprintf('\t\t "Thank you for using MusicLive". \n');
continue;
end
end
Hope this solves!

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!