Laman

Tuesday, November 30, 2010

Play 3gp video file using MediaPlayer

The former exercise show how to "play MIDI audio using MediaPlayer". MediaPlayer can play video file also. This exercise show hoe to play 3gp video file in sdcard using MediaPlayer

(Remark: In my case, the video (and sound) can not be displayed on emulator. But it can work as expected on true phone.)

Play 3gp video file using MediaPlayer

Copy 3gp file to root folder on SD Card.

Modify main.xml to have two buttons and a SurfaceView to show the video.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/playvideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY Video -"
/>
<Button
android:id="@+id/pausevideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE Video -"
/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify source code, AndroidVideoPlayer.java.
package com.exercise.AndroidVideoPlayer;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class AndroidVideoPlayer extends Activity implements SurfaceHolder.Callback{

MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;;

String stringPath = "/sdcard/samplevideo.3gp";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
Button buttonPauseVideo = (Button)findViewById(R.id.pausevideoplayer);

getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();

buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pausing = false;

if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);

try {
mediaPlayer.setDataSource(stringPath);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mediaPlayer.start();


}});

buttonPauseVideo.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(pausing){
pausing = false;
mediaPlayer.start();
}
else{
pausing = true;
mediaPlayer.pause();
}
}});

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
}


Download the files.

Related Article:
- Implement a SeekBar to control the volume of Video Player



READ MORE - Play 3gp video file using MediaPlayer

Wallpaper Juke

http://3.bp.blogspot.com/_FoXyvaPSnVk/S3MKrCfh_eI/AAAAAAACgEI/zx0or573Ulc/s1600/Nissan-Juke-1.JPG
2011 Nissan Juke Wallpaper




http://2.bp.blogspot.com/_FoXyvaPSnVk/S3MKwdM3CSI/AAAAAAACgGI/3LhSuxbppFw/s1600/Nissan-Juke-35.jpg
2011 Nissan Juke Wallpaper


http://2.bp.blogspot.com/_FoXyvaPSnVk/S3MKsDiEW8I/AAAAAAACgEo/_cY0o6MMRYQ/s1600/Nissan-Juke-5.JPG
2011 Nissan Juke Wallpaper


welcometothefuckingwonderland.xobius.com


http://4.bp.blogspot.com/_FoXyvaPSnVk/S3MKvqRMQII/AAAAAAACgF4/pxbk-c-Xrzc/s1600/Nissan-Juke-33.jpg
juke wallpaper nissan juke wallpaper nissan juke wallpaper nissan juke ...


http://3.bp.blogspot.com/_FoXyvaPSnVk/S3MLIu6HLqI/AAAAAAACgII/FZQ4MPytCaE/s1600/Nissan-Juke-27.jpg
2011 Nissan Juke Wallpaper


http://2.bp.blogspot.com/-4GJbvaWq68c/Tov6UfMssGI/AAAAAAAAAsk/o7REWsQAZNE/s1600/2012+Nissan+Juke+Kuro+edition+wallpaper.jpg
Kendall self drive: 2012 Nissan Juke Kuro Edition Review


http://4.bp.blogspot.com/-wEsjT3X7r5Y/T733S30zYQI/AAAAAAAADgw/lxDRHV0q30E/s1600/nissan+juke+5.jpg
Posted by Crunchy News at 1:55 AM


http://2.bp.blogspot.com/_FoXyvaPSnVk/S3MLITF_S0I/AAAAAAACgIA/tAvh6S-uQhY/s1600/Nissan-Juke-26.jpg
2011 Nissan Juke Wallpaper


http://2.bp.blogspot.com/_3V1YR3u6VBg/TM8mO0NSHtI/AAAAAAAAGLs/zKLIHTy-nuE/s1600/Nissan-Juke_2011_1280x960_wallpaper_19.jpg
Just my Desktop: Wallpapers Nissan Juke-Poze cu Noul Nissan Juke


