【Android源码 栏目提醒】:本文主要为网学会员提供“MediaPlayerDemo_Video.java android源码 - 编程语言”,希望对需要MediaPlayerDemo_Video.java android源码 - 编程语言网友有所帮助,学习一下!
MediaPlayerDemo_Video.java android源码 / Copyright C 2009 The Android Open Source Project Licensed under the Apache License Version 2.0 the License you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. See the License for the specific language governing permissions and limitations under the License. / package com.example.android.apis.media import com.example.android.apis.R import android.app.Activity import android.media.AudioManager import android.media.MediaPlayer import android.media.MediaPlayer.OnBufferingUpdateListener import android.media.MediaPlayer.OnCompletionListener import android.media.MediaPlayer.OnPreparedListener import android.media.MediaPlayer.OnVideoSizeChangedListener import android.os.Bundle import android.util.Log import android.view.SurfaceHolder import android.view.SurfaceView import android.widget.Toast public class MediaPlayerDemo_Video extends Activity implements OnBufferingUpdateListener OnCompletionListener OnPreparedListener OnVideoSizeChangedListener SurfaceHolder.Callback private static final String TAG MediaPlayerDemo private int mVideoWidth private int mVideoHeight private MediaPlayer mMediaPlayer private SurfaceView mPreview private SurfaceHolder holder private String path private Bundle extras 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 boolean mIsVideoSizeKnown false private boolean mIsVideoReadyToBePlayed false / Called when the activity is first created. / Override public void onCreateBundle icicle super.onCreateicicle setContentViewR.layout.mediaplayer_2 mPreview SurfaceView findViewByIdR.id.surface holder mPreview.getHolder holder.addCallbackthis holder.setTypeSurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS extras getIntent.getExtras private void playVideoInteger Media doCleanUp try switch Media case LOCAL_VIDEO: / TODO: Set the path variable to a local media file path. / path if path // Tell the user to provide a media file URL. Toast .makeText MediaPlayerDemo_Video.this Please edit MediaPlayerDemo_Video Activity and set the path variable to your media file path. Your media file must be stored on sdcard. Toast.LENGTH_LONG.show break case STREAM_VIDEO: / TODO: Set path variable to progressive streamable mp4 or 3gpp format URL. Http protocol should be used. Mediaplayer can only play progressive streamable contents which basically means: 1. the movie atom has to precede all the media data atoms. 2. The clip has to be reasonably interleaved. / path if path // Tell the user to provide a media file URL. Toast .makeText MediaPlayerDemo_Video.this Please edit MediaPlayerDemo_Video Activity and set the path variable to your media file URL. Toast.LENGTH_LONG.show break // Create a new media player and set the listeners mMediaPlayer new MediaPlayer mMediaPlayer.setDataSourcepath mMediaPlayer.setDisplayholder mMediaPlayer.prepare mMediaPlayer.setOnBufferingUpdateListenerthis mMediaPlayer.setOnCompletionListenerthis mMediaPlayer.setOnPreparedListenerthis mMediaPlayer.setOnVideoSizeChangedListenerthis mMediaPlayer.setAudioStreamTypeAudioManager.STREAM_MUSIC catch Exception e Log.eTAG error: e.getMessage e public void onBufferingUpdateMediaPlayer arg0 int percent Log.dTAG onBufferingUpdate percent: percent public void onCompletionMediaPlayer arg0 Log.dTAG onCompletion called public void onVideoSizeChangedMediaPlayer mp int width int height Log.vTAG onVideoSizeChanged called if width 0 height 0 Log.eTAG invalid video width width or height height return mIsVideoSizeKnown true mVideoWidth width mVideoHeight height if mIsVideoReadyToBePlayed mIsVideoSizeKnown startVideoPlayback public void onPreparedMediaPlayer mediaplayer Log.dTAG onPrepared called mIsVideoReadyToBePlayed true if mIsVideoReadyToBePlayed mIsVideoSizeKnown startVideoPlayback public void surfaceChangedSurfaceHolder surfaceholder int i int j int k Log.dTAG surfaceChanged called public void surfaceDestroyedSurfaceHolder surfaceholder Log.dTAG surfaceDestroyed called public void surfaceCreatedSurfaceHolder holder Log.dTAG surfaceCreated called playVideoextras.getIntMEDIA Override protected void onPause super.onPause releaseMediaPlayer doCleanUp Override protected void onDestroy super.onDestroy releaseMediaPlayer doCleanUp private void releaseMediaPlayer if mMediaPlayer null mMediaPlayer.release mMediaPlayer null private void doCleanUp mVideoWidth 0 mVideoHeight 0 mIsVideoReadyToBePlayed false mIsVideoSizeKnown false private void startVideoPlayback Log.vTAG startVideoPlayback holder.setFixedSizemVideoWidth mVideoHeight mMediaPlayer.start