Is there a way to open music and play it?

Is there a way for MATLAB to read all music files and work like a music player

1 commentaire

Rahul Gulia
Rahul Gulia le 7 Mar 2022
How can we play Youtube songs links like this? I tried using the webread() function, but it gives me an error "Too many output arguments".

Connectez-vous pour commenter.

 Réponse acceptée

Anton Semechko
Anton Semechko le 5 Juil 2018
Yeah, there is a way. Here is an example:
% Get sample audio file from the web
[y,Fs]=webread('http://www.worldnationalanthem.com/wp-content/uploads/2015/05/canada-national-anthem-mp3-free-download.mp3');
% Create a 'player' object
PO=audioplayer(y,Fs);
% Play audio
play(PO)
% Stop audio
%stop(PO)
You can find more info about 'audioplayer' here

12 commentaires

Rainaire Hansford
Rainaire Hansford le 10 Juil 2018
what if I wanted to play a song from my computer? Is there a way to play like multiple songs one at a time and control the pause, play, stop, rewind, skip etc.
Thank you
Anton Semechko
Anton Semechko le 10 Juil 2018
Use 'audioread' function to load music files into Matlab from disk. After creating the 'player' object with the 'audioplayer' function (as shown above), you can play, pause, resume, and stop. Methods like rewind and skip are not supported by this class, so you will have to implement them yourself.
Walter Roberson
Walter Roberson le 10 Juil 2018
For flexibility beyond play / pause / resume / stop, you should probably look at the Audio System Toolbox, which permits you to do streaming-style audio operations. Or you could look at Java.
Rainaire Hansford
Rainaire Hansford le 4 Août 2018
So if I wanted to use audioread how can I write that in code?
Do you have like an example
[filename, filepath] = uigetfile({'*.wav', '*.mp3', *.*'}, 'Pick an audio file');
if ~ischar(filename); return; end %user canceled
fullname = fullfile(filepath, filename);
try
[y, fs] = audioread(fullname);
PO=audioplayer(y,Fs);
% Play audio
playblocking(PO)
catch ME
uiwait(msgbox('Could not open that file with audioread'));
end
Rainaire Hansford
Rainaire Hansford le 23 Sep 2018
Hi Walter is there a possible way to give me a functional example?
[filename, filepath] = uigetfile({'*.wav'; '*.mp3'; '*.*'}, 'Pick an audio file');
if ~ischar(filename); return; end %user canceled
fullname = fullfile(filepath, filename);
try
[y, Fs] = audioread(fullname);
PO=audioplayer(y,Fs);
% Play audio
playblocking(PO)
catch ME
uiwait(msgbox('Could not open that file with audioread'));
end
So the "*.*" is where I have to write the file name of the music?
Stephen23
Stephen23 le 1 Oct 2018
Modifié(e) : Stephen23 le 1 Oct 2018

"So the "*.*" is where I have to write the file name of the music?"

No. filename is where you write the filename of the music file.

And filepath is where you write the filepath of the music file.

Walter Roberson
Walter Roberson le 1 Oct 2018
The uigetfile() code prompts the user to select a file. If you want to skip that, then store the name of the file (preferably complete with directory) in fullname and then carry on from the "try" statement.
Rainaire Hansford
Rainaire Hansford le 17 Oct 2018
Ok I tried running this code and put in the directory but it was keep given me error. Could some share a example code I might be missing something.
Sorry for taking so long.
fullname = 'C:\Users\rain\Documents\MATLAB\ProjectGreen\abalone_sound7.wav'; %change as required
try
[y, Fs] = audioread(fullname);
PO=audioplayer(y,Fs);
% Play audio
playblocking(PO)
catch ME
uiwait(msgbox('Could not open that file with audioread'));
end

Connectez-vous pour commenter.

Plus de réponses (1)

Rainaire Hansford
Rainaire Hansford le 21 Oct 2018

1 vote

Yes I got it thank you. Now next step is to implement pause and stop and play in this code cause I really need it lol But Walter your the best

1 commentaire

Walter Roberson
Walter Roberson le 21 Oct 2018
Use play() instead of playblocking() . Then put in a button that uses the pause() method on the audioplayer object.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by