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:
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();
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).
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");
mediaPlayerObject.start();
int pos = mediaPlayerObject.getCurrentPosition();
int duration = mediaPlayerObject.getDuration();
mediaPlayerObject.seekTo(pos + (duration-pos)/10);
if (!mediaPlayer.isLooping())
mediaPlayer.setLooping(true);
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);
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();
}
}
}
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:
- Initialize the Media Player with media to play.
- Prepare the Media Player for playback.
- Start the playback.
- Pause or stop the playback prior to its completing.
- Playback complete.
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
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 timemediaPlayerObject.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();
}
}
}
No comments :
Post a Comment