Here is a screen shot.

Notes
- You can specify a directory path or a remote URL in the edit box
- You can specify just an mp3 file to play music only
- Choose any 3gp file from http://daily3gp.com/ 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
- All the leg work was done by dahui. Many thanks!!
- The icons are from here
- MediaPlayer does not yet support any remote url’s or MPEG4 video AFAICT
Here’s the code
package org.apache.android;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class VideoPlayer extends Activity implements OnErrorListener,
OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private static final String TAG = "VideoPlayer";
private MediaPlayer mp;
private SurfaceView mPreview;
private EditText mPath;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Set up the play/pause/reset/stop buttons
mPreview = (SurfaceView) findViewById(R.id.surface);
mPath = (EditText) findViewById(R.id.path);
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.stop();
mp.release();
}
}
});
// Set the transparency
getWindow().setFormat(PixelFormat.TRANSPARENT);
// Set a size for the video screen
holder = mPreview.getHolder();
holder.setCallback(this);
holder.setFixedSize(400, 300);
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
// If the path has not changed, just start the media player
if (path.equals(current) && mp != null) {
mp.start();
return;
}
current = path;
// Create a new media player and set the listeners
mp = new MediaPlayer();
mp.setOnErrorListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(2);
// Set the surface for the video output
mp.setDisplay(mPreview.getHolder().getSurface());
// Set the data source in another thread
// which actually downloads the mp3 or videos
// to a temporary location
Runnable r = new Runnable() {
public void run() {
try {
setDataSource(path);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
}
};
new Thread(r).start();
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mp != null) {
mp.stop();
mp.release();
}
}
}
/**
* If the user has specified a local url, then we download the
* url stream to a temporary location and then call the setDataSource
* for that local file
*
* @param path
* @throws IOException
*/
private void setDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
mp.setDataSource(path);
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
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 = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
mp.setDataSource(tempPath);
try {
stream.close();
}
catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
}
}
public void onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:" + what + " extra:" + extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent);
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
}
public boolean surfaceCreated(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceCreated called");
return true;
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
Log.d(TAG, "surfaceChanged called");
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceDestroyed called");
}
}
Download Source and APK from here – VideoPlayer.zip
Hi,
Thanks for your code, It works like a charm but i need to know one thing where the temp file is created and is it not necessary to delete the temp file every time when the media is completed
Comment by Navas — December 31, 2007 @ 1:39 am
Hey, How did you all guys worked this code? I’m not able run any video using this code. It runs on my device. when i play with the sdcard path ( file:///sdcard/a.mp4), it doesn’t run my video there. And i kept that as with default path also didn’t play any video. But i want to run a video which is from Raw folder available in project resource using this code…Can it be done? If yes, how? Please advice asap.
Comment by John — October 16, 2009 @ 7:12 am
Hi,
I tried to run the code but I cannot import the project in Eclipse and when I create a new project and I try to copy the unzipped files over the created ones, some errors occur.
Can you provide more informations on how to run this project?
Thank you
Comment by Liviu — December 31, 2007 @ 9:07 am
[...] has also made the source available, which can be found here. SHARETHIS.addEntry({ title: “A New Media Player for Android”, url: [...]
Pingback by A New Media Player for Android | DroidSpot — December 31, 2007 @ 11:02 pm
Hi,
I download the application and unable to run the same in eclipse. It showing some exception in Androidmanifest.xml file.
It showing “package ‘ org.apache.android’ does not exist! “. what i suppose to do for this.
Thanks,
Bala.
Comment by Balaji — January 24, 2008 @ 5:03 am
Hi,
I have created one more file in that i added all the files present in the VideoPlayer.zip.
Finally i got something, but with simple text box alone, nothin other than that is comming .
Kindly help me in this regrds.
Bala.
Comment by Balaji — January 24, 2008 @ 6:07 am
Hi,
Is there any way to start video playback from a service?
Comment by Rohit — January 28, 2008 @ 1:38 am
Thanks for a great demo!
Simple, elegant and well-written.
Comment by Amos — January 31, 2008 @ 4:48 pm
Hi,
Could you please update this to work with M5.
Thanks,
Regards
Comment by gary silva — February 17, 2008 @ 9:44 pm
Very nice example. For those who have problem to run it:
How it is written one will need to use HVGA-L mode of emulator or to edit the following line inside onCreate:
holder.setFixedSize(400, 300);
to something less ambitious like 200×200.
Comment by Filip Bulovic — February 24, 2008 @ 1:45 pm
Hi,
Can you update this player to M5 please!
Tanks.
Andrew
Comment by Andrew — March 5, 2008 @ 2:50 pm
Thanks for a great demo!
Comment by Deri — March 23, 2008 @ 1:06 pm
Hi,
Could you update this player to M5 please!
thanks
Comment by peng — April 24, 2008 @ 10:13 pm
Hi .
I can’t play the video from the local . the error as follows:
Duration: ===> -1
Could you help me check it .Thansk
Thanks.
Comment by peng — April 28, 2008 @ 2:35 am
peng i am also gtting th same problem … as in Dalvik debug monitor showing th duration as -1
Comment by Dinesh — May 12, 2008 @ 7:48 am
hv u found the solution for tat .. if so help me out ..
Comment by Dinesh — May 12, 2008 @ 7:49 am
Hi,
This code is showing some errors.
Actually the setCallback method is not avalable in m5. And the surfaceCreated method’s return type has been changed.
i updated these errors. but am not able toplay any file. Please help me.
Comment by radhika — May 13, 2008 @ 7:29 am
may i have a look at your main file? thanks!
Comment by abc — May 20, 2008 @ 10:46 pm
hi……
i have the proble with the video view i will post my code down …….its not working…
[syntax="java"]
Java:
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class musicplayer extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
VideoView vv=(VideoView)findViewById(R.id.vdoplayer);
// MediaPlayer mp=new MediaPlayer();
// Uri myUrl = Uri.parse(“http://207.210.108.32/Music/Crazy.3gp”);
// vv.setVideoURI(myUrl);
vv.setVideoPath(“http://207.210.108.32/Music/Alarm.wmv”);
vv.start();
}
}
this is the java file andd xml given below
XML:
]syntax=”xml”]
but is not working…..i was trying with the my own server….it is getting in browser….but not working with the video view…….can any body help me…….
or can any body tell me how to access and play video on the emulator …..
please…
with thanks bins……..
Comment by bins — May 22, 2008 @ 6:43 am
Hi,
thanks a lot for that example!
1. I can play videos from the internet when i remove the extra thread and start & prepare the player without it. Do you really need an extra thread? The GUI is reacting nevertheless, so whats the advantage?
2. How can i play local files? I put them into the resource folder (res/raw/), but no path which i enter in the textfield seem to be correct.
thanks for help
Comment by alex — May 28, 2008 @ 7:58 am
Ok,
i figured out a way to play local videos. If you play them from the Internet, they get stored in /tmp/… I just copied a video over eclipse FileExplorer into that directory and played the url /tmp/filename. But i don´t know how to play files within the resources directory. Most likely they are stored in the app.apk. Why can´t i play them with /res/raw/filenname?
Comment by alex — May 28, 2008 @ 10:19 am
Hi,
This is really great and thanks for sharing this code. but
can anybody tell me where is the zip file for this source code?
else let me know how to run this code in android emulator?
Thanks,
Souvik
Comment by souvik — June 16, 2008 @ 2:37 am
Problem: Not able to load the project into Eclipse.
when I want to add this project as new project from existing source and given the path for this folder , its showing an error msg:” No activity name defined in D:\android\android_workspace\VideoPlayer\AndroidManifest.xml.”
and not able to load this project.
Please help me to load this project to run it in emulator.
Thanks,
Souvik
Comment by souvik — June 16, 2008 @ 4:03 am
Hi,
I could resolve all the errors. I’m finding problem at the run time. I’m getting NullPointerException error. Found that the error is due to
mPreview = (SurfaceView) findViewById(R.id.surface)
mPreview is null.
Likewise, mPath, mPlay, … are also null.
Can anyone suggest how to overcome this issue???????
Celia
Comment by celia — June 20, 2008 @ 4:02 am
Hi, Change the id to android:id in main.xml.
You will see the layout by the above modification.
Comment by Prahlad T — July 7, 2008 @ 1:28 am
hi there..
from above pasted code.. when i did some changes at line number 122.. i just cut that line and put out of thread then only it is working fine for total video lenght.
otherwise if i keep it as it is in thread then only very small fraction of video from initial part is playing then after it is terminating application…
can you help me in that .,…?
Comment by nekin — July 9, 2008 @ 5:43 am
Does anybody here know a valid and updated code that works fine?
Comment by ricardo souza — July 15, 2008 @ 7:21 am
I have some problem on
final String path = mPath.getText().toString();
when I run the program..
java.nullpointer exception occur..
can someone help me?
thx in advanced
Comment by hendrik — July 24, 2008 @ 9:15 pm
Hi!,
Comment by name — August 31, 2008 @ 10:40 pm
Hi,
I can’t see the source code. I got web page not found exception if I’m trying to download the source code.
Can you plz… anybody share the source code ?
Thanks in advance.
Comment by Yasmin — October 20, 2008 @ 10:53 pm
The source and pictures are no longer available. If you still have the source, could you please email it to me ? Thank you !
Comment by Tim — October 24, 2008 @ 11:25 am
Hallo ,
great Work ,but i can’t play it with the 0.9 Beta ,Could you please help me and mail me this .
thanks
Comment by Ami — November 5, 2008 @ 9:47 am
hi,
i cant able to download the code canu pls share the code
i have copied and run it i am getting error
MediaPLayer :Error(-38,0)
Error(-1,0)
will u pls help me ou of this
thanxin advance
Comment by babavali — November 21, 2008 @ 1:28 am
hi,
where is the source file? I got web page not found exception 404 whie downloading the source code.
can anybody share the code pls….
my id is alpesh.harsoda@gmail.com
thnks in advance.
Regards,
Alpesh Harsoda
Comment by Alpesh — December 8, 2008 @ 4:34 am
Hi,
I tried this code by make some necessary changes. but not working can you please help me. Its giving the NullPointerException at surface view,giving error(-38,0). Some where it showing Status=#fffffff
Comment by ankit somani — December 13, 2008 @ 3:42 pm
Can anybody share the source code?
I couldn’t download the code.
Please…
my email is cindyduh@nthu.us
thanks a lot!
Comment by cindy — December 18, 2008 @ 7:45 am
Please share the Video Player Code .. Since it is not downloadable from this site
Comment by Nikhil — January 25, 2009 @ 3:52 am
my email id is nikhilbhandary@gmail.com
Please share the Video Player Code .. Since it is not downloadable from this site
Comment by Nikhil — January 25, 2009 @ 3:56 am
Can anybody please share the source code? The download link is not working. email: high2freq@yahoo.com
Thanks.
Comment by Andrea — January 26, 2009 @ 3:40 am
Yes, I would appreciate the code also. My email is aphilsmith@gmail.com
Thanks!
Comment by Phil — January 26, 2009 @ 3:17 pm
can you please email me the ZIP file as its no longer downloadable from this site?
i desperately need it.
Comment by anushka — January 27, 2009 @ 1:04 pm
please email me the ZIP file as its no longer downloadable from this site?
i desperately need it.
Comment by anushka — January 27, 2009 @ 1:05 pm
can anybody pleaseeeeeee email this file to me… as i’m not able to download the zip file from this site…PLeaseeeeee
email : varsha.uni@gmail.com
Comment by Varsha — February 2, 2009 @ 6:11 pm
Many thanks to this code.
The lik to zip file is not available. Is this been removed from the server?
I am trying use the code mentioned above. I could not compile it. It saying R.ID is not recognized. Can someone give me strps to compile this code. What are other files needed or what declaration are needed to compile this successfully?
Thank you for your time.
Comment by vishy — February 3, 2009 @ 1:39 am
Dear, Can some one share the code to update the Progress bar of the Media playback?
Comment by vishy — February 5, 2009 @ 10:23 am
Please share source code with me, as this has been removed from server. my email id is jayesh.thadani@gmail.com
Comment by jayesh — February 5, 2009 @ 11:52 pm
The link to the sourcecode does not exist. Can you share the latest zip file at pbdnit@yahoo.com pls ?
Comment by Gate — February 13, 2009 @ 2:51 pm
Can anybody please share the source code? The download link is not working. email: mrcararia@gmail.com
Thanks.
Comment by Mr. Cararia — February 16, 2009 @ 6:00 am
Hey guyz.. plz i need this code .. so could u ppl send this code on this email add: javeeduddin.b@hotmail.com
Comment by Javed — February 20, 2009 @ 12:14 am
Can anybody please share the source code? The download link is not working. email: smartcelldebono@yahoo.com
Thanks.
Comment by Smartcell — February 26, 2009 @ 12:21 pm
The download able zip code is not available on this link. can you pls send it to me in mail? muthusankarm@hotmail.com
Comment by muthu — March 3, 2009 @ 6:44 am
Hey, could you please send me(or update the download link) the source code of this app? I need it for a future-android-streaming app i’m developing. Thanks in advance! (Email: tdr.popa@gmail.com)
Comment by tudor — March 8, 2009 @ 11:41 am
Please share source code with me, as this has been removed from server. my email id is linesaver@gmail.com
Comment by linesaver — March 9, 2009 @ 12:55 am
some to me… where is the source
please info to: ilovesinai@gmail.com
Comment by chris — March 9, 2009 @ 4:32 am
pls send me the source code to shaks@ureach.com
Comment by shakeel — March 10, 2009 @ 10:09 pm
Hi their,
I would like to have the Video/Musik player, so please can anyone send me the source code. e-Mail: m.kruemmling@googlemail.com
Comment by Michael — March 25, 2009 @ 9:15 am
Please share source code with me, as this has been removed from server. my email id is devadrita.harh@gmail.com
Comment by Devadrita — March 28, 2009 @ 1:14 pm
cant download source from that link -.-” can u send to my email? embedzit@gmail.com
thx lotz
Comment by suesi tan — March 30, 2009 @ 3:43 am
Hi, can’t download the Zip-File. Can pleas send me the code to gurkenlager@googlemail.com
Thanks!!!
Comment by Gurke — April 2, 2009 @ 8:37 am
I can’t download the zip file from the link. Please send me the file to hsyun@sktelecom.com Thank you in avanced!
Comment by Harrison — April 23, 2009 @ 2:13 am
Please share the code to me at my email id: apanwar@live.com as it is not downloadable from the link.
Thanks,
Amit
Comment by Amit Panwar — April 24, 2009 @ 9:53 am
I can’t download the zip file from the link. Please send me the file to emily.ting@ecs.com.tw Thank you in avanced!
Comment by emily — April 29, 2009 @ 7:41 am
Hi,i’m starting learning android now and this is really helpful to me,thanks for making it public.
Can i have the source code at fidevel@yahoo.com,since its not downloadable from the link?Thanks in advance!
3m
Comment by fidevel — April 30, 2009 @ 8:23 am
nice work man can i have the source code at my email id
here it is kl_jander@yahoo.com thanks
Comment by ben — May 1, 2009 @ 4:40 am
Can anybody please share the source code? I desperately need it. thanks.
recl24@hotmail.com
Comment by rudi — May 4, 2009 @ 1:50 am
Can anybody please share the source code? I desperately need it. thanks.
hinty_ollo@hotmail.com
Comment by sabrina — May 17, 2009 @ 10:01 pm
I can’t download source code. Please Share the source code..
lakdong@hotmail.com
Comment by ldsung — May 18, 2009 @ 1:34 am
Can you please share the source code ? Thanks a lot !
Comment by Jesslyn — May 19, 2009 @ 5:23 pm
I can’t download source code. Please Share the source code..
bhwon@pyroworks.co.kr
Comment by bhwon — May 20, 2009 @ 12:47 am
I can’t download source code. Please Share the source code. Thanks a lot.
color47@gmail.com
Comment by Color — May 21, 2009 @ 4:20 am
Can somebody send source zip file to me?
I am stuck with running live video streaming part
TIA
Comment by Aahna — May 28, 2009 @ 3:26 am
Can you please share the source code ? Thanks a lot !
Comment by poveteen — June 1, 2009 @ 10:28 pm
Can you please share the source code ? Thanks a lot !
poveteen@gmail.com
Comment by poveteen — June 1, 2009 @ 10:29 pm
other solution pages from davanum have the zip file downloadable. Any particular reason why this link is disabled?
It would be great help if people who got it earlier upload the same zip somewhere and share the link for helping others.
Regards,
Aahna
Comment by Aahna — June 3, 2009 @ 1:12 am
[...] serie de manuales (http://www.android-spa.com/manuales.php)Reproductor multimedia (http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-...) Posted: sábado, 06 de junio de 2009 10:52 por Thempra Archivado en: Software [...]
Pingback by Thempra.NET : Enlaces para iniciarse en la programacion sobre Android — June 6, 2009 @ 4:19 am
Can anybody please share the source code? I desperately need it. thanks.
shiihs5286@gmail.com
Comment by Skono — June 9, 2009 @ 1:10 am
I can’t download the zip file from the link. Please send me the file to shiihs5286@gmail.com Thank you in avanced!
Comment by shiihs — June 9, 2009 @ 1:42 am
hey hi buddy …
cn i get source code of same.. it will be gr8 help fr me if i get code .. thnx in advance ..:)
Comment by tushar — June 9, 2009 @ 9:35 am
Code: http://rapidshare.com/files/92645847/VideoPlayer.rarl
Source
http://www.android-spa.com/verManual.php?id=47
Comment by Adrian — June 9, 2009 @ 10:00 am
how to get those 4 image button displayed?
Comment by alan — July 2, 2009 @ 3:17 pm
very well presented. thank you.
can you please help ? I want to install the android apk in a real device. how? procedure?
Comment by mathew — July 28, 2009 @ 6:18 am
Has anybody extended on this design? I listen to folder based audio courses and most media players are not very good at helping me managing them.
I really like that this app starts with a folder view and it would be great if you could just say “play this folder” so that when one file finished it would load and play the next file. Even better if it remembered which file you were on and the location within that file per directory.
And of course work with the bluetooth control buttons on my stereo headset and behave nicely when switching from audio to phone and back again.
Any ideas where I can find an app like that?
Comment by Dave — August 9, 2009 @ 8:21 pm
hallo,
I’m programming with Eclipse IDE and using the Android Emulator.I have make a copy and paste of this code, but I’m getting some errors with the class R.id.Thats is the class R.Java that was generated by creation of the application. I don’t know what is wrong on it. If it is correct what could be the problem. Could somebody help me ?
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0×7f020000;
}
public static final class layout {
public static final int main=0×7f030000;
}
public static final class string {
public static final int app_name=0×7f040001;
public static final int hello=0×7f040000;
}
}
Comment by stephane — August 13, 2009 @ 7:44 am
hi, the download link is failed, could u send me the source file? i think it can help me to develop code about media player. thanks. my email: maestro1230@gmail.com
Comment by Brian — September 14, 2009 @ 7:24 am
Hey! can someone share the src with me? markmathis@gmail.com
Comment by mark mathis — September 22, 2009 @ 5:54 am
Hi,
i cannot find the source files! can anyone please mail it to me(bhavyaprakash@gmail.com)
Thanks a ton!
Comment by Bhavya — September 23, 2009 @ 5:24 am
Hi
can any one share the APK file and the source code too
pl. send to sayiram.kothuri@gmail.com
Comment by sram — October 10, 2009 @ 3:02 am
Hey! can someone share the src with me? kimjs0928@hanmail.net
Comment by kim — October 26, 2009 @ 11:40 am
will you please send me the source and apk to my email address as it giving some problem to download.
Comment by gour — November 10, 2009 @ 12:02 pm