We provide complete mobile and web apps development solutions

Thursday, October 4, 2012

MediaPlayer/ MediaRecorder in Android


Package: android.media  

android.media   package provides various media interfaces in audi and video.
MediaPlayer class: it is used to control the playback of audio and video files.
MediaRecorder class: it is used to record audio and video.
MediaPlayer

MediaPlayer/MediaRecorder:
Steps to create MediaPlayer

Step1: Create an instance of the MediaPlayer
MediaPlayer mp1=new MediaPlayer();
Step2: specify the source path of media file
mp1=MediaPlayer.create(this,R.raw.my_music);
another method is to set a file from file system
mp1.setDataSource(path);
step3: prepare the mediaplayer for starting
mp1.prepare();

step4: start the playback of the audio

mp1.start();

mp1.resume();    // running state
mp1.pause();    // before stopping
mp1.stop();     // stopping stage
mp1.release();    //destroying state

example:
Context appCon=getApplicationContext();
If the media is from raw:
MediaPlayer mp1=MediaPlayer.create(appCon,Uri.parse("file://sdcard/newfile.mp3));
if the media file is from internet
MediaPlayer url=MediaPlayer.create(appCon,Uri.parse("http://websiteaddress/audio/filename.mp3"));

For Video Player playing
create videoView in xml

Initialize the VideoView in OnCreate()

VideoView vv=(VideoView)findviewById(R.id.videoid);

vv.setKeepScreenOn(true);
vv.setVideoPath("//sdcard/videofile.3gp");
if(vv.canSeekForward())
vv.seekTo(vv.getDuration()/2);
vv.start();
vv.stopPlayBack();









0 coment�rios:

Post a Comment

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts