Making a MATLAB Media Player

  • Posted on: 25 March 2011
  • By: Anand
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.

path(path,'mp3readwrite');

To verify that you have done right, put a mp3 file with name playfile.mp3 in your current directory and run this command

[y f]=mp3read('playfile.mp3');

if you get undefined function or variable mp3readwrite, then check the path of the file inside folder and change the path accordingly.

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)

Fig 1 : sample media player GUI
  1. A playlist (ListBox)
  2. An Add File button (PushButton)
  3. A Volume Silder (Slider)
  4. A play Progress Slider(Slider)
  5. Play/Pause Button (Push Button)
  6. Stop Button (Push Button)
  7. Visualization (Axis)

See fig 2 to see the sample tag name used in this example
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 

function mmp_OpeningFcn(hObject, eventdata, handles, varargin)
handles.playfiles=[];
% Update handles structure
guidata(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

[y f]=wavread('aa.wav');

now make an instance of audioplayer with this y and f.

pl=audioplayer(y,f);

pl is the handle to this audioplayer.
Now you can use the play/pause/resume/stop methods to do the respective operations. As an Example:

play(pl);
pause(pl);

resume(pl);

You can get the total sample and current sample number using get method over pl object as 

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

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);
play(pl);

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.
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.

[email protected]() 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);

[email protected]() 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
v=get(handles.playlist,'Value');
cfile=handles.playfiles{v};

2. create the player and assign it to the player in handles structure.

extn=cfile[end-2:end];
switch (extn)
  case 'mp3'
        [y f]=mp3read(cfile);
  case  'wav'
       [y f]=wavread(cfile);
end
pl=audioplayer(y,f);
[email protected]() UpdateWin(pl);
set(pl,'TimerFcn','f()');
handles.pl=pl;

3. Now play this if the player is not running else pause it

if isrunning(pl)
    pause(pl);
else
  resume(pl);
end

Adding other callbacks
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)
c=get(pl,'CurrentSample');t=get(pl,'TotalSamples');
%disp(c/t);
h=guidata(pl);
set(h.playslider,'value',c/t);

Enjoy Your player is working now