編輯:Android開發實例
上文調用android api播放視頻使用的是VideoView,是比較高層的API播放視頻,比較簡明,但定制性方面不如使用MediaPlayer。
參考的示例是android SDK示例代碼:
源代碼:
- package com.easymorse;
- import java.io.IOException;
- import android.app.Activity;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnBufferingUpdateListener;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class MPlayerActivity extends Activity implements
- OnBufferingUpdateListener, OnCompletionListener,
- MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
- private MediaPlayer mediaPlayer;
- private SurfaceView surfaceView;
- private SurfaceHolder surfaceHolder;
- private int videoWidth;
- private int videoHeight;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.surfaceView = (SurfaceView) this.findViewById(R.id.surface);
- this.surfaceHolder = this.surfaceView.getHolder();
- this.surfaceHolder.addCallback(this);
- this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- Log.v("mplayer", ">>>create ok.");
- }
- private void playVideo() throws IllegalArgumentException,
- IllegalStateException, IOException {
- this.mediaPlayer = new MediaPlayer();
- this.mediaPlayer
- .setDataSource("http://sayedhashimi.com/downloads/android/movie.mp4");
- this.mediaPlayer.setDisplay(this.surfaceHolder);
- this.mediaPlayer.prepare();
- this.mediaPlayer.setOnBufferingUpdateListener(this);
- this.mediaPlayer.setOnPreparedListener(this);
- this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- Log.v("mplayer", ">>>play video");
- }
- @Override
- public void onBufferingUpdate(MediaPlayer mp, int percent) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onCompletion(MediaPlayer mp) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPrepared(MediaPlayer mp) {
- this.videoWidth = this.mediaPlayer.getVideoWidth();
- this.videoHeight = this.mediaPlayer.getVideoHeight();
- if (this.videoHeight != 0 && this.videoWidth != 0) {
- this.surfaceHolder.setFixedSize(this.videoWidth, this.videoHeight);
- this.mediaPlayer.start();
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- Log.v("mplayer", ">>>surface changed");
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- try {
- this.playVideo();
- } catch (Exception e) {
- Log.e("mplayer", ">>>error", e);
- }
- Log.v("mplayer", ">>>surface created");
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v("mplayer", ">>>surface destroyed");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (this.mediaPlayer != null) {
- this.mediaPlayer.release();
- this.mediaPlayer = null;
- }
- }
- }
源代碼見:
http://easymorse.googlecode.com/svn/tags/mplayer-1.0
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android SDK本身提供了一種默認創建菜單的機制。但通過這種機制創建的菜單雖然從功能上很完備,但在界面效果
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
屏幕亮度自動調節:主要是從Sensor分析之中分離出來分析LIGHT 光線感應器,因此就分析一下自動調節屏幕亮度(手機隨著光線的強度自我調節,也就是在亮的光線下屏