You are on page 1of 9

Vedio Player

003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 import import import import import import import import import import import import import import import import import import import import android.app.Activity; android.graphics.PixelFormat; android.media.MediaPlayer; android.media.MediaPlayer.OnBufferingUpdateListener; android.media.MediaPlayer.OnCompletionListener; android.media.MediaPlayer.OnErrorListener; android.os.Bundle; android.util.Log; android.view.SurfaceHolder; android.view.SurfaceView; android.view.View; android.webkit.URLUtil; android.widget.EditText; android.widget.ImageButton; java.io.File; java.io.FileOutputStream; java.io.IOException; java.io.InputStream; java.net.URL; java.net.URLConnection;

public class VideoPlayer extends Activity implements OnErrorListener, OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback { private static final String TAG = "VideoPlayer"; private private private private private private private private private MediaPlayer mp; SurfaceView mPreview; EditText mPath; SurfaceHolder holder; ImageButton mPlay; ImageButton mPause; ImageButton mReset; ImageButton mStop; String current;

/** * Called when the activity is first created. */ public void onCreate(Bundle icicle) { super.onCreate(icicle);

046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094

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 {

095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 player

final String path = mPath.getText().toString(); Log.v(TAG, "path: " + path); // If the path has not changed, just start the media 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

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

* 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---> if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } public void onBufferingUpdate(MediaPlayer arg0, int percent) { Log.d(TAG, "onBufferingUpdate called ---> percent:" + what:" + what + " extra:" +

extra);

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

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) { 205 Log.d(TAG, "surfaceChanged called"); 206 } 207 208 public void surfaceDestroyed(SurfaceHolder surfaceholder) { 209 Log.d(TAG, "surfaceDestroyed called"); 210 } 211 }

Android Developement
Monday, April 6, 2009
Android : MediaPlayer Example
First Create a MediaPlayerDemo.java Project File package com.example.android.apis.media; import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import android.widget.Toast; import com.example.android.apis.R; public class MediaPlayerDemo_Audio extends Activity { private static final String TAG = "MediaPlayerDemo"; private MediaPlayer mMediaPlayer; private static final String MEDIA = "media"; private static final int LOCAL_AUDIO = 1; private static final int STREAM_AUDIO = 2; private static final int RESOURCES_AUDIO = 3; private static final int LOCAL_VIDEO = 4; private static final int STREAM_VIDEO = 5; private String path; private TextView tx; public void onCreate(Bundle icicle) { super.onCreate(icicle); tx = new TextView(this); setContentView(tx); Bundle extras = getIntent().getExtras(); playAudio(extras.getInt(MEDIA)); } private void playAudio(Integer media) { try { switch (media) { case LOCAL_AUDIO: /** * TODO: Set the path variable to a local audio file path. */ path =

"@E:/raww/test_cbr"; if (path =="") { // Tell the user to provide an audio file URL. Toast .makeText( MediaPlayerDemo_Audio.this, "Please edit MediaPlayer_Audio Activity, " + "and set the path variable to your audio file path." + " Your audio file must be stored on sdcard.", Toast.LENGTH_LONG).show(); } mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource("@E:/raww/test_cbr"); mMediaPlayer.prepare(); mMediaPlayer.start(); break; case RESOURCES_AUDIO: /** * TODO: Upload a audio file to res/raw folder and provide * its resid in MediaPlayer.create() method. */ mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); mMediaPlayer.start(); } tx.setText("Playing audio..."); } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); } } @Override protected void onDestroy() { super.onDestroy(); // TODO Auto-generated method stub if (mMediaPlayer != null) { mMediaPlayer.release(); mMediaPlayer = null; } } }

Android Developement
Monday, April 6, 2009
Android API : Media Player
Package Android.media Interfaces MediaPlayer.OnBufferingUpdateListener Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network. MediaPlayer.OnCompletionListener Interface definition for a callback to be invoked when playback of a media file has completed. MediaPlayer.OnErrorListener Interface definition of a callback to be invoked when there has been an error during an asynchronous operation (other errors will throw exceptions at method call time). MediaPlayer.OnPreparedListener Interface definition for a callback to be invoked when the media file is ready for playback. MediaPlayer.OnSeekCompleteListener Interface definition of a callback to be invoked indicating the completion of a seek operation. MediaScannerConnection.MediaScannerConnectionClient An interface for notifying clients of MediaScannerConnection when a connection to the MediaScanner service has been established and when the scanning of a file has completed. Classes AsyncPlayer Plays a series of audio URIs, but does all the hard work on another thread so that any slowness with preparing or loading doesn't block the calling thread. AudioManager AudioManager provides access to volume and ringer mode control. FaceDetector Identifies the faces of people in a Bitmap graphic object. FaceDetector.Face A Face contains all the information identifying the location of a face in

a bitmap. MediaPlayer Used to play audio and video files and streams. MediaRecorder Used to record audio and video. MediaRecorder.AudioEncoder Defines the audio encoding. MediaRecorder.AudioSource Defines the audio source. MediaRecorder.OutputFormat Defines the output format. MediaScannerConnection MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. Ringtone Ringtone provides a quick method for playing a ringtone, notification, or other similar types of sounds. RingtoneManager RingtoneManager provides access to ringtones, notification, and other types of sounds. SoundPool ToneGenerator This class provides methods to play DTMF tones (ITU-T Recommendation Q.23), call supervisory tones (3GPP TS 22.001, CEPT) and proprietary tones (3GPP TS 31.111).

You might also like