http://3.bp.blogspot.com/_FoXyvaPSnVk/S3MKvdlgMXI/AAAAAAACgFw/bU7rvjA7rTY/s1600/Nissan-Juke-32.jpg
2011 Nissan Juke Wallpaper


http://4.bp.blogspot.com/_FoXyvaPSnVk/S3MLHywLGTI/AAAAAAACgHw/Gmlk4f6loFc/s1600/Nissan-Juke-24.jpg
2011 Nissan Juke Wallpaper


http://2.bp.blogspot.com/_FoXyvaPSnVk/S3MLGLigKxI/AAAAAAACgHI/qiG95gHBJ3c/s1600/Nissan-Juke-18.jpg
2011 Nissan Juke Wallpaper


http://2.bp.blogspot.com/-VF3Ejk8sHpY/TvpvvSr0RxI/AAAAAAAAD1s/y3s-d39Dedk/s1600/Nissan-Juke-R_Concept_2011_1280x960_wallpaper_0c.jpg
Nissan Juke R High Performance Concept Wallpaper The 2011 Nissan Juke


http://4.bp.blogspot.com/_FoXyvaPSnVk/S0X-avOG7DI/AAAAAAACZCA/FwQx4rW2_TA/s1600/Nissan-Qazana-Concept-38.jpg
supercar center: Nissan Juke Baby Crossover


http://2.bp.blogspot.com/_oau7M_4urd8/S-lB4PRyUZI/AAAAAAAAAXQ/S4nzPQNHPcU/s1600/Nissan-Juke_2011_1600x1200_wallpaper_01.jpg
นิสสันจู้ค ลูกบ้าเที่ยว ...


http://4.bp.blogspot.com/_3V1YR3u6VBg/TM8mSoZNfJI/AAAAAAAAGLw/DUepErEo2_o/s1600/Nissan-Juke_2011_1280x960_wallpaper_0e.jpg
Just my Desktop: Wallpapers Nissan Juke-Poze cu Noul Nissan Juke


http://4.bp.blogspot.com/_gvqSxFO-17Y/TLGcGDDWmhI/AAAAAAAABco/LHuUdIKjmDc/s1600/125_duke_wallpaper_2_01.jpg
Email This BlogThis! Share to Twitter Share to Facebook


http://1.bp.blogspot.com/_gvqSxFO-17Y/TLGcA_6SeQI/AAAAAAAABck/oCq3zacZuj0/s1600/125_duke_wallpaper_1_01.jpg
stickering on pulsar180 | Page 5


http://2.bp.blogspot.com/-eylH-CGcWO8/Tov6OP0iMsI/AAAAAAAAAsU/NfC_VZ7k84I/s1600/2012+Nissan+Juke+Kuro+edition+wallpaper+-+1.jpg
2012 Nissan Juke Kuro Edition Wallpaper


http://1.bp.blogspot.com/_NpeIbAsXPso/TGSRcDBq7zI/AAAAAAAAGe8/LXFmknlCANM/s1600/Nissan+juke++06.jpg
99 WALLPAPERS: 2011 NISSAN JUKE WALLPAPERS


http://1.bp.blogspot.com/-QZP-qLHTspk/TepcK_XUPCI/AAAAAAAAAfM/C9C-fmKTACk/s1600/wallpaper_duke_nukem_forever_03_1440x900.jpg
Wallpaper] Duke Nukem Forever variante


http://2.bp.blogspot.com/-h2zLYmWne6o/TfnPdo-kz3I/AAAAAAAAACA/gLFrROIK0JU/s1600/Nissan-Qazana-Concept-1.jpg
nissan juke related car picture,351 to 400 - Hello Search


http://4.bp.blogspot.com/_rL1wtoHITCk/TPTI2iM_AtI/AAAAAAAACEQ/5djfTwPrnN4/s1600/Nissan_Juke_HD_Wallpaper.jpg
Nissan Juke 15 RX marketed in 2011 - Harga Mobil Indonesia


