How to play video and audio on Android


SDK Version: 
M3

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

picture

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.

  1.  public void audioPlayer(String path, String fileName){
  2.     //set up MediaPlayer    
  3.     MediaPlayer mp = new MediaPlayer();
  4.  
  5.     try {
  6.         mp.setDataSource(path+"/"+fileName);
  7.     } catch (IllegalArgumentException e) {
  8.         // TODO Auto-generated catch block
  9.         e.printStackTrace();
  10.     } catch (IllegalStateException e) {
  11.         // TODO Auto-generated catch block
  12.         e.printStackTrace();
  13.     } catch (IOException e) {
  14.         // TODO Auto-generated catch block
  15.         e.printStackTrace();
  16.     }
  17.     try {
  18.         mp.prepare();
  19.     } catch (IllegalStateException e) {
  20.         // TODO Auto-generated catch block
  21.         e.printStackTrace();
  22.     } catch (IOException e) {
  23.         // TODO Auto-generated catch block
  24.         e.printStackTrace();
  25.     }
  26.     mp.start();
  27. }

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.getExternalStorageDirectory().

  1. public void videoPlayer(String path, String fileName, boolean autoplay){
  2.     //get current window information, and set format, set it up differently, if you need some special effects
  3.     getWindow().setFormat(PixelFormat.TRANSLUCENT);
  4.     //the VideoView will hold the video
  5.     VideoView videoHolder = new VideoView(this);
  6.     //MediaController is the ui control howering above the video (just like in the default youtube player).
  7.     videoHolder.setMediaController(new MediaController(this));
  8.     //assing a video file to the video holder
  9.     videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
  10.     //get focus, before playing the video.
  11.     videoHolder.requestFocus();
  12.     if(autoplay){
  13.         videoHolder.start();
  14.     }
  15.  
  16.  }

Comments

Good post. Very impressive. Thanks for sharing.
android tablet

Very interesting articles. Great work done
batik design

This was very informative thanks for carrying out this analysis while using the precious recommendations you got while using the site. It's not at all simplistic to just happen to be giving freely strategies, you are really doing great. saga hair

Very good article !!! Android is not simple for me....

r4

I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! saga remy

Widgets are a handy, easy and simple way to add some flare to your blog. Whether you’d like to display the number of currently online visitors to your site or simply the weather report for Los Angeles, you can do it with widgets.saga gold

Widgets are a handy, easy and simple way to add some flare to your blog. Whether you’d like to display the number of currently online visitors to your site or simply the weather report for Los Angeles, you can do it with widgets.saga gold

It's my first-time visiting here. I came across so many helpful stuff within your blog particularly the on going conversation. From the tons of comments on your posts, I suppose I'm not the only one enjoying reading through your blog. Keep up the excellent work. mariage

The post is pretty interesting. I really never thought I could have a good read by this time until I found out this site. I am grateful for the information given. Thank you for being so generous enough to have shared your knowledge with us...

clubmz e-spy

hmmmmm I am very happy to see it all
thanks for sharing it
thanks again
bobbi boss

Ok gang, science teacher here, novice java guy. I've developed an app for school via Dreamweaver CS5.5. Trying to use script given to embed video. How do I get the code given here to input into html? Tried Then pasted code.

Please don't laugh, I know this must be simple. Thanks in advance!!!

BTW, here's script:

1.public void videoPlayer(String path, String fileName, boolean autoplay){
2. //get current window information, and set format, set it up differently, if you need some special effects
3. getWindow().setFormat(PixelFormat.TRANSLUCENT);
4. //the VideoView will hold the video
5. VideoView videoHolder = new VideoView(this);
6. //MediaController is the ui control howering above the video (just like in the default youtube player).
7. videoHolder.setMediaController(new MediaController(this));
8. //assing a video file to the video holder
9. videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
10. //get focus, before playing the video.
11. videoHolder.requestFocus();
12. if(autoplay){
13. videoHolder.start();
14. }
15.
16. }

the best use of audio and video is in Media Tours. nowadays almost all companies are doing it to promote themselves.

شات صوتي

دردشة صوتية

دردشة

دردشه

شات سعودي

شات خليجي
سكر بنات

جات

شات صوتي سعودي خليجي

chat voice

ahj

خليجي الصوتي

سعودي الصوتي

دردشة صوتي

شات صوتي
دردشة صوتية
شات كتابي

شات كتابي خليجي

شات عسل الصوتي

دردشة كتابية

chat
سعودي كول
سعودي كول 6666

كول

سعودي

سعودي كول انحراف

سعودي كول بنات

سعودي كول 1994

سعودي كول 94

شات سعودي كول

سعودي انحراف

سعودي انحراف2010

