HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Search
ADDTOFAVORITES
MARKASVIEWED
ADDANOTE
Services
Addacomment
Addtofavorites
Markasviewed
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
1/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
108
Addanote
3
37.8K
Like(3)
(0)
+Mobile
WewilluseresourceIdinthistutorialfortheplaybackof
audio(.mp3)file.Afterreadingthistopicyouwillbeableto
makeabasicaudioplayer.
AndroidhasMediaPlayerclasswhichholdsthefunctionality
thatweneedtobuildamusicplayer.Inthistutorialwewill
coverbasicmethodswiththeirexplanationssothatusercan
enhancefunctionalityasperneed.Inthistutorialwewilluse
rawfoldertoholdtheaudiofilehavingextension.mp3for
theplaybackofspecifiedfile.
Objective:
Toprovidefamiliarityabouttheplaybackofmediafiles.
ImplementationofMediaPlayerclassforplayback.
Toprovidefunctionalityofseekbartocontrolpositionof
playback.
Classesusedtoperformthistask:
MediaPlayerclass.
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
2/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Handlerclass.
Runnableclass
Introductiontoclasses:
MediaPlayerClass:InandroidMediaPlayerclassisvery
powerfulwhichcontainswiderangeoffunctionalitylikeaudio
playback,videoplaybacketc.MediaPlayerclasscontainsthe
followingmainmethods:
staticMediaPlayercreate(Contextctx,intresourceId):
Thismethodisusedtosetthepathofaudiofilewithin
contextandresourceIdwill
givetheidwithinassetfolder.
booleanisPlaying():Thismethodwilltellthat
MediaPlayerisplayingornot.
IntgetDuration():Thismethodwillreturnthetotal
durationofcurrentfile.
IntgetCurrentPosition():Thismethodwillreturnthe
currentpostionofplayback.
voidstart():Thismethodwillstarttheplaybackof
MediaPlayer.
voidpause():Thismethodwillpausetheplaybackof
audiofile.
voidstop():Thismethodwillstoptheplaybackofaudio
file.
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
3/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
voidreset():ThismethodwillresettheMediaPlayerto
uninitializedstate.
voidseekTo(intmillisec):Thismethodwillseekthe
playbacktospecifiedseektimeinmillisecargument.
voidsetLooping(booleanvalue):Thismethodwillsetthe
repetitionofMediaPlayeraspervaluegiven,eithertrue
orfalse.
HandlerClass:Handlerclassisusedtohandlethethreads.We
haveusedpostDelayed()methodtoproducedelayof1second.
Hereisbelowthemethodsyntax:
publicfinalbooleanpostDelayed(Runnableobject,long
delayMilli):Thismethodisusedtointroducethedelayin
executionofthreadwhichisobjectofRunnableand
delayMilliisthedelaytimeinmilliseconds.
RunnableClass:Runnableclassisusedtocontrolthe
functionalityofthreadsasperneed.Itcontainsthe
followingonlyonemethod:
abstractvoidrun():Thismethodisusedtowritethe
codeforimplementationofthread.
StepsInvolved:Herearethebelowstepsthatarerequiredto
completethisproject.
Step1:Createanewandroidprojectineclipsewithname
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
4/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
MediaPlaybackandpackagenameshouldbe
com.nkm.mediaplayback.
Figure1:ProjectnameisMediaPlaybackasshowninfigure.
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
5/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure2:Packagenameiscom.nkm.mediaplayback
Step2:Rightclickontheresfolderandchoose>New>Folder
asshownbelow:
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
6/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure3:ChooseFolderoptionasshowninfigure.
Step3:Givenametothefolderrawthenclickonfinishas
shownbelow:
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
7/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure4:Foldernamemustberawasshowninfigure.
Step4:Putyourdesiredaudiofileinthisfolder.
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
8/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure5:Win8.mp3ismediafileasshowninfigure.
Step5
Listing1:Pastethefollowingcodeinsidethemain.xmlfile.
<?xmlversion="1.0"encoding="utf8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/
apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_shown"
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
9/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120px"
android:text=""
android:textSize="42px"/>
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/play_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:gravity="center"
android:text="Play"/>
<Button
android:id="@+id/pause_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:text="Pause"/>
</RelativeLayout>
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
10/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Step6
Listing2:Pastethefollowingcodeinsidethe
MediaPlaybackActivity.javafile.
packagecom.nkm.mediaplayback;
importandroid.app.Activity;
importandroid.media.MediaPlayer;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.SeekBar;
importandroid.widget.TextView;
publicclassMediaPlaybackActivityextendsActivityimplem
entsOnClickListener{
SeekBarseek_bar;
Buttonplay_button,pause_button;
MediaPlayerplayer;
TextViewtext_shown;
HandlerseekHandler=newHandler();
/**Calledwhentheactivityisfirstcreated.*/
@Override
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
11/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getInit();
seekUpdation();
publicvoidgetInit(){
seek_bar=(SeekBar)findViewById(R.id.see
play_button=(Button)findViewById(R.id.p
k_bar);
lay_button);
pause_button=(Button)findViewById(R.id.
pause_button);
text_shown=(TextView)findViewById(R.id.
text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player=MediaPlayer.create(this,R.raw.wi
seek_bar.setMax(player.getDuration());
Runnablerun=newRunnable(){
@Override
publicvoidrun(){
};
n8);
seekUpdation();
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
12/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
publicvoidseekUpdation(){
seek_bar.setProgress(player.getCurrentPosi
tion());
seekHandler.postDelayed(run,1000);
@Override
publicvoidonClick(Viewview){
switch(view.getId()){
caseR.id.play_button:
text_shown.setText("Playing...");
player.start();
break;
caseR.id.pause_button:
player.pause();
text_shown.setText("Paused...");
CHANNELS
COURSES
Step7:Runyourprojectandhereistheoutput:
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
LOGIN
13/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure6:StateofMediaPlayerwhileplayback.
Know how to keep MrBool Online S U P P O R T U S
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
14/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Figure7:StateofMediaPlayerwhilepause
Explanantionofabovecode:
Listing3:ThegetInit()method:
publicvoidgetInit(){
seek_bar=(SeekBar)findViewById(R.id.see
k_bar);
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
15/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
play_button=(Button)findViewById(R.id.p
lay_button);
pause_button=(Button)findViewById(R.id.
pause_button);
text_shown=(TextView)findViewById(R.id.
text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player=MediaPlayer.create(this,R.raw.wi
seek_bar.setMax(player.getDuration());
n8);
Thisisuserdefinedmethodwhichisusedtoinitializethe
GUIcontrols.
Herethefollowingassignmentisusedforthecreationof
MediaPlayerclassobjectwithnameplayerwhichassignmentof
pathoftheaudiofile.Inthiscasewin8.mp3isourfilewhich
residesintherawfolderunderresfolder.
player=MediaPlayer.create(this,R.raw.win8);
ThesetMax()methodofSeekbarclass:Thismethodwillset
thetotaldurationofseekbartothetotaldurationofcurrent
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
16/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
audiofile.
seek_bar.setMax(player.getDuration());
Therun()method:Thefollowingcodeisusedtoinitiatethe
Runnableclasswithanobjectnamedasrunasshownbelow.
Listing4:Inrun()methodseekUpdation()methodiscalled
whichmeansseekUpdationwillcalledagainandagaintillthe
playback.
Runnablerun=newRunnable(){
@Override
publicvoidrun(){
};
seekUpdation();
TheseekUpdation()method:Thismethodisusedtoupdate
theseekbarwithdelayof1second.
publicvoidseekUpdation(){
seek_bar.setProgress(player.getCurrentPosi
tion());
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
17/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
seekHandler.postDelayed(run,1000);
Hereinthismethodthefollowingmethodwillsettheseekbar
tocurrentpositionoftheplayback.
seek_bar.setProgress(player.getCurrentPosition();
Andthefollowingmethodwillintroducedelayof1secondin
executionofrun()method.
seekHandler.postDelayed(run,1000);
WhatisCovered?
Inthisarticlewehavelearnedhowtoplayaudiofilesusing
MediaPlayerclass,howtohandleseekbarpositionwithHandler
classandhowtocontrolthreadexecutioninandroid.
Hopeyoulikedthearticle,seeyounexttime.
Also read
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
18/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
HowtohandleMediawithAndroid
HowtoExtractMetadatafromMediafileinAndroid
VarunArora
IamasoftwaredeveloperfromIndiawithhandsonexperienceonjava,html
forover5years.
Whatdidyouthinkofthispost?
Like(3)
(0)
108
3
9
37.8K
Addyourcomment
VishwajitPalankar
howchangethesongscurrenttimewith
OnSeekBarChangeListener?
[+1monthago]Answerit
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
19/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Withyourhelp,wecan
keepprovidingfree
contentandhelping
youtobeabetter
professional
support us
Related Posts
Post
Post
Post
20/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
Post
Post
Video
Video
Show more
Contactus | Publishyourpost
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
21/22
30/1/2016
HowtoplayAudioFilesinAndroidwithaSeekBarfeatureandMediaPlayerclass
MrBoolPlaceforsoftwaredevelopers
LikePage
81klikes
Bethefirstofyourfriendstolikethis
MrBool.com
Copyright2016allrightsreserved
towww.web03.net
http://mrbool.com/howtoplayaudiofilesinandroidwithaseekbarfeatureandmediaplayerclass/28243
22/22