Recently i got a request to write a blog on making a media player on MATLAB. This example shows you a simple way to make a mp3 player in steps. In this step, i will write how to setup the initial background and design base GUI using GUIDE.
Using a mp3 reader
MATLAB does not have an inbuilt mp3 reader function. mp3 is a encoded which you need to convert to wavfile before reading it. But Dont worry, matlab central provides you all.
go to this download link and download the zip file.
http://www.mathworks.com/matlabcentral/fileexchange/6152-mp3write-and-mp3read
extract the folder and save it to your current directory with name mp3readwrite.
Now you need to add the path of these files in your matlab file.
Generating gui fig using GUIDE
first step is to generate a fig file which contains the basic component of a media player. (As in fig 1)
save this file as mmp.fig and this will generate a mmp.m file
Using a mp3 reader
MATLAB does not have an inbuilt mp3 reader function. mp3 is a encoded which you need to convert to wavfile before reading it. But Dont worry, matlab central provides you all.
go to this download link and download the zip file.
http://www.mathworks.com/matlabcentral/fileexchange/6152-mp3write-and-mp3read
extract the folder and save it to your current directory with name mp3readwrite.
Now you need to add the path of these files in your matlab file.
To verify that you have done right, put a mp3 file with name playfile.mp3 in your current directory and run this commandpath(path,'mp3readwrite');
if you get undefined function or variable mp3readwrite, then check the path of the file inside folder and change the path accordingly.[y f]=mp3read('playfile.mp3');
Generating gui fig using GUIDE
first step is to generate a fig file which contains the basic component of a media player. (As in fig 1)
- A playlist (ListBox)
- An Add File button (PushButton)
- A Volume Silder (Slider)
- A play Progress Slider(Slider)
- Play/Pause Button (Push Button)
- Stop Button (Push Button)
- Visualization (Axis)
save this file as mmp.fig and this will generate a mmp.m file
Fig 2 : GUIDE TAGS
In the previous section, We have seen the basic GUI fig. In this section, we would be learning to add basic features in this player.
Making the playlist work
Making the playlist is easy task. Since we need the full path for playing purpose and we need to show only title information in playlist. So we will maintain a spearate list playfiles for full path.
Add this to your opening function
As you can see, uigetfile is used to open a filebrowser and then i store the filename and its path into a list playfiles.
I also update the playlist string property with the file name which is shown on playlist on player.
How to play Audio
There are some option like wavplay or sound in matlab which can play your audio. But you cannot pause or resume it once you have started it. We will use audioplayer class of MATLAB for our purpose.
This small code tells you how to play a wav file by audioplayer object.
first read the .wav file using wavread
Now you can use the play/pause/resume/stop methods to do the respective operations. As an Example:
first make an new m file and save it as UpdateWin.m and write the following in this
Now make a function f where you can pass this pl.
Whole piece of code will look like as
Making the playlist work
Making the playlist is easy task. Since we need the full path for playing purpose and we need to show only title information in playlist. So we will maintain a spearate list playfiles for full path.
Add this to your opening function
function mmp_OpeningFcn(hObject, eventdata, handles, varargin)handles.playfiles=[];% Update handles structureguidata(hObject, handles);
We have made the add button. Modify the callback for pladd
function pladd_Callback(hObject, eventdata, handles)% hObject handle to pladd (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)[f g]=uigetfile;p=get(handles.playlist,'String');p{length(p)+1}=[f];set(handles.playlist,'String',p);handles.playfiles{length(handles.playfiles)+1}=[g '\' f];guidata(hObject,handles);
As you can see, uigetfile is used to open a filebrowser and then i store the filename and its path into a list playfiles.
I also update the playlist string property with the file name which is shown on playlist on player.
How to play Audio
There are some option like wavplay or sound in matlab which can play your audio. But you cannot pause or resume it once you have started it. We will use audioplayer class of MATLAB for our purpose.
This small code tells you how to play a wav file by audioplayer object.
first read the .wav file using wavread
now make an instance of audioplayer with this y and f.[y f]=wavread('aa.wav');
pl is the handle to this audioplayer.pl=audioplayer(y,f);
Now you can use the play/pause/resume/stop methods to do the respective operations. As an Example:
You can get the total sample and current sample number using get method over pl object asplay(pl);
pause(pl);resume(pl);
cs=get(pl,'CurrentSample');
ps=get(pk,'TotalSamples');To get the progress of the sound file, you can use
ctime=cs/ps;you can use isplaying option to check whether the pl is running or not.
f=isplaying(pl);You can combine this with mp3reader to read a mp3 file
There is one more cool thing to do here. You can make a function which gives you the current progress of music and it will be called automatically by matlab after a certain time if you give its name to audioplayer object. let us see how we can do it.cfile='aa.mp3';extn=cfile[end-2:end];switch (extn)case 'mp3'[y f]=mp3read(cfile);case 'wav'[y f]=wavread(cfile);endpl=audioplayer(y,f);play(pl);
first make an new m file and save it as UpdateWin.m and write the following in this
function UpdateWin(pl)
%global pl;
c=get(pl,'CurrentSample');
t=get(pl,'TotalSamples');
disp(c/t);
Now make a function f where you can pass this pl.
f=@() UpdateWin(pl);Now set this f as timer function of pl.
set(pl,'TimerFcn','f()');Timercall time is automatically set as TimerPeriod: 0.0500 in seconds
Whole piece of code will look like as
cfile='aa.mp3';
extn=cfile[end-2:end];
switch (extn)
case 'mp3'
[y f]=mp3read(cfile);
case 'wav'
[y f]=wavread(cfile);
end
pl=audioplayer(y,f);
f=@() UpdateWin(pl);
set(pl,'TimerFcn','f()');
play(pl);
Adding Playing Callback
In playbuttonCallback function add the following code for starting the playing of music. Steps we are following are
1. Selecting the file name from playlist
making other call backs are pretty easy.
just write following in stop callback
Noe it is time to move your player slider to show the progress.
Update your UpdateWin file as following
In playbuttonCallback function add the following code for starting the playing of music. Steps we are following are
1. Selecting the file name from playlist
v=get(handles.playlist,'Value');2. create the player and assign it to the player in handles structure.
cfile=handles.playfiles{v};
extn=cfile[end-2:end];3. Now play this if the player is not running else pause it
switch (extn)
case 'mp3'
[y f]=mp3read(cfile);
case 'wav'
[y f]=wavread(cfile);
end
pl=audioplayer(y,f);
f=@() UpdateWin(pl);
set(pl,'TimerFcn','f()');
handles.pl=pl;
if isrunning(pl)Adding other callbacks
pause(pl);
else
resume(pl);
end
making other call backs are pretty easy.
just write following in stop callback
stop(pl);Making the playersilder move
Noe it is time to move your player slider to show the progress.
Update your UpdateWin file as following
function UpdateWin(pl)Enjoy Your player is working now
c=get(pl,'CurrentSample');t=get(pl,'TotalSamples');
%disp(c/t);
h=guidata(pl);
set(h.playslider,'value',c/t);