http://3.bp.blogspot.com/_lsyt_wQ2awY/TDR_j1QJ3YI/AAAAAAAB7L0/P0iC4OWxMYk/s1600/Nissan-Juke_2011_1280x960_wallpaper_05.jpg
2011 Nissan Juke | Nissan Autos Spain


http://2.bp.blogspot.com/-A1zQj3Iatcs/TuQoPTBI2dI/AAAAAAAAKUU/f1z59GfJ5e0/s1600/2011-NISSAN-Juke-R_Concept_japanese-car-wallpapers_05.jpg
Car desktop wallpaper. Car insurance, accident lawyers info.


http://4.bp.blogspot.com/-tSnysrBgRgw/UZejuv5E3DI/AAAAAAAACT4/_CCmjX6G678/s1600/976705_460382627389975_1814705825_o.jpg
Papercraft realistic - Goku SSj1


http://2.bp.blogspot.com/-g5kDO42EN_M/Tz-MTqCYsqI/AAAAAAAAABs/qT-IbcatnKI/s1600/nissan-juke-nismo-concept-front.jpg
Wallpapers Background: Nissan March Modified | Cars Wallpapers


http://4.bp.blogspot.com/-z1WQEaQ7Lg8/T-Kh2NqCMKI/AAAAAAAADts/UKHfCp1XPR8/s1600/Nissan+Juke+Nismo+hd+Wallpapers+2013_5.jpg
Nissan Juke Nismo hd Wallpapers 2013


http://4.bp.blogspot.com/_3V1YR3u6VBg/TM8mWNyGV5I/AAAAAAAAGL8/0j6r9wwnPO0/s1600/Nissan-Juke_2011_1280x960_wallpaper_06.jpg
Just my Desktop: Wallpapers Nissan Juke-Poze cu Noul Nissan Juke


http://4.bp.blogspot.com/-yPSXRW4lvOw/T-bbDvJCtJI/AAAAAAAACFA/SZZG18ZKAJI/s1600/Nissan-Juke-SUV-1.jpg
Le prix du Nissan Juke entre 16.490 et 26.040 €


http://3.bp.blogspot.com/_3V1YR3u6VBg/TM8mVbO6gEI/AAAAAAAAGL4/H-vLVSxmMPM/s1600/Nissan-Juke_2011_1280x960_wallpaper_02.jpg
Just my Desktop: Wallpapers Nissan Juke-Poze cu Noul Nissan Juke


http://4.bp.blogspot.com/_u7gHxLyplB4/TAldeMdIvdI/AAAAAAAAAIk/EMiar9cGCnM/s1600/DSC_0057.JPG
Jukebox Wallpaper


http://1.bp.blogspot.com/_EvyYQ49LpOg/TIPUo2FQTRI/AAAAAAAACWc/rI9XIHJatFI/s1600/Jukebox+014.JPG
ExoTiki's Vintage Corner: Jukebox Rock Ola - Empress 200


http://1.bp.blogspot.com/-D_GjJRygNuc/T83iMzoNtUI/AAAAAAAABIc/P2QNE0Fa_pE/s1600/Nissan+Juke+Nismo+Concept+01.jpg
2012 Nissan Juke Nismo Concept - Free Full HD Car Wallpaper


http://1.bp.blogspot.com/_HUBq08zcOIU/TCEizHWBaMI/AAAAAAAAANs/4JrAIvZZt7s/s1600/duke-nukem-3d.jpg
INTERVICIO: wallpaper


http://1.bp.blogspot.com/-CU4Q8W795xw/T-KhocYVK0I/AAAAAAAADtc/oZT5QK2fOkU/s1600/Nissan+Juke+Nismo+hd+Wallpapers+2013_3.jpg
Nissan Juke Nismo hd Wallpapers 2013


http://4.bp.blogspot.com/-0kfzY2EkJU8/Tw5HqOXVUzI/AAAAAAAAD8E/R3jny05PuyI/s1600/Nissan-Juke_Shiro_2012_1280x960_wallpaper_01.jpg
Nissan Juke Shiro | ACTUALIDAD MOTOR


http://4.bp.blogspot.com/-FQ_fpexpIIY/T-KhfmPtyhI/AAAAAAAADtE/dA7KfFLrz6M/s1600/Nissan+Juke+Nismo+hd+Wallpapers+2013_.jpg
Nissan Juke Nismo hd Wallpapers 2013


http://3.bp.blogspot.com/_lsyt_wQ2awY/TDSASD0IwqI/AAAAAAAB7Ms/9lDj2uN5EuI/s1600/Nissan-Juke_2011_1280x960_wallpaper_03.jpg
2011 Nissan Juke | Nissan Autos Spain


http://1.bp.blogspot.com/-nrrb9KpMdVI/TeexsOhRRlI/AAAAAAAAAG4/3bwu8qARPMk/s1600/Duke+Nukem+Forever+Wallpapers-5.jpg
duke nukem forever wallpaper duke nukem forever wallpaper duke nukem ...


http://25.media.tumblr.com/3da1f7b9b35bc89fc8267cf24cfa3c20/tumblr_mghttisRGM1qeuo6vo1_1280.jpg
2014 Renault Captur CrossoverThe French Nissan Juke.(vía Carscoop)


http://24.media.tumblr.com/tumblr_mdy84wHHaS1riuznko1_1280.jpg
Lumberjack Jukebox


http://24.media.tumblr.com/tumblr_mdy8kcGs5F1riuznko1_1280.jpg
Lumberjack Jukebox


http://25.media.tumblr.com/tumblr_m2pbzbCQt81qhk45do1_1280.jpg
Ictio Mutatis Get fro FREE!!! the Ictio-Mutatis Wallpaper, a digital ...


http://24.media.tumblr.com/ed2c71ffd4f29b4476e1c761322292cb/tumblr_mij4dp1btn1r1dcs8o1_1280.jpg
Rossi, Carlo (1775-1849)Window wall of the White Hall, Mikhailovsky ...


http://25.media.tumblr.com/tumblr_m79spc7dvS1rpp9doo1_1280.jpg
Pics Of Linux


http://25.media.tumblr.com/tumblr_md1x3pH3SP1rpvjjio1_1280.jpg
DRAW PAINT PRINT • Giambattista Tiepolo: Apollo and the Continents ...


http://24.media.tumblr.com/tumblr_lvpsgrbsyG1r6i51ao1_1280.jpg
explicicism (pussy-pro: a wall of vagina)


http://24.media.tumblr.com/tumblr_m8m10lS4361rqnarho1_1280.jpg
Sarah Duke


http://24.media.tumblr.com/1a09b62023813d3ef25e76073328e4e4/tumblr_mgff41a27S1qedd28o1_1280.jpg
Jazz Legends Charlie Parker Quotes Duke Ellington John Coltrane ...


http://24.media.tumblr.com/bd4277d47612d3297517c1eb01eb10cb/tumblr_mk11ehI0gg1qe6vsbo1_1280.jpg
become a Gay Rights mecca, forever known as Stonewall. Hit The Wall ...


http://25.media.tumblr.com/tumblr_lmncqgqt2e1qinrk0o1_1280.jpg
VGB - Video Games Babes


http://24.media.tumblr.com/tumblr_m9eb5r4C811qeef3ro4_1280.jpg
bizhijidi.com-Sara Albert, 模特, 时尚摄影11-


http://25.media.tumblr.com/tumblr_mcn1ydrVzV1rpvjjio1_1280.jpg
... Duke of Urbino, possibly to celebrate his 1534 marriage. It would


http://25.media.tumblr.com/tumblr_me4xc3YYa71r60734o1_1280.jpg
Billy Z Duke - Womb 207(1990), Marker on Paper: 36 x 48 in.


http://25.media.tumblr.com/tumblr_maaeymX93Q1qk4qdqo1_1280.jpg
Dr Raoul Duke


http://25.media.tumblr.com/tumblr_lxm5k8Qleb1qd27qro1_1280.jpg
hamilton # lamp # vintage


