1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.media; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.media.AudioManager; 23 import android.media.MediaPlayer; 24 import android.media.MediaPlayer.OnBufferingUpdateListener; 25 import android.media.MediaPlayer.OnCompletionListener; 26 import android.media.MediaPlayer.OnPreparedListener; 27 import android.media.MediaPlayer.OnVideoSizeChangedListener; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.view.SurfaceHolder; 31 import android.view.SurfaceView; 32 import android.widget.Toast; 33 34 35 public class MediaPlayerDemo_Video extends Activity implements 36 OnBufferingUpdateListener, OnCompletionListener, 37 OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback { 38 39 private static final String TAG = "MediaPlayerDemo"; 40 private int mVideoWidth; 41 private int mVideoHeight; 42 private MediaPlayer mMediaPlayer; 43 private SurfaceView mPreview; 44 private SurfaceHolder holder; 45 private String path; 46 private Bundle extras; 47 private static final String MEDIA = "media"; 48 private static final int LOCAL_AUDIO = 1; 49 private static final int STREAM_AUDIO = 2; 50 private static final int RESOURCES_AUDIO = 3; 51 private static final int LOCAL_VIDEO = 4; 52 private static final int STREAM_VIDEO = 5; 53 private boolean mIsVideoSizeKnown = false; 54 private boolean mIsVideoReadyToBePlayed = false; 55 56 /** 57 * 58 * Called when the activity is first created. 59 */ 60 @Override onCreate(Bundle icicle)61 public void onCreate(Bundle icicle) { 62 super.onCreate(icicle); 63 setContentView(R.layout.mediaplayer_2); 64 mPreview = (SurfaceView) findViewById(R.id.surface); 65 holder = mPreview.getHolder(); 66 holder.addCallback(this); 67 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 68 extras = getIntent().getExtras(); 69 70 } 71 playVideo(Integer Media)72 private void playVideo(Integer Media) { 73 doCleanUp(); 74 try { 75 76 switch (Media) { 77 case LOCAL_VIDEO: 78 /* 79 * TODO: Set the path variable to a local media file path. 80 */ 81 path = ""; 82 if (path == "") { 83 // Tell the user to provide a media file URL. 84 Toast 85 .makeText( 86 MediaPlayerDemo_Video.this, 87 "Please edit MediaPlayerDemo_Video Activity, " 88 + "and set the path variable to your media file path." 89 + " Your media file must be stored on sdcard.", 90 Toast.LENGTH_LONG).show(); 91 92 } 93 break; 94 case STREAM_VIDEO: 95 /* 96 * TODO: Set path variable to progressive streamable mp4 or 97 * 3gpp format URL. Http protocol should be used. 98 * Mediaplayer can only play "progressive streamable 99 * contents" which basically means: 1. the movie atom has to 100 * precede all the media data atoms. 2. The clip has to be 101 * reasonably interleaved. 102 * 103 */ 104 path = ""; 105 if (path == "") { 106 // Tell the user to provide a media file URL. 107 Toast 108 .makeText( 109 MediaPlayerDemo_Video.this, 110 "Please edit MediaPlayerDemo_Video Activity," 111 + " and set the path variable to your media file URL.", 112 Toast.LENGTH_LONG).show(); 113 114 } 115 116 break; 117 118 119 } 120 121 // Create a new media player and set the listeners 122 mMediaPlayer = new MediaPlayer(); 123 mMediaPlayer.setDataSource(path); 124 mMediaPlayer.setDisplay(holder); 125 mMediaPlayer.prepare(); 126 mMediaPlayer.setOnBufferingUpdateListener(this); 127 mMediaPlayer.setOnCompletionListener(this); 128 mMediaPlayer.setOnPreparedListener(this); 129 mMediaPlayer.setOnVideoSizeChangedListener(this); 130 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 131 132 133 } catch (Exception e) { 134 Log.e(TAG, "error: " + e.getMessage(), e); 135 } 136 } 137 onBufferingUpdate(MediaPlayer arg0, int percent)138 public void onBufferingUpdate(MediaPlayer arg0, int percent) { 139 Log.d(TAG, "onBufferingUpdate percent:" + percent); 140 141 } 142 onCompletion(MediaPlayer arg0)143 public void onCompletion(MediaPlayer arg0) { 144 Log.d(TAG, "onCompletion called"); 145 } 146 onVideoSizeChanged(MediaPlayer mp, int width, int height)147 public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 148 Log.v(TAG, "onVideoSizeChanged called"); 149 if (width == 0 || height == 0) { 150 Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")"); 151 return; 152 } 153 mIsVideoSizeKnown = true; 154 mVideoWidth = width; 155 mVideoHeight = height; 156 if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 157 startVideoPlayback(); 158 } 159 } 160 onPrepared(MediaPlayer mediaplayer)161 public void onPrepared(MediaPlayer mediaplayer) { 162 Log.d(TAG, "onPrepared called"); 163 mIsVideoReadyToBePlayed = true; 164 if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 165 startVideoPlayback(); 166 } 167 } 168 surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k)169 public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) { 170 Log.d(TAG, "surfaceChanged called"); 171 172 } 173 surfaceDestroyed(SurfaceHolder surfaceholder)174 public void surfaceDestroyed(SurfaceHolder surfaceholder) { 175 Log.d(TAG, "surfaceDestroyed called"); 176 } 177 178 surfaceCreated(SurfaceHolder holder)179 public void surfaceCreated(SurfaceHolder holder) { 180 Log.d(TAG, "surfaceCreated called"); 181 playVideo(extras.getInt(MEDIA)); 182 183 184 } 185 186 @Override onPause()187 protected void onPause() { 188 super.onPause(); 189 releaseMediaPlayer(); 190 doCleanUp(); 191 } 192 193 @Override onDestroy()194 protected void onDestroy() { 195 super.onDestroy(); 196 releaseMediaPlayer(); 197 doCleanUp(); 198 } 199 releaseMediaPlayer()200 private void releaseMediaPlayer() { 201 if (mMediaPlayer != null) { 202 mMediaPlayer.release(); 203 mMediaPlayer = null; 204 } 205 } 206 doCleanUp()207 private void doCleanUp() { 208 mVideoWidth = 0; 209 mVideoHeight = 0; 210 mIsVideoReadyToBePlayed = false; 211 mIsVideoSizeKnown = false; 212 } 213 startVideoPlayback()214 private void startVideoPlayback() { 215 Log.v(TAG, "startVideoPlayback"); 216 holder.setFixedSize(mVideoWidth, mVideoHeight); 217 mMediaPlayer.start(); 218 } 219 } 220