I'm trying to create Random Music Player and could use help!

TLDR: I stream music in my dorm room 24/7 and the stations I listen to loop every 19 hours and 41 minutes, and it gets old. So I want to make a random music player with matlab.
I am using the randi function to generate a random number so that it chooses one. Now, I need it to realize that is the name of a song, grab it, and then play it.
Here is the code I have so far! Any help would be greatly appreicated as I am a beginer. (I know that I only have 12 songs so far, this is just a proof of concept for now).
r = randi(12);
%use the random number generated to select a song (all named 1.m4a or
%12.m4a or whatever)
%plug the song into the folling code that plays the song
[a, Fs] = audioread('__________.m4a');
plot(a(:,1))
p = audioplayer(a,Fs);
play(p)
%repeat process. I will eventually turn this into a fucntion that can
%smartly choose between playing another song or letting us hear from a DJ
%in the Fallout Video games, but right now I just want to get the random
%generation perfected.

 Accepted Answer

% clear all; close all; clc
MusicList = dir('*.m4a');
while(1)
r = randi(length(MusicList),1,1)
[a, Fs] = audioread(MusicList(r).name);
plot(a(:,1))
p = audioplayer(a,Fs);
play(p);
pause(length(a)/Fs);
end
I am not sure if I understand your question completely. Does this help?
This gets the list of .m4a files in the current directory and plays them randomly indefinitely.

6 Comments

To do it like a "shuffle" so there are no repeats until the entire list has played, you'd have to call randperm() in advance of the loop
sortOrder = randperm(length(MusicList));
while(1) % Actually using while like this is bad
r = sortOrder(count); % Get current index
count = mod(count + 1, length(MusicList)); % Increment the index in sortOrder.
Awesome! Thank you so much! That was a huge help and you understood the question perfectly!
Just to add on one last part to that, as I have it plotting the song, is there a way to get the songs title at the top of the plot so that I know what I'm listening to? The end goal is to have this be on 24/7 in my dorm room and then to download a bunch of old tunes off internet archive and put them in the directory, so there will always be something new in there, and if I want to add it to my phone I know which file to grab.
Thank you again!
Just add the following line after plot() command.
title(MusicList(r).name(1:end-4));
dir command generates a struct of all the music files. struct has 6 fields in it (name, folder, date, bytes, isdir, datenum). double click on MusicList variable in workspace. You can see its contents.
Awesome! That seems to generate another random number though, instead of the song title. I will see what I can do to fix that! Adapting both of the scripts, I only have one other problem... Matlab seems to complain when I want to pause it. I will type stop(p) in the command window and it doesn't seem to stop. In fact, the only way I can get Matlab to stop is to actually force quit it. Any ideas?
Thank you so much again!
UPDATE: I was using numbered music files, and importing the actaul titled songs seems to have fixed the naming error.
Thank you for your guidence into this! If anyone else wants to try this, my code is...
%HOLD control c to stop because it won't stop on it's own
mus = dir('mus/*.m4a');
dj = dir('dj/*.m4a');
while true
rd = randi(length(dj),1,1);
[a, Fs] = audioread(strcat(dj(rd).folder,"/",dj(rd).name));
plot(a(:,1))
title(dj(rd).name(1:end-4));
p = audioplayer(a,Fs);
play(p)
pause(length(a)/Fs);
for ii=1:randi(4)+3
rR = randi(length(mus),1,1);
[a2, Fs2] = audioread(strcat(mus(rR).folder,"/",mus(rR).name));
plot(a2(:,1))
title(mus(rR).name(1:end-4));
p = audioplayer(a2,Fs2);
play(p)
pause(length(a2)/Fs2);
end
end

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!