سعودي انحراف الصوتي

شات سعودي انحراف

دردشة سعودي انحراف

سعودي انحراف الصوتية

شبكة سعودي انحراف

سعودي انحراف الاصلي

سعودي انحراف كول

سعودي انحراف 2010

انحراف سعودي

saudideviation

دردشة صوتية سعوديه
دردشة صوتية سعودية

دردشة كتابية
دردشة كتابية خليجية
شات
دردشة
خاص للبنات

عرب ذوق

عرب ذوق الصوتي

عرب ذوق الصوتية

دردشة عرب ذوق

شات عرب ذوق

شبكة عرب ذوق

شات صوتي بنات

شات بنات الصوتي

دردشة بنات الصوتي

Girls Chat

شبكة عفناك

صوتية عفناك

شات عفناك

دردشة عفناك

عفناك الصوتي

دردشة عفناك

الخيال
الخيال كام
شبكة الخيال
الخيال الصوتي
الخيال الصوتية
دردشة الخيال
الخيال الصوتية
دردشة صوتية الخيال

شات سعودي خليجي

منتدى نونو

منتدى

منتديات

موقع

شبكة

نونو

Chat Nono

ahj w,jd

]v]am w,jdm

دليل مواقع ويب

دليل مواقع

دليل

مواقع

بنت كول
بنت كول الصوتي
شات بنت كول

دردشة بنت كول
شات بنت كول الصوتية

بنت كول الصوتيه
سعودي كول
صوتية سعودي كول
شات سعودي كول
دردشة سعودي كول
سعودي كول الصوتي
سعودي كول 6666
سعودي كول6666
سكر بنات
شات صوتي زين
شات صوتي ملوك
شات صوتي سعودي
شاتات صوتيه
مكتبة ماسنجر
شات صوتي حبي
شات صوتي كويت
YouTube - Broadcast Yourself.‏ , اليوتيوب نونو
صيف كام
شات صوتي كول
شات انحراف
وه بس
خريطة الموقع نونو
الرياض كول الصوتي
كامات 6666
شات المها
كامات6666
شات كامات 6666
كامات 666
كامات 66
سعودي انحراف
شاتكامات6666
سعودي احوه
شات سعودي احوه
سعودي احوه الصوتي
سعودي احوه كول
دردشة سعودي احوه
احوه سعودي
بنات احوه
دبي الصوتي
سعودي في اي بي الصوتي
شبكة الرياض الصوتي
روعة الليل
لايف كام
الخليج كام
شات كان زمان الصوتي
شات صوتي قصيمي
شات قلبي
ارجوان
شات صوتي قطري
بدور الخليج

منتدى روح

شبكة روح

روح ديزاين

تحميل ماسنجر بلس

توبيكات حزينه

توبيكات

ماسنجر

ماسنجر بلس

تحميل ماسنجر

توبيكات رومنسيه

منتديات روح
دردشة
شات سعودي
خليجي
شات صوتي
سعودي انحراف 2010 , صوتية سعودي انحراف , شبكة سعودي انحراف , موقع سعودي انحراف , سعودي انحراف لايف , مواقع انحرافية , دردشة شات سعودي انحراف الصوتي , سعودي لايف 2010 , سعودي انحراف 2011 , بنات لايف , منحرفات لايف جاتسعودي انحراف 6666- سعودي انحراف 2010 - سعودي انحراف2010 - سعودي انحراف 2007 - سعودي فور انحراف - سعودي انحراف - Saudi an7raf سعودي انحراف , deviation Saudi 2010‏ دردشة, شات, سعودي, انحراف 2010, كامات, ,شات انحرافي saudi, an7raf, deviation.‏ سعودي انحراف , انحراف , An7raf 6666‏سعودي انحراف , شات سعودي انحراف , شات انحرافي , انحراف 2010سعودي انحراف , سعودي انحراف 2010 , chat saudi an7raf , saudi ...‏

This is a great topic, and I find it particularly interesting. Please keep up the fantastic work, and I urge everyone here to participate more often! I'm certainly telling my friends about this. Mercedes-Benz Mobil Mewah Terbaik Indonesia | Mercedes-Benz Mobil Mewah Terbaik Indonesia | Meriahkan pesta ulang tahun bersama GarudaFood | Mari Berkomunitas Di Faceblog | Harga Jual Blackberry iPhone Laptop Murah | Video Music | Mp3 | Beauty and Health

The post is written in very a good manner and it entails many useful information for me. I appreciated what you have done here. I am always searching for informative information like this. Thanks for sharing with us.
upright bagless vacuum cleaners

You may have not intended to do so, but I think you have managed to
express the state of mind that a lot of people are in. The sense of
wanting to help, but not knowing how or where, is something a lot of us
are going through.
Clothing || Blog || Hey Brew

