Breaking News


Tuesday, 21 January 2014

Android MediaPlayer Example

MediaPlayer class is  used to control playback of audio/video files and streams.
Using MediaPlayer class we can control the behavioyr of player like volume control, whether to repeat the current tarck or not, stop or pause the playback etc.

Steps:

  1. Initialize the Media Player with media to play.
  2. Prepare the Media Player for playback.
  3. Start the playback. 
  4. Pause or stop the playback prior to its completing.
  5. Playback complete.
To play a media resource you need to create a new MediaPlayer object, initialize it with a media
source, and prepare it for playback. 

When you have finished playing , call the release method on your Media Player object to free the resources associated with the MediaPlayer

mediaPlayer.release();

Preparing Audio for Playback


To play back audio content using the Media Player, we need to create a new Media Player object and
set the data source of the audio (pass as paarmeter).

  • A resource identifier
  • A URI to a local file using the file
  • A URI to an online audio resource as a URL 
  • A URI to a local Content Provider row
MediaPlayer filePlayer = MediaPlayer.create(appContext,
Uri.parse("file:///sdcard/localfile.mp3"));


MediaPlayer urlPlayer = MediaPlayer.create(appContext,
Uri.parse("http://site.com/audio/audio.mp3"));


MediaPlayer contentPlayer = MediaPlayer.create(appContext,
Settings.System.DEFAULT_RINGTONE_URI)



we can also use setDataSource method to specify the resource
like

myMediaPlayerObject.setDataSource("/sdcard/music/tarck1.mp3");


Controlling The PlayBack

Skip for a particular time

mediaPlayerObject.start();
int pos = mediaPlayerObject.getCurrentPosition();
int duration = mediaPlayerObject.getDuration();
mediaPlayerObject.seekTo(pos + (duration-pos)/10);

Repeating the Audio Playback track

We can use the isLooping and setLooping methods to specify if the media being played should repeat or loop when it completes.

if (!mediaPlayer.isLooping())
mediaPlayer.setLooping(true);

Controlling the Volume of Media Player


We acn also  control the volume for each channel during playback using the setVolume method. It takes
a scalar float value between 0 and 1 for both the left and right channels (where 0 is silent and 1 is
maximum volume).

mediaPlayer.setVolume(1f, 0.5f);


MediaPlayer Example:


public class MediaPalyerActivity  extends Activity
{
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                
                MediaPlayer  myMediaPlayer=new MediaPlayer();
                
                try
                {
                    myMediaPlayer.setDataSource("/sdcard/music/tarck1.mp3");
                    myMediaPlayer.prepare();
                    myMediaPlayer.start();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
        }
}
Share This
Blogger
Facebook
Disqus

comments powered by Disqus

No comments :

Post a Comment

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids