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
Navas
December 31, 2007 at 1:39 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
Liviu
December 31, 2007 at 9:07 am
[...] has also made the source available, which can be found here. SHARETHIS.addEntry({ title: “A New Media Player for Android”, url: [...]
A New Media Player for Android | DroidSpot
December 31, 2007 at 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.
Balaji
January 24, 2008 at 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.
Balaji
January 24, 2008 at 6:07 am
Hi,
Is there any way to start video playback from a service?
Rohit
January 28, 2008 at 1:38 am
Thanks for a great demo!
Simple, elegant and well-written.
Amos
January 31, 2008 at 4:48 pm
Hi,
Could you please update this to work with M5.
Thanks,
Regards
gary silva
February 17, 2008 at 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.
Filip Bulovic
February 24, 2008 at 1:45 pm
Hi,
Can you update this player to M5 please!
Tanks.
Andrew
Andrew
March 5, 2008 at 2:50 pm
Thanks for a great demo!
Deri
March 23, 2008 at 1:06 pm
Hi,
Could you update this player to M5 please!
thanks
peng
April 24, 2008 at 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.
peng
April 28, 2008 at 2:35 am
peng i am also gtting th same problem … as in Dalvik debug monitor showing th duration as -1
Dinesh
May 12, 2008 at 7:48 am
hv u found the solution for tat .. if so help me out ..
Dinesh
May 12, 2008 at 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.
radhika
May 13, 2008 at 7:29 am
may i have a look at your main file? thanks!
abc
May 20, 2008 at 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……..
bins
May 22, 2008 at 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
alex
May 28, 2008 at 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?
alex
May 28, 2008 at 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
souvik
June 16, 2008 at 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
souvik
June 16, 2008 at 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
celia
June 20, 2008 at 4:02 am
Hi, Change the id to android:id in main.xml.
You will see the layout by the above modification.
Prahlad T
July 7, 2008 at 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 .,…?
nekin
July 9, 2008 at 5:43 am
Does anybody here know a valid and updated code that works fine?
ricardo souza
July 15, 2008 at 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
hendrik
July 24, 2008 at 9:15 pm
Hi!,
name
August 31, 2008 at 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.
Yasmin
October 20, 2008 at 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 !
Tim
October 24, 2008 at 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
Ami
November 5, 2008 at 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
babavali
November 21, 2008 at 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
Alpesh
December 8, 2008 at 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
ankit somani
December 13, 2008 at 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!
cindy
December 18, 2008 at 7:45 am
Please share the Video Player Code .. Since it is not downloadable from this site
Nikhil
January 25, 2009 at 3:52 am
my email id is nikhilbhandary@gmail.com
Please share the Video Player Code .. Since it is not downloadable from this site
Nikhil
January 25, 2009 at 3:56 am
Can anybody please share the source code? The download link is not working. email: high2freq@yahoo.com
Thanks.
Andrea
January 26, 2009 at 3:40 am
Yes, I would appreciate the code also. My email is aphilsmith@gmail.com
Thanks!
Phil
January 26, 2009 at 3:17 pm
can you please email me the ZIP file as its no longer downloadable from this site?
i desperately need it.
anushka
January 27, 2009 at 1:04 pm
please email me the ZIP file as its no longer downloadable from this site?
i desperately need it.
anushka
January 27, 2009 at 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
Varsha
February 2, 2009 at 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.
vishy
February 3, 2009 at 1:39 am
Dear, Can some one share the code to update the Progress bar of the Media playback?
vishy
February 5, 2009 at 10:23 am
Please share source code with me, as this has been removed from server. my email id is jayesh.thadani@gmail.com
jayesh
February 5, 2009 at 11:52 pm
The link to the sourcecode does not exist. Can you share the latest zip file at pbdnit@yahoo.com pls ?
Gate
February 13, 2009 at 2:51 pm
Can anybody please share the source code? The download link is not working. email: mrcararia@gmail.com
Thanks.
Mr. Cararia
February 16, 2009 at 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
Javed
February 20, 2009 at 12:14 am
Can anybody please share the source code? The download link is not working. email: smartcelldebono@yahoo.com
Thanks.
Smartcell
February 26, 2009 at 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
muthu
March 3, 2009 at 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)
tudor
March 8, 2009 at 11:41 am
Please share source code with me, as this has been removed from server. my email id is linesaver@gmail.com
linesaver
March 9, 2009 at 12:55 am
some to me… where is the source
please info to: ilovesinai@gmail.com
chris
March 9, 2009 at 4:32 am
pls send me the source code to shaks@ureach.com
shakeel
March 10, 2009 at 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
Michael
March 25, 2009 at 9:15 am
Please share source code with me, as this has been removed from server. my email id is devadrita.harh@gmail.com
Devadrita
March 28, 2009 at 1:14 pm
cant download source from that link -.-” can u send to my email? embedzit@gmail.com
thx lotz
suesi tan
March 30, 2009 at 3:43 am
Hi, can’t download the Zip-File. Can pleas send me the code to gurkenlager@googlemail.com
Thanks!!!
Gurke
April 2, 2009 at 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!
Harrison
April 23, 2009 at 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
Amit Panwar
April 24, 2009 at 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!
emily
April 29, 2009 at 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
fidevel
April 30, 2009 at 8:23 am
nice work man can i have the source code at my email id
here it is kl_jander@yahoo.com thanks
ben
May 1, 2009 at 4:40 am
Can anybody please share the source code? I desperately need it. thanks.
recl24@hotmail.com
rudi
May 4, 2009 at 1:50 am
Can anybody please share the source code? I desperately need it. thanks.
hinty_ollo@hotmail.com
sabrina
May 17, 2009 at 10:01 pm
I can’t download source code. Please Share the source code..
lakdong@hotmail.com
ldsung
May 18, 2009 at 1:34 am
Can you please share the source code ? Thanks a lot !
Jesslyn
May 19, 2009 at 5:23 pm
I can’t download source code. Please Share the source code..
bhwon@pyroworks.co.kr
bhwon
May 20, 2009 at 12:47 am
I can’t download source code. Please Share the source code. Thanks a lot.
color47@gmail.com
Color
May 21, 2009 at 4:20 am
Can somebody send source zip file to me?
I am stuck with running live video streaming part
TIA
Aahna
May 28, 2009 at 3:26 am
Can you please share the source code ? Thanks a lot !
poveteen
June 1, 2009 at 10:28 pm
Can you please share the source code ? Thanks a lot !
poveteen@gmail.com
poveteen
June 1, 2009 at 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
Aahna
June 3, 2009 at 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 [...]
Thempra.NET : Enlaces para iniciarse en la programacion sobre Android
June 6, 2009 at 4:19 am
Can anybody please share the source code? I desperately need it. thanks.
shiihs5286@gmail.com
Skono
June 9, 2009 at 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!
shiihs
June 9, 2009 at 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 ..:)
tushar
June 9, 2009 at 9:35 am
Code: http://rapidshare.com/files/92645847/VideoPlayer.rarl
Source
http://www.android-spa.com/verManual.php?id=47
Adrian
June 9, 2009 at 10:00 am