Search This Blog

Support my cause by downloading

Thursday, 27 September 2012

Media player example in android with seek bar

Playing audio in android is very simple. It provides a lots of tool to handle life cycle of Media Player. We can play a audio file from following source

  • Playing from Resource
  • Playing from local i.e sdcard 
  • Playing from online URL 
Android provide some listener to handle how your song will play, to handle when your song completed, to forward song or to backward song.
I have make one simple application of media player that will play media from above three specified source. Lets discuss one by one


  • Playing from resource -  suppose you want to give one custom notification in an application. so better to keep this sound file inside raw folder and play it from there using Media Player. In this simple we do not need any bigger complexity                 
  • 1:  MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sor);  
    2:  mediaPlayer.start();  
       

  • Playing from sdcard or online - These both ways are quite similar. We have to prepare Media Player to be ready for playing music either from sdcard or online URL.  But in online URL case we used  mediaPlayer.prepareAsync(); as we do not know how much time URL will take to respond. Immediately when Media Player prepared, called onPreparedListener();  and we start music. 
When music file played completely then listener onCompletionListener() gives us a way to start a new song or played it again. 
These are piece of line that will play a online URL

1:       /**  
2:        * Give the online url  
3:        */  
4:       private void PlayOnlineUrl() {  
5:            String url = "http://www.gaana.mp3"; // your URL here  
6:            MediaPlayer mediaPlayer = new MediaPlayer();  
7:            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
8:            try {  
9:                 mediaPlayer.setDataSource(url);  
10:            } catch (IllegalArgumentException e) {  
11:                 e.printStackTrace();  
12:            } catch (SecurityException e) {  
13:                 e.printStackTrace();  
14:            } catch (IllegalStateException e) {  
15:                 e.printStackTrace();  
16:            } catch (IOException e) {  
17:                 e.printStackTrace();  
18:            }  
19:            mediaPlayer.prepareAsync();  
20:            // You can show progress dialog here untill it prepared to play  
21:            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
22:                 @Override  
23:                 public void onPrepared(MediaPlayer mp) {  
24:                      // Now dismis progress dialog, Media palyer will start playing  
25:                      mp.start();  
26:                 }  
27:            });  
28:            mediaPlayer.setOnErrorListener(new OnErrorListener() {  
29:                 @Override  
30:                 public boolean onError(MediaPlayer mp, int what, int extra) {  
31:                      // dissmiss progress bar here. It will come here when  
32:                      // MediaPlayer  
33:                      // is not able to play file. You can show error message to user  
34:                      return false;  
35:                 }  
36:            });  
37:       }  

                                                       Screen Shot will be look like

Media player
                         


Main thing remains now is, Extracting information of song file to show in the list. Even though i have not used this feature anywhere in my sample media player example but android provide a way to extract information about song like image, Song name, Album name, Year, Rating etc

If you are using Media query to fetch URL(From content provider) then you will get all information using cursor index else you can use MediaMetadataReceiver  class to find information about currently playing song.  These information will return as null if not available so checking these condition to avoid UN-necessary crash of application is very useful.

Note - MediaMetadataReceiver   is available only after API level 10


Generally this application run Media Player inside the main thread. But we recommend to play a media player inside a service so we have to bind a service with activity .Please see this how to bind a service and updating UI
                                             

                                        Download Source Code of Media Player

Android News and source code