How to play video and audio on Android
There is more than one way, to play media files on an Android phone, let me show you two of them.

Audio:
MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.
- //set up MediaPlayer
- MediaPlayer mp = new MediaPlayer();
- try {
- mp.setDataSource(path+"/"+fileName);
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- mp.prepare();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mp.start();
- }
Video:
In this example, I open a video file, and decide if I want it to autoplay after it is loaded.
You propably want to store your video files, on the sd card. You can get the path to the sd card via: Environment.getExternalStorage
- //get current window information, and set format, set it up differently, if you need some special effects
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- //the VideoView will hold the video
- VideoView videoHolder = new VideoView(this);
- //MediaController is the ui control howering above the video (just like in the default youtube player).
- videoHolder.setMediaController(new MediaController(this));
- //assing a video file to the video holder
- videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
- //get focus, before playing the video.
- videoHolder.requestFocus();
- if(autoplay){
- videoHolder.start();
- }
- }
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- Android bases applications development (only indian & very skilled developers apply) by sudhirjeet01
- Health Care App - want freelancer in Georgia by rshearer
- IOS+Android+HTML5+PHP Expert Needed Urgently by johnusa1
- NFC Keyboard Wedge by billinginfoes
- I need an iphone and android app + web services by senjy
- Mobile Remote control software for android only by leoagent786
- Add a "Print" button, with code, to our existing process (PDF explains) by sportingchance
- I need a phone app - Android and Iphone - a copy of an existing app by DooniseP
- Apple & Android app - Connecting our CRM and an external GPS device by kshyamnatraj
- Flash&Actionscript app for android by mobistar2013



