MATLAB Answers

# how do I want to make a this song playing in my favorite song only

1 view (last 30 days)
azman hasan on 7 Jan 2021
Answered: Monisha Nalluru on 12 Jan 2021
%display number of tracks
cd1 = randi([1 30]);
cd2 = randi([30 60]);
cd3 = randi([60 90]);
c = [cd1 cd2 cd3];
test = 0;
fprintf('\nThere are %d tracks on CD 1\n', cd1);
fprintf('There are %d tracks on CD 2\n', cd2);
fprintf('There are %d tracks on CD 3\n\n', cd3);
fprintf('Please enter your favorite track?\n');
cd = input('Please enter the number of the CD: ', 's');
cd = str2double(cd);
while ~any(cd == 1:3)
fprintf('Sorry, We dont have that cd. Please try again.\n');
cd = input('Please enter the number of the CD: ', 's');
cd = str2double(cd);
end
track = input('Please enter the track number: ', 's');
track = str2double(track);
while ~any(track == 1:c(cd))
fprintf('Sorry, that''s not a valid track on CD %d \n', cd);
track = input('Please enter the track number: ', 's');
track = str2double(track);
end
fprintf('\n\nPlay List:\n'); %get sum of all the tracks and randomly select number of tracks to be
n = sum(c); %randomized in the playlist
n = randi([1 n]);
for i = 1:n
randCD = randi([1 3]);
randTrack = randi([1 c(randCD)]);
fprintf('CD %d Track %d\n', randCD, randTrack);
if randCD == cd && randTrack == track
test = 1;
end
end
if test == 1
fprintf('\nYour favorite track was played in this cd.\n');
[test, fs]= audioread('song.mp3');
s=audioplayer(test,fs);
else
fprintf('\nSorry, your favorite track was not played in this cd.\n');
end
##### 4 CommentsShowHide 3 older comments
azman hasan on 7 Jan 2021
ouh im so sorry everyone, i still newbie in this mathlab. this is my first time asking the question in this mathlab.

Sign in to comment.

### Answers (1)

Monisha Nalluru on 12 Jan 2021
From my understanding you want to play songs from user chose cd and track.
Inorder to achieve the above requirement create a array with list of all songs and ask user to select one track from available track
As an example
cd1=['song11.mp3','song12.mp3','song13.mp3'];
cd2=['song21.mp3','song22.mp3','song23.mp3'];
cd3=['song31.mp3','song32.mp3','song33.mp3'];
% display available options
fprintf('\nThere are %d tracks on CD 1\n', length(cd1));
...
% ask user to select one cd
cd = input('Please enter the number of the CD: ', 's');
%validate his choice avaialble or not
%ask user to select track and validate the track available
track = input('Please enter the track number: ', 's');
%play the selected song using audioplayer object
[test, fs]= audioread(cd1[track]); % place selected cd and track cdselected[track]
s=audioplayer(test,fs);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!