Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android SurfaceView的生命周期,androidsurfaceview

Android SurfaceView的生命周期,androidsurfaceview

編輯:關於android開發

Android SurfaceView的生命周期,androidsurfaceview


本文利用SurfaceView來實現視頻的播放

本文地址:http://www.cnblogs.com/wuyudong/p/5851156.html,轉載請注明源地址。

在main.xml布局文件添加用於視頻畫面繪制的SurfaceView 控件:

<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />

項目布局設計:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_path"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入視頻文件的路徑" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_play"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放" />

        <Button
            android:id="@+id/bt_pause"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暫停" />

        <Button
            android:id="@+id/bt_replay"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重播" />

        <Button
            android:id="@+id/bt_stop"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止" />
    </LinearLayout>
    
    <SurfaceView 
        android:id="@+id/sv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

大部分代碼和《Android 多媒體播放API簡介》中的完全一樣,修改一點點代碼即可:

public class MainActivity extends Activity implements OnClickListener {

    private EditText et_path;
    private Button bt_play, bt_replay, bt_pause, bt_stop;
    private SurfaceView sv;

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sv = (SurfaceView)findViewById(R.id.sv);
        et_path = (EditText) findViewById(R.id.et_path);
        bt_play = (Button) findViewById(R.id.bt_play);
        bt_replay = (Button) findViewById(R.id.bt_replay);
        bt_pause = (Button) findViewById(R.id.bt_pause);
        bt_stop = (Button) findViewById(R.id.bt_stop);

        bt_pause.setOnClickListener(this);
        bt_play.setOnClickListener(this);
        bt_replay.setOnClickListener(this);
        bt_stop.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_play:
            play();
            break;
        case R.id.bt_replay:
            replay();
            break;
        case R.id.bt_stop:
            stop();
            break;
        case R.id.bt_pause:
            pause();
            break;
        default:
            break;
        }
    }

    /**
     * 暫停音樂
     */
    private void pause() {
        if ("繼續".equals(bt_pause.getText().toString().trim())) {
            mediaPlayer.start();
            bt_pause.setText("暫停");
            return;
        }
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            bt_pause.setText("繼續");
            return;
        }
    }

    /**
     * 重新播放
     */
    private void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            return;
        }
        play();
    }

    /**
     * 停止播放音樂
     */
    private void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release(); // 記得釋放資源
            mediaPlayer = null;
            bt_play.setEnabled(true);
        }
    }

    /**
     * 播放音樂
     */
    private void play() {
        String path = et_path.getText().toString().trim();
        File file = new File(path);
        if (file.exists() && file.length() > 0) {
            try {
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                
                /* 設置Video影片以SurfaceHolder播放 */
                mediaPlayer.setDisplay(sv.getHolder());
                
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare(); // might take long! (for buffering, etc)
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        bt_play.setEnabled(true);
                    }
                } );
                
                bt_play.setEnabled(false);
            } catch (Exception e) {
                Toast.makeText(this, "播放失敗", 0).show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "文件不存在", 0).show();
        }

    }

}

運行項目效果如下:

但是上面的代碼有點問題,那就是當點擊home或返回屏幕主界面後,在回到播放頁面,視頻黑屏

原因:

SurfaceView內部維護雙緩沖,消耗內存資源

如果發現當前SurfaceView 用戶可見的時候,創建SurfaceView的holder

如果SurfaceView變成用戶不可見的時候  銷毀SurfaceView的holder

為了觀察SurfaceView的生命周期,可以添加下面的代碼進行打印追蹤:

    sv.getHolder().addCallback(new Callback() {
            
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                System.out.println("holder被銷毀了");
            }
            
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                System.out.println("holder被創建了");
                
            }
            
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {
                System.out.println("holder的大小變化了");
            }
        });

    }

logcat中打印下面的信息:

09-07 11:36:34.613: I/System.out(28858): holder被創建了
09-07 11:36:34.623: I/System.out(28858): holder的大小變化了

返回主界面

09-07 11:39:08.245: I/System.out(28858): holder被銷毀了

再次回到視頻界面

09-07 11:39:39.405: I/System.out(28858): holder被創建了
09-07 11:39:39.405: I/System.out(28858): holder的大小變化了

可以看到,每次回到視頻界面,holder都會被重新創建

修改後的代碼如下:

public class MainActivity extends Activity implements OnClickListener {

    private EditText et_path;
    private Button bt_play, bt_replay, bt_pause, bt_stop;
    private SurfaceView sv;

    private MediaPlayer mediaPlayer;
    private int currentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sv = (SurfaceView) findViewById(R.id.sv);
        et_path = (EditText) findViewById(R.id.et_path);
        bt_play = (Button) findViewById(R.id.bt_play);
        bt_replay = (Button) findViewById(R.id.bt_replay);
        bt_pause = (Button) findViewById(R.id.bt_pause);
        bt_stop = (Button) findViewById(R.id.bt_stop);

        bt_pause.setOnClickListener(this);
        bt_play.setOnClickListener(this);
        bt_replay.setOnClickListener(this);
        bt_stop.setOnClickListener(this);

        sv.getHolder().addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                System.out.println("holder被銷毀了");

                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    currentPosition = mediaPlayer.getCurrentPosition();
                    stop();
                }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                System.out.println("holder被創建了");
                if (currentPosition > 0) {
                    play(currentPosition);
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                System.out.println("holder的大小變化了");
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_play:
            play(0);
            break;
        case R.id.bt_replay:
            replay();
            break;
        case R.id.bt_stop:
            stop();
            break;
        case R.id.bt_pause:
            pause();
            break;
        default:
            break;
        }
    }

    /**
     * 暫停音樂
     */
    private void pause() {
        if ("繼續".equals(bt_pause.getText().toString().trim())) {
            mediaPlayer.start();
            bt_pause.setText("暫停");
            return;
        }
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            bt_pause.setText("繼續");
            return;
        }
    }

    /**
     * 重新播放
     */
    private void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            return;
        }
        play(0);
    }

    /**
     * 停止播放音樂
     */
    private void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release(); // 記得釋放資源
            mediaPlayer = null;
            bt_play.setEnabled(true);
        }
    }

    /**
     * 播放音樂
     */
    private void play(final int currentPosition) {
        String path = et_path.getText().toString().trim();
        File file = new File(path);
        if (file.exists() && file.length() > 0) {
            try {
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                /* 設置Video影片以SurfaceHolder播放 */
                mediaPlayer.setDisplay(sv.getHolder());

                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare(); // might take long! (for buffering, etc)
                mediaPlayer.start();
                mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                    
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.seekTo(currentPosition);
                        
                    }
                });
                mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        bt_play.setEnabled(true);
                    }
                });

                bt_play.setEnabled(false);
            } catch (Exception e) {
                Toast.makeText(this, "播放失敗", 0).show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "文件不存在", 0).show();
        }

    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved