package             video.demo;             /*             * 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.             */                         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;             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                         boolean             mIsVideoSizeKnown             =                         false            ;             private                         boolean             mIsVideoReadyToBePlayed             =                         false            ;             /**             * * Called when the activity is first created.             */             @Override             public                         void             onCreate(Bundle icicle) {             super            .onCreate(icicle); setContentView(R.layout.mediaplayer_2); mPreview             =             (SurfaceView) findViewById(R.id.surface); holder             =             mPreview.getHolder(); holder.addCallback(            this            ); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }             private                         void             playVideo() { doCleanUp();             try             {             /*             * 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             =                         "            http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4            "            ; path             =                         "            http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp            "            ;             //             Create a new media player and set the listeners                          mMediaPlayer             =                         new             MediaPlayer(); mMediaPlayer.setDataSource(path); mMediaPlayer.setDisplay(holder); mMediaPlayer.prepare(); mMediaPlayer.setOnBufferingUpdateListener(            this            ); mMediaPlayer.setOnCompletionListener(            this            ); mMediaPlayer.setOnPreparedListener(            this            ); mMediaPlayer.setOnVideoSizeChangedListener(            this            ); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); }             catch             (Exception e) { Log.e(TAG,             "            error:             "                         +             e.getMessage(), e); } }             public                         void             onBufferingUpdate(MediaPlayer arg0,             int             percent) { Log.d(TAG,             "            onBufferingUpdate percent:            "                         +             percent); }             public                         void             onCompletion(MediaPlayer arg0) { Log.d(TAG,             "            onCompletion called            "            ); }             public                         void             onVideoSizeChanged(MediaPlayer mp,             int             width,             int             height) { Log.v(TAG,             "            onVideoSizeChanged called            "            );             if             (width             ==                         0                         ||             height             ==                         0            ) { Log.e(TAG,             "            invalid video width(            "                         +             width             +                         "            ) or height(            "                         +             height             +                         "            )            "            );             return            ; } mIsVideoSizeKnown             =                         true            ; mVideoWidth             =             width; mVideoHeight             =             height;             if             (mIsVideoReadyToBePlayed             &&             mIsVideoSizeKnown) { startVideoPlayback(); } }             public                         void             onPrepared(MediaPlayer mediaplayer) { Log.d(TAG,             "            onPrepared called            "            ); mIsVideoReadyToBePlayed             =                         true            ;             if             (mIsVideoReadyToBePlayed             &&             mIsVideoSizeKnown) { startVideoPlayback(); } }             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            "            ); }             public                         void             surfaceCreated(SurfaceHolder holder) { Log.d(TAG,             "            surfaceCreated called            "            ); playVideo(); } @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.v(TAG,             "            startVideoPlayback            "            ); holder.setFixedSize(mVideoWidth, mVideoHeight); mMediaPlayer.start(); } }         
                            <?                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"                >                                 <                SurfaceView                 android:id                ="@+id/surface"                 android:layout_width                ="wrap_content"                 android:layout_height                ="wrap_content"                 android:layout_gravity                ="center"                >                                 </                SurfaceView                >                                 </                LinearLayout                >            

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)换肤之旅——主题切换
  2. SWIG与JAVA的交互指南一
  3. 安卓和机器学习 -- 学习周记{第1周}
  4. Android系统权限
  5. Android(安卓)Studio使用Git提交、分支、
  6. android设备静默升级
  7. Android(安卓)设备在Play Store无法搜索
  8. Android时光轴实现淘宝物流信息浏览效果
  9. Android导出与使用带资源的lib包
  10. Android(安卓)进阶——高级UI必知必会之