http://25.media.tumblr.com/tumblr_m9y8o9cO6P1rqnarho1_1280.jpg
Sarah Duke


http://25.media.tumblr.com/5eb34929a554b14c95f391206d3d03b2/tumblr_mi3cb8hlBG1rrnekqo1_1280.jpg
Music. Rare Book, Manuscript, and Special Collections Library, Duke ...


http://25.media.tumblr.com/tumblr_lnwa7dAWXB1qcr88ho1_1280.jpg
kick the elephant


http://24.media.tumblr.com/tumblr_m11voz2rul1r75h53o1_1280.jpg
dutch house | Tumblr


http://24.media.tumblr.com/tumblr_mdgl3j7aFw1r2eskeo1_1280.jpg
maya onoda # paper sculpture # kaleidoscope


http://25.media.tumblr.com/28dbad3970c1b9381c5dda3f9493339d/tumblr_mkotud3fVL1qd382lo1_1280.jpg
Hey to Your Mama N'em


http://25.media.tumblr.com/f252e39046189f657c405af3c0b22886/tumblr_mlvniwgibw1rkuug5o1_1280.jpg
duke of cambridge # duchess of cambridge # harry potter # my edit


http://25.media.tumblr.com/tumblr_m4qeoy79Tj1r2gmfbo1_1280.jpg
af9f7f1b1a62 Free Divx Adult Movies Toxxxy Official homepage of fetish ...


http://25.media.tumblr.com/tumblr_m1ysj7lm2h1r4ium7o1_1280.jpg
Modo vehicles with snow tires and all-wheel drive


http://24.media.tumblr.com/tumblr_lw88ucs3dT1r1ckuno1_1280.jpg
getter robo # shin getter robo


http://25.media.tumblr.com/tumblr_m9y8lnDjBH1rqnarho1_1280.jpg
Sarah Duke


http://24.media.tumblr.com/tumblr_m0oofae0CA1rqv23yo1_1280.jpg
hopscotch # hopscotch 2012


http://24.media.tumblr.com/tumblr_m8lb6wAmR61r9e5ymo1_1280.jpg
Ronaldo R9


http://25.media.tumblr.com/tumblr_maqda5KS2r1qkxa3do1_1280.png
... PAPER so he turned out way dark ugh ugh I am so pissed at


http://24.media.tumblr.com/tumblr_m8205doNtb1rpgoq2o1_1280.jpg
Amber Heard Charger


http://25.media.tumblr.com/tumblr_mdeghqqiTE1qhgi50o1_1280.jpg
washington nationals # bryce harper # rookie of the year


http://24.media.tumblr.com/a8dbf993106b72885b7864be4a826fd8/tumblr_mf2ctnJea11s062dwo4_1280.jpg
Shell Yeah! TMNT 2012 • fifian: TMNT 2012 wallpapers. Found them ...


http://25.media.tumblr.com/tumblr_m1k3gzQ0O71qkk8zeo1_1280.jpg
16/07/12- Putney Bridge, London


http://25.media.tumblr.com/tumblr_m632lq0Yjp1qk5tzzo1_1280.jpg
harry potter studio tour | Tumblr


http://24.media.tumblr.com/tumblr_m4t360oGAV1qe17xxo1_1280.jpg
Photos from h3llbetty / Southern Delight


http://25.media.tumblr.com/tumblr_m5w91wl3Wg1r0vs1ao1_1280.jpg
this from a video… Hope you like as much as I do. Now my wallpaper ...


http://25.media.tumblr.com/tumblr_mccrqxkLtj1qgheipo1_1280.jpg
tampa bay bucs cheerleaders # nfl # nfl cheerleader


http://24.media.tumblr.com/tumblr_m9vttsbBfY1qbo39mo1_1280.jpg
Cy Twombly - The Song of the Border-Guard, 1952. Woodcut on paper


READ MORE - Wallpaper Juke
Related Posts Plugin for WordPress, Blogger...