This is a nice web site. Good fresh interface and nice informative articles. I will be coming back soon, thanks for the great article. send mothers day flowers Hyderabad
mothers day flowers Chongqing

Kakaiba talaga ang site na ito sa iba. Alam mong marami kang matututunan dito. Marami akng bagay na gusto pang malaman sana ay marami pa akong matuklasan sito. tenant background check at iba pa.

Media Player mill valley peelsthe easiest way, if you just want to play an audio file in the background, somewhere in a appliaction. No control of the user interface here, but of course you can use MediaPlayer.stop (), play (), seekTo (), etc.online sexy dance lessons

This is a very useful program for me. Seems like my music player is always on. This site will be bookmarked.

http://www.consumer-health-reports.com

Thank you for sharing such nice information

Nice information. Get much more about world festivals.

I have tried this and currently using with my android phone. It is very interesting and fun to use! business plan writing

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my friend check up here often. Bali Property Bali Villas

Nicely presented information in this post, I prefer to read this kind of stuff. The quality of content is fine and the conclusion is good. Thanks for the post. mobile phones

you need convert your flash to android compatible video format so as to play it on android.

GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları

Hey thanks for the tips! I was on the middle of researching for my term papers when I stumbled on you post, back then I was really having a hard time until I found your post, Since I'm always on the go and I some of my music are media files.

Convenience method to create a MediaPlayer for a given Uri. On success, prepare() will already have been called and must not be called again.

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.”

http://www.tryhydroxatone.tv/

Thank you for providing nice information. La tomatina is a food fight festival observed in the town of Bunol in Valencia, Spain. It is held every year on the last Wednesday of August.

Quelle chance que j'ai trouvé ce que je cherchais depuis longtemps. Et je vais à ce site - il est ici! Beauty salon Bien fait!

Oh wow this information are wonderful. I am glad I have found this, made my research completely easy. Thank you so much!
English Editing / English Editing Services / Online Proofreading

Gifts for Her : I have been reading your posts regularly. I need to say that you are doing a fantastic job. Please keep up the great work.
I really appreciate the kind of topics you post here. Thanks for sharing information that is actually helpful. Good day!

Hmn, nice and detailed infos.thesis editing

Another app for media player.thanks for this information.
dog training courses
dog shop

this is good deal of information. relationship problems

I've been paying attention to Android since its release, but have been on the fence about using it. Your site is really good in providing info about Android. I'm still learning about it and am still not sure it's for me, but you have me as a reader because until I know for sure, I want to learn if Android is the way to go.

I would be really thankful if you keep with level what you are serving reactionary now with your diary...I really enjoyed it...and i rattling understand to you for this....its e'er pleasance to record so....Thanks for intercourse!!!
aortic valve surgery

Wonderful, This is my first time I visit here, I found so many interesting stuffs in your blog especially its discussion, It was a beneficial workout for me to go through your web page, It definitely stretches the limits with the mind when you go through very good info and make an effort to interpret it properly, Thanks. Nokia Mobiles

When my cancer was diagnosed I was in last stage with the cancer spread from one testicle to abdomen and throughout both lungs Watch Winder. After testicle removal and 4 rounds of the Einhorn Series chemo, all signs of cancer were gone. Urologists/oncologists wanted to remove all the lymph nodes "to err on the side of caution." But from what I was reading in medical library, there seemed no better survival rate for those who had the lymph node removal. I got a second opinion from an oncologist who contacted the University of Indiana on my Dual Watch Winder behalf. I was advised the lymph node disection would not offer me any real benefit, while almost certainly preventing me from ever ejaculating again. It would seem there are quality of life issues with this surgery, as well as cost/benefit issues that some doctors will be happy to make for you if one does not take the responsibility to make them for themselves, and to get second Single Watch Winderand third opinion if necessary. It was 1983 and I elected to forego the lymph node disection. It was a good decision for me.

Well, I am so excited that I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject. the diet solution program

Den Wohngebäudeversicherung Test machen.
Wie ein Vergleich funktioniert.
Den Hausrat versichern.

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!Bike For Sale 

Well, I am so excited that I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject, Thanks a lot. Free Cell Phone Spy Software

Useful sources, and then on my animal was a glitch. Nothing has corrected a little text and all fine. True to how to choose a digital camera had to tinker, but the world is not so simple.

Hi,I'm a lawsuit mesothelioma lawyer.My job keep me very busy.But I spend some time reading the article. Last time I bought a mdr513h Hdd Dvr after I read the review article.It was helping me save the money.
Now,i'm looking for the article about How to be a first time mom ? and payday online loans.If who know where is it please tell me.Thank you.