视频播放器videoview surfaceview

代码:

package com.itheima.videoview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;

public class MainActivity extends Activity {

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

        vv = (VideoView) findViewById(R.id.vv);

        vv.setVideoPath("http://192.168.22.136:8080/11.mp4");

        vv.start();


    }


}

surfaceview表面视图

双缓冲区:使用两个线程协调工作;

surfaceview 重量级空间,当画面可见时,双缓冲器被创建,占用较高的cpu和内存资源。
当界面不可见时,双缓冲器占用释放。

音乐播放器API
package com.itheima.surfaceview;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainActivity extends Activity {

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

    sv = (SurfaceView) findViewById(R.id.sv);

    SurfaceHolder holder = sv.getHolder();
    //推送缓冲数据,api 13之前的版本上必须执行这行代码,13版本之后,播放器可以自动把
    //数据加载到缓冲区
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


    holder.addCallback(new Callback() {

        private MediaPlayer mediaPlay;



        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mediaPlay = new MediaPlayer();

                mediaPlay.setDataSource("http://192.168.22.136:8080/11.mp4");
                mediaPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);

                mediaPlay.setDisplay(holder);

                //使用阻塞式准备方法,等待数据资源准备完成后才开始播放,用于播放本地的音视频文件

// mediaPlay.prepare();
//使用异步式准备方法,使用子线程等待数据资源准备完成后才开始播放,用于播放网络上的音视频文件
mediaPlay.prepareAsync();

                mediaPlay.setOnPreparedListener(new OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer mp) {

                        mediaPlay.start();
                    }
                });


            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(mediaPlay != null && mediaPlay.isPlaying()){
                mediaPlay.stop();

                mediaPlay.release();

                mediaPlay = null;

// mediaPlay.seekTo(msec);

            }

        }


        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

        }
    });
}

}

你可能感兴趣的:(android,视频)