Show me the code! – By Davanum Srinivas

December 4, 2009

Android – Video/Music player sample (Take #2)

Filed under: Uncategorized — Davanum Srinivas @ 12:55 am

NOTE: Since many folks were looking for it…Here’s the updated version of code originally posted here

Here is a screen shot.



  • You can specify a directory path or a remote URL in the edit box

  • Choose any 3gp file from to view videos

  • The 4 buttons are for play, pause, reset and stop

  • When you specify a http or https url, we save the url contents to hard disk first and then play it

  • The icons are from here

Here’s the code

 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoViewDemo extends Activity {
	private static final String TAG = "VideoViewDemo";

	private VideoView mVideoView;
	private EditText mPath;
	private ImageButton mPlay;
	private ImageButton mPause;
	private ImageButton mReset;
	private ImageButton mStop;
	private String current;

	public void onCreate(Bundle icicle) {
		mVideoView = (VideoView) findViewById(;

		mPath = (EditText) findViewById(;

		mPlay = (ImageButton) findViewById(;
		mPause = (ImageButton) findViewById(;
		mReset = (ImageButton) findViewById(;
		mStop = (ImageButton) findViewById(;

		mPlay.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
		mPause.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
		mReset.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
		mStop.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
					current = null;
		runOnUiThread(new Runnable(){
			public void run() {

	private void playVideo() {
		try {
			final String path = mPath.getText().toString();
			Log.v(TAG, "path: " + path);
			if (path == null || path.length() == 0) {
				Toast.makeText(VideoViewDemo.this, "File URL/path is empty",

			} else {
				// If the path has not changed, just start the media player
				if (path.equals(current) && mVideoView != null) {
				current = path;

		} catch (Exception e) {
			Log.e(TAG, "error: " + e.getMessage(), e);
			if (mVideoView != null) {

	private String getDataSource(String path) throws IOException {
		if (!URLUtil.isNetworkUrl(path)) {
			return path;
		} else {
			URL url = new URL(path);
			URLConnection cn = url.openConnection();
			InputStream stream = cn.getInputStream();
			if (stream == null)
				throw new RuntimeException("stream is null");
			File temp = File.createTempFile("mediaplayertmp", "dat");
			String tempPath = temp.getAbsolutePath();
			FileOutputStream out = new FileOutputStream(temp);
			byte buf[] = new byte[128];
			do {
				int numread =;
				if (numread <= 0)
				out.write(buf, 0, numread);
			} while (true);
			try {
			} catch (IOException ex) {
				Log.e(TAG, "error: " + ex.getMessage(), ex);
			return tempPath;

Download Source and APK from here –


  1. Thanks for sharing this with us!

    Comment by Jim — December 6, 2009 @ 9:56 am

    • I get the same, i.e. white screen with Audio only. I am using the Android Emulator with the target set to 2.0.1. Did you ever resolve this? Is there a specific setting in the emulator that must be used for this to work?

      Comment by John — February 1, 2010 @ 2:03 pm

  2. Hi,

    Thanks for sharing the code srinivas.

    When i run this on my machine, the audio works fine, but instead of video i just see a white screen.
    I have tried running a similar version of this app provided in android sdk samples but that also produces a white screen.

    Are there any additional settings required while creating the emulator for running videos ?

    Comment by Pooja — December 6, 2009 @ 8:04 pm

  3. Hi,
    Thanks for providing code and I got output means video is played on emulator

    Comment by Sunitha — December 25, 2009 @ 6:22 am

  4. I’m new to Android and Java. How do I import your code into Eclipse?

    Comment by Lou — January 8, 2010 @ 2:03 pm

  5. Hi ,
    But this code download video and then show from cache can we do streaming of video.

    Comment by himanshu — January 8, 2010 @ 2:49 pm

  6. Figured it out. Never mind. I’m not seeing a video either. Just a white screen.

    Comment by Lou — January 8, 2010 @ 4:09 pm

    • I am trying to do the same thing could you let me know how you did this?

      Thanks for sharing!!

      Comment by Chuck — May 6, 2010 @ 1:28 am

  7. thx!… works fine for me on motorola milestone!

    Comment by thesparxinc — January 18, 2010 @ 4:36 am

  8. Ok, but the video plays until it was downloaded. How to play in parts (with buffer arrays)?… I mean… I tried to download it in parts and play part by part (for not wait)… the video part losed the format and I can’t play it… How can i re-format that part of video?

    Comment by Arturo Plauchu — March 19, 2010 @ 4:15 pm

  9. I have not seen video, only edit text with url is coming.

    is there any setting required in android emulator?

    pls tell me what to do for playing the video….

    Comment by Amrendra — March 23, 2010 @ 4:48 am

    • same with me.. did you get any resolution??

      Comment by Chitresh — April 28, 2010 @ 4:44 am

      • Hi i am also facing same problem did you get any solution?

        Comment by Nag — December 27, 2011 @ 1:17 am

  10. hi,thank you for great support.Now i want to know how to add media file on SD card so that i can listen the music through this apllication.And tell me that emulator can give output of music using laptop’s speaker……

    Comment by vivek satasia — April 15, 2010 @ 2:34 pm

  11. very informative..thanks a lot.
    james smith

    Comment by james smith — April 21, 2010 @ 7:22 am

  12. I am not able to see anything. Even the control buttons are not visible. Just a black screen and edit box with the default address is visible.

    Comment by Chitresh — April 28, 2010 @ 4:27 am

  13. I have the same problem:
    when i run this on my machine, the control buttons are not visible. Just a black screen and edit box with the default address is visible. And if I use the similar version of this app provided in android sdk samples, the audio works fine, but instead of video i just see the first picture of my video. Anybody have a solution?

    Sorry for my poor English, I’m french :/

    Thanks a lot

    Comment by Marie — April 29, 2010 @ 10:33 am

  14. Hi all…
    please help..

    My project build is giving me error while setting target API level 5. I do not know whats wrong in that.

    I got following error :


    [2010-05-06 10:43:01 – CallReceiver]
    trouble processing “java/net/DatagramPacket.class”:
    [2010-05-06 10:43:01 – CallReceiver]
    Attempt to include a core class (java.* or javax.*) in something other
    than a core library. It is likely that you have attempted to include
    in an application the core library (or a part thereof) from a desktop
    virtual machine. This will most assuredly not work. At a minimum, it
    jeopardizes the compatibility of your app with future versions of the
    platform. It is also often of questionable legality.

    If you really intend to build a core library — which is only
    appropriate as part of creating a full virtual machine distribution,
    as opposed to compiling an application — then use the
    “–core-library” option to suppress this error message.

    If you go ahead and use “–core-library” but are in fact building an
    application, then be forewarned that your application will still fail
    to build or run, at some point. Please be prepared for angry customers
    who find, for example, that your application ceases to function once
    they upgrade their operating system. You will be to blame for this

    If you are legitimately using some code that happens to be in a core
    package, then the easiest safe alternative you have is to repackage
    that code. That is, move the classes in question into your own package
    namespace. This means that they will never be in conflict with core
    system classes. If you find that you cannot do this, then that is an
    indication that the path you are on will ultimately lead to pain,
    suffering, grief, and lamentation.


    Comment by nextcome — May 6, 2010 @ 12:21 am

    • hey.. problem is solved…
      I removed advance android library….and it works now… but now problem is

      it is showing only black screen..nothing else…

      Please help me…


      Comment by nextcome — May 6, 2010 @ 1:38 am

  15. Hi Shrinivas.. thx for the code… i am getting error when import your code into eclipse. Please refer above.



    Comment by nextcome — May 6, 2010 @ 12:24 am

  16. The people who are having trouble playing the video but can hear the sound just have a layout issue, it can be fixed with the following code in your layout file:

    You can move the VideoView tag above the buttons, but if your video is particularly big your buttons won’t show up on the screen.



    Comment by Dom — May 12, 2010 @ 2:03 pm

    • All I see for the code is a bunch of blank comment lines:(

      Comment by Matt J. — July 5, 2010 @ 8:12 pm

  17. The people who are having trouble playing the video but can hear the sound just have a layout issue, it can be fixed with the following code in your layout file:


    You can move the VideoView tag above the buttons, but if your video is particularly big your buttons won’t show up on the screen.



    Comment by Dom — May 12, 2010 @ 2:05 pm

    • Use the target Platform 1.5 for video play

      Comment by Lee HyunSoo — May 14, 2010 @ 5:05 am

    • I have the VideoView above the buttons but it still doesn’t show the video.

      Comment by Smitha — May 31, 2010 @ 2:02 am

      • Hi Lee,

        Did you get any replies for this. I am too in a fix like you :-(

        Comment by Senthil — June 10, 2011 @ 12:48 am

  18. hii … wen i tried to run the code its giving an error saying

    /VideoView/gen already exists but is not a source folder. Convert to a source folder or rename it.

    can anyone pls help me with it…?

    Comment by hema — June 5, 2010 @ 11:35 pm

  19. hi,

    I found that the temp file in not deleted on exit. New file is created like
    mediaplayertmp50767dat, mediaplayertmp36794dat, etc whenever i run the app.

    And help me how to save the file in sdcard download dir.

    Comment by kishore — July 7, 2010 @ 2:15 am

    • Kishore, were you able to save the file in sdcard?

      Comment by Sreeram — August 19, 2010 @ 11:52 am

  20. Hi,
    I am new in android programming. I couldn’t able to change the url. If I want to play wmv file with this code how I can do this.
    If anyone help me,it will be nice.

    Comment by Asma — August 18, 2010 @ 3:33 am

  21. Hi,
    It works for .mp4 using local website.
    Can anyone tell me how can I measure the different video clip’s running time using progress bar?
    I need an urgent help. I stuck in my assignment. Please help me.

    Comment by Asma — August 27, 2010 @ 7:53 am

  22. Hi,

    i tried the code and all I can get is the audio track. The video didn’t progress more after one second and the only thing I get is the audio being played. Is there something that needs to be done to get the video to play, not just the audio?


    Comment by prana — August 30, 2010 @ 4:12 am

  23. Hi,
    It works for .mp4 using local website too.

    Can anyone tell me how can I measure the different video clip’s running time using progress bar?

    I need an urgent help. I stuck in my assignment. Please help me.

    Comment by Asma — September 3, 2010 @ 3:32 am

  24. […] also tried this example and it works fine. But it downloads video from world wide web and I need to use […]

    Pingback by “Sorry, this video cannot be played” | — October 5, 2010 @ 12:13 am

  25. hi,
    i m new to android
    what to do, after downloading the code,
    where to paste the code?
    and there r lots of errors?
    what name should be given to the new project we make for this application??


    Comment by ashish — October 22, 2010 @ 5:34 am

  26. […] the sd card and hence video is not being played.I have used following code :- check it out the link help me out.Abhijeet […]

    Pingback by sd card folder is not being created in DDMS - Question Lounge — December 30, 2010 @ 7:49 pm

  27. The source file not avaliable. pls reupload mirror! :)

    Comment by fjani — January 1, 2011 @ 11:24 am

  28. This app. fails on the line:

    File temp = File.createTempFile(“mediaplayertmp”, “dat”);
    throws “Permission denied” exception.

    Why would this occur?
    It sounds like I need to change some kind of Java setting to designate a “remp” folder.


    Comment by achalk — January 17, 2011 @ 12:06 am

    • Add the follwing permission

      Comment by Rameez — November 3, 2011 @ 1:11 am

  29. Hi
    Can anyone give the configuration of the main xml? I’m new in android :s
    Thanks ;)

    Comment by Marc Bernardo — June 30, 2011 @ 6:46 am

  30. Hey..Hii
    I am getting same error as ‘Permission Denied’ exception
    on line —File temp = File.createTempFile(“mediaplayertmp”, “dat”);

    Can you suggest me some solution for this exception please..
    Reply as early as possible..
    Thanxs in Advance!!

    Comment by Ishwari Shah — July 2, 2011 @ 6:54 am

    • This could happen when your SDcard is not mounted. Use following code to check SDcard is mounted or not
      Toast.makeText(this, “External SD card not mounted”, Toast.LENGTH_LONG).show();

      Comment by Nidhi — September 13, 2011 @ 6:56 am

  31. Hi you!
    i am a new Android person.
    i am developing about movie and theater finder.
    now,i don’t know how to develop?
    Can you help me about this.
    And key feature:


    List movies, upcoming movies and showtimes :


    List theatres, theatres detail, view theatres on map:


    List movie trailers, play trailers, list movie news:


    Manage movies and theatres favourites:
    Best Regards!

    Comment by viethung — August 13, 2011 @ 11:07 am

  32. hi i am gtting error on networkonmainthreadException….

    Comment by emmanuel — October 18, 2011 @ 7:28 am

  33. I experienced a blank screen with an EditText on top with the path of the 3gp file like others had written about in the comments.
    For me the fix to this was simply adding the correct permissions to the androidmanifest.xml file.

    Internet permission is obviously needed.
    Temp files are stored on SD so we need permission to write to external storage.

    Comment by Leffy — October 26, 2011 @ 7:45 am

    • thanks man, was i needed

      Comment by tony — December 5, 2011 @ 9:59 am

  34. i need do that for Android 2.2, some help

    Comment by tony — December 5, 2011 @ 9:49 am

  35. ok forget the above, this solved, a question,
    how I do the same for a youtube video?

    Comment by tony — December 5, 2011 @ 10:02 am

  36. I need to see my video upto full screen size even in portrait and landscape mode….
    can any one help me…….Thanks in advance…..

    Comment by sandeep_android — December 14, 2011 @ 8:19 am

  37. how do i play you-tube and ooyala video, can you help me,

    Comment by sheetal suryan — January 18, 2012 @ 1:46 am

  38. How can this be converted to just stream audio files and shoutcast streams? And remove the prowser bar?


    Comment by Markess — February 16, 2012 @ 8:55 pm

  39. what is “id” in line 058 , 059 ???

    Comment by shivam — March 6, 2012 @ 11:34 am

  40. i run this code but this code not run please reply me

    Comment by vinit — July 30, 2012 @ 6:26 am

  41. […] I want to preload and stream mp4 video in android from server and display it in Video View.I found Many example but when i integrate it in my application deos not work.This tutorial for example… […]

    Pingback by Android: Preload mp4 video file and streaming - How-To Video — April 17, 2013 @ 5:26 pm

  42. How can I do live stream on the IP camera?Waiting for help.My blog is

    Comment by rclk — August 29, 2013 @ 3:59 am

  43. […] This example work for me (on 1.5 and 2.0 at least) by downloading the file and playing it locally. […]

    Pingback by Using VideoView for streaming or progressive-download video | Ask Programming & Technology — November 1, 2013 @ 5:34 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at


Get every new post delivered to your Inbox.

Join 136 other followers

%d bloggers like this: