how to play audio files in android with a seekbar feature and mediaplayser class

Upload: omar-muniz

Post on 01-Mar-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    1/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 1/22

    Search

    ADD TO FAVORITES MARK AS VIEWED ADD A NOTE

    H o w t o p l a y A u d i o F i l e s i n

    A n d r o i d w i t h a S e e k a r

    f e a t u r e a n d M e d i a P l a y e r

    c l a s s

    I n t h i s t u t o r i a l w e w i l l e x p l a i n h o w t o p l a y

    A u d i o f i l e s i n A n d r o i d u s i n g M e d i a P l a y e r

    c l a s s . Services

    Add a comment

    Add to favorites

    Mark as viewed

    https://googleads.g.doubleclick.net/aclk?sa=L&ai=C5E4qMRWtVrzZAoekmQSpwbbYAf3nup8IlZPr3tYCx-eivcABEAEgn4PTLGDdhICAiBnIAQKoAwHIA8EEqgSNAU_QZnwfBgECzLIjR9DZ-BzbAxbzDb_sht77tYTNgSxJqGxUUhVUeZLo7aP103cDPrUE1kUEUWpFEQp8Xvd5-M1cfkS9Z7mNf_PZQe6EmvTrm4RD7gyMNcwA_VNkVSelfcapWph8y-zzft-2LsLELyJDb22if7r4s8TVfaziSDJLCGD7jqdEsKxZPAk03aAGAoAH9a6PRKgHpr4b2AcB2BMB&num=1&sig=AOD64_2MWaY53y9xUHWxlMHyddIqrFB-8g&client=ca-pub-1771753930805675&adurl=http://www.radisson.com/http://mrbool.com/favorite/add_finished.asp?comp=28243http://mrbool.com/favorite/addfavorite.asp?comp=28243http://www.mrbool.com/rte3/addcomment.asp?idcomp=28243http://mrbool.com/articles/addnote.asp?idcomp=28243http://mrbool.com/favorite/add_finished.asp?comp=28243http://mrbool.com/favorite/addfavorite.asp?comp=28243http://mrbool.com/
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    2/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 2/22

    Like (3) (0)

    We will use resource Id in this tutorial for the playback of

    audio(.mp3) file. After reading this topic you will be able to

    make a basic audio player.

    Android has MediaPlayer class which holds the functionality

    that we need to build a music player. In this tutorial we will

    cover basic methods with their explanations so that user can

    enhance functionality as per need. In this tutorial we will use

    raw folder to hold the audio file having extension .mp3 for

    the playback of specified file.

    Objective :

    To provide familiarity about the playback of media files.

    Implementation of MediaPlayer class for playback.To provide functionality of seekbar to control position of

    playback.

    Classes used to perform this task:

    MediaPlayer class.

    Add a note

    +Mobile

    108

    9

    3

    37.8K

    http://mrbool.com/channel/mobilehttp://mrbool.com/articles/addnote.asp?idcomp=28243
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    3/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 3/22

    Handler class.

    Runnable class

    Introduction to classes:

    MediaPlayer Class : In android MediaPlayer class is very

    powerful which contains wide range of functionality like audio

    playback, video playback etc. MediaPlayer class contains the

    following main methods :

    static MediaPlayer create(Context ctx, int resourceId) :

    This method is used to set the path of audio file with in

    context and resourceId will

    give the id with in asset folder.

    boolean isPlaying() : This method will tell that

    MediaPlayer is playing or not.

    Int getDuration() : This method will return the total

    duration of current file.

    Int getCurrentPosition() : This method will return the

    current postion of playback.

    void start() : This method will start the playback ofMediaPlayer.

    void pause() : This method will pause the playback of

    audio file.

    void stop() : This method will stop the playback of audio

    file.

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    4/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 4/22

    void reset() : This method will reset the MediaPlayer to

    uninitialized state.

    void seekTo(int millisec) : This method will seek the

    playback to specified seek time in millisec argument.

    void setLooping(boolean value) : This method will set the

    repetition of MediaPlayer as per value given, either true

    or false.

    Handler Class : Handler class is used to handle the threads. We

    have used postDelayed() method to produce delay of 1 second.

    Here is below the method syntax:

    public final boolean postDelayed(Runnable object, long

    delayMilli) : This method is used to introduce the delay in

    execution of thread which is object of Runnable and

    delayMilli is the delay time in milliseconds.

    Runnable Class : Runnable class is used to control the

    functionality of threads as per need. It contains the

    following only one method :

    abstract void run() : This method is used to write the

    code for implementation of thread.

    Steps Involved : Here are the below steps that are required to

    complete this project.

    Step 1: Create a new android project in eclipse with name

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    5/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 5/22

    MediaPlayback and package name should be

    com.nkm.mediaplayback.

    Figure 1: Project name is Media Playback as shown in figure.

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    6/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 6/22

    Figure 2 : Package name is com.nkm.mediaplayback

    Step 2 : Right click on the res folder and choose> New>Folder

    as shown below:

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    7/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 7/22

    Figure 3: Choose Folder option as shown in figure.

    Step 3 : Give name to the folder raw then click on finish as

    shown below:

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    8/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 8/22

    Figure 4 : Folder name must be raw as shown in figure.

    Step 4 : Put your desired audio file in this folder.

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    9/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 9/22

    Figure 5 : Win8.mp3 is media file as shown in figure.

    Step 5

    Listing 1: Paste the following code inside the main.xml file.

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    10/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 10/22

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerHorizontal="true"

    android:layout_marginTop="120px"

    android:text=""

    android:textSize="42px" />

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    11/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 11/22

    Step 6

    Listing 2: Paste the following code inside the

    MediaPlaybackActivity.java file.

    package com.nkm.mediaplayback;

    import android.app.Activity;

    import android.media.MediaPlayer;

    import android.os.Bundle;

    import android.os.Handler;

    import android.view.View;import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.SeekBar;

    import android.widget.TextView;

    public class MediaPlaybackActivity extends Activity implem

    ents OnClickListener {

    SeekBar seek_bar;

    Button play_button, pause_button;MediaPlayer player;

    TextView text_shown;

    Handler seekHandler = new Handler();

    /** Called when the activity is first created. */

    @Override

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    12/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 12/22

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    getInit();

    seekUpdation();

    }

    public void getInit() {

    seek_bar = (SeekBar) findViewById(R.id.see

    k_bar);

    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

    n8);

    seek_bar.setMax(player.getDuration());

    }

    Runnable run = new Runnable() {

    @Override

    public void run() {

    seekUpdation();

    }

    };

    30/1/2016 H t l A di Fil i A d id ith S kB f t d M di Pl l

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    13/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 13/22

    public void seekUpdation() {

    seek_bar.setProgress(player.getCurrentPosi

    tion());

    seekHandler.postDelayed(run, 1000);

    }

    @Override

    public void onClick(View view) {

    switch (view.getId()) {

    case R.id.play_button:

    text_shown.setText("Playing...");

    player.start();

    break;

    case R.id.pause_button:

    player.pause();

    text_shown.setText("Paused...");

    }

    }

    Step 7 : Run your project and here is the output:

    CHANNELS COURSES LOGIN

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/course/http://mrbool.com/articles/listcomp.asp
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    14/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 14/22

    Figure 6 : State of MediaPlayer while playback.K n o w h o w t o k e e p M r B o o l O n l i n e S U P P O R T U S

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    https://secure.mrbool.com/support-us/
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    15/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 15/22

    Figure 7 : State of MediaPlayer while pause

    Explanantion of above code :

    Listing 3: The getInit() method :

    public void getInit() {

    seek_bar = (SeekBar) findViewById(R.id.see

    k_bar);

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    16/22

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 16/22

    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

    n8);

    seek_bar.setMax(player.getDuration());

    }

    This is user defined method which is used to initialize the

    GUI controls.

    Here the following assignment is used for the creation of

    MediaPlayer class object with name player which assignment of

    path of the audio file. In this case win8.mp3 is our file which

    resides in the raw folder under res folder.

    player = MediaPlayer.create(this, R.raw.win8);

    The setMax() method of Seekbar class : This method will set

    the total duration of seekbar to the total duration of current

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    17/22

    p y y

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 17/22

    audio file.

    seek_bar.setMax(player.getDuration());

    The run() method : The following code is used to initiate the

    Runnable class with an object named as run as shown below.

    Listing 4: In run() method seekUpdation() method is called

    which means seekUpdation will called again and again till the

    playback.

    Runnable run = new Runnable() {

    @Override

    public void run() {

    seekUpdation();

    }

    };

    The seekUpdation() method : This method is used to update

    the seekbar with delay of 1 second.

    public void seekUpdation() {

    seek_bar.setProgress(player.getCurrentPosi

    tion());

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    18/22

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 18/22

    seekHandler.postDelayed(run, 1000);

    }

    Here in this method the following method will set the seekbar

    to current position of the playback.

    seek_bar.setProgress(player.getCurrentPosition();

    And the following method will introduce delay of 1 second in

    execution of run() method.

    seekHandler.postDelayed(run, 1000);

    What is Covered ?

    In this article we have learned how to play audio files using

    MediaPlayer class, how to handle seekbar position with Handler

    class and how to control thread execution in android.

    Hope you liked the article, see you next time.

    Also read

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    19/22

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 19/22

    How to handle Media with Android

    How to Extract Meta-data from Media file in Android

    Varun Arora

    I am a software developer from India with hands on experience on java, html

    for over 5 years.

    What did you think of this post? Like (3) (0)

    Add your commentVishwajit Palankar

    how change the songs current time with

    OnSeekBarChangeListener?

    [+1 month ago] Answer it

    108 9

    3 37.8K

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/rte3/addcomment.asp?idcomp=28243&idco_pai=63599http://mrbool.com/rte3/addcomment.asp?idcomp=28243http://www.mrbool.com/space.asp?id=301606http://www.mrbool.com/space.asp?id=301606http://mrbool.com/how-to-extract-meta-data-from-media-file-in-android/28130http://mrbool.com/how-to-handle-media-with-android/25875
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    20/22

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 20/22

    R e l a t e d P o s t s

    P o s t

    G o o g l e D i r e c t i o n s A P I : T r a c i n g r o u t e s i n A n d r o i d

    P o s t

    I n t r o d u c t i o n t o A n d r o i d S t u d i o I D E

    P o s t

    I n t e g r a t i n g A n d r o i d A p p l i c a t i o n s w i t h F a c e b o o k

    s u p p o r t u s

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    https://secure.mrbool.com/support-us/http://mrbool.com/integrating-android-applications-with-facebook/33934http://mrbool.com/introduction-to-android-studio-ide/34022http://mrbool.com/google-directions-api-tracing-routes-in-android/34083
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    21/22

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 21/22

    P o s t

    S e n d i n g E m a i l s i n A n d r o i d A p p l i c a t i o n s

    P o s t

    W o r k i n g w i t h A s y n c T a s k i n A n d r o i d

    V i d e o

    J S O N d a t a : H o w t o c r e a t e d i f f e r e n t i n t e n t s - A n d r o i d C o u r s e -

    P a r t 3 2

    V i d e o

    J S O N d a t a : H o w t o c r e a t e s t e a m s - A n d r o i d C o u r s e - P a r t 3 1

    S h o w m o r e

    Contact us | Publish your post

    30/1/2016 How to play Audio Files in Android with a SeekBar feature and MediaPlayer class

    http://mrbool.com/portal/publish.asphttp://mrbool.com/portal/contact.asphttp://mrbool.com/channel/mobilehttp://mrbool.com/-json-data-how-to-create-steams-android-course-part-31/33518http://mrbool.com/-json-data-how-to-create-different-intents-android-course-part-32/33519http://mrbool.com/working-with-asynctask-in-android/33548http://mrbool.com/sending-emails-in-android-applications/33810
  • 7/26/2019 How to Play Audio Files in Android With a SeekBar Feature and MediaPlayser Class

    22/22

    http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243 22/22

    MrBool.com Copyright 2016 - all rights reservedto www.web-03.net

    Be the first of your friends to like this

    MrBool - Place for software

    81k likesLike Page

    https://www.facebook.com/MrBool.Website/https://www.facebook.com/MrBool.Website/https://www.facebook.com/MrBool.Website/https://www.facebook.com/giri.dharan.98096https://www.facebook.com/kabeer.mahlari.9https://www.facebook.com/abhinav.ramanhttps://www.facebook.com/guttireddy.akhilahttps://www.facebook.com/karunkumarmusukula369https://www.facebook.com/ryan.ray.5851127https://www.facebook.com/pradip.badehttps://www.facebook.com/pulackalshihab034https://www.facebook.com/asif.hellriderhttp://www.web-03.net/http://mrbool.com/http://mrbool.com/feed/https://plus.google.com/+Mrboolhttps://twitter.com/mrboolteamhttps://www.facebook.com/MrBool.Website