Android--多媒体

a、加载大图片
public class MainActivity extends Activity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. activity_main);
        iv = (ImageView) findViewById(R.id. iv);
    }

    public void loadbigpic(View v){
        File file = new File(Environment.getExternalStorageDirectory(), "22.jpg");
        Options opts= new Options();
        opts. inJustDecodeBounds = true ;
        BitmapFactory. decodeFile(file.getAbsolutePath(), opts);
        //获取到图片的宽高
        int outWidth = opts. outWidth;
        int outHeigth = opts. outHeight;

        Display display = getWindow().getWindowManager().getDefaultDisplay();
        //获取手机屏幕的大小
        int width = display. getWidth();
        int height = display. getHeight();

        int scaleX= outWidth/width;
        int scaleY = outHeigth/height;

        int scale = scaleX>scaleY?scaleX:scaleY;
        if(scale<=0){
            scale = 1;
        }

        opts. inSampleSize = scale;
        opts. inJustDecodeBounds = false ;
        Bitmap bitmap = BitmapFactory. decodeFile(file.getAbsolutePath(), opts);
        iv.setImageBitmap(bitmap);
    }
}
b、mp3播放
//一种方式
privatebooleanisplaying = false;

public void playMusic() {
        if (isplaying ) {
        return;
        }
        isplaying = true ;
        mMp = MediaPlayer.create(getApplicationContext(), R.raw.qqqg);
        mMp.start();
        mMp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
        isplaying = false ;
        }
        });
        }

//另一种方式
public class MainActivity extends Activity {

    private MediaPlayer mp;
    private EditText et_path ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        mp = new MediaPlayer();//媒体播放器
    }

    public void start(View v){
        String path = et_path.getText().toString();
        if(TextUtils. isEmpty(path)){
            Toast. makeText(this, "歌曲路径不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        mp.reset(); //重置
        try {
            mp.setDataSource(path);
            prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void prepareAsync() {
        mp.prepareAsync(); //异步准备
        //从开始的位置开始播放
        mp.seekTo(0);
        //设置准备完成监听
        mp.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            //准备完成
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    public void pausemoveon(View v){
        Button bt = (Button)v;
        if(mp .isPlaying()){//判断文件是否正在播放
            //暂停 文字变成继续
            mp.pause();
            bt.setText( "继续");
        } else{
            //继续播放
            mp.start();
            bt.setText( "暂停");
        }
    }

    public void stop(View v){
        mp.stop();
    }

    public void replay(View v){
        mp.stop();
        prepareAsync();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp.release(); //释放资源
        mp = null;
    }
}
c、mp4播放
public class MainActivity extends Activity {

    private SurfaceView surfaceView;
    private MediaPlayer mp ;
    private EditText et_path ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        //找到视频播放的控件
        surfaceView = (SurfaceView) findViewById(R.id.sv);
        //进行初始化
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//把视频直接显示出来
        //保证手机的屏幕一直高亮(任何的控件都可以)
        surfaceView.setKeepScreenOn(true);
        mp = new MediaPlayer();
    }

    public void play(View v){
        String path = et_path.getText().toString();
        if(TextUtils. isEmpty(path)){
            Toast. makeText(this, "播放文件的路径不能为空" , Toast.LENGTH_SHORT ).show();
            return;
        }
        mp.reset();
        try {
            mp.setDataSource(path);
            prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void prepareAsync() {
        //把媒体播放器和SurfaceView关联起来
        mp.setDisplay( surfaceView.getHolder());//设置显示
        mp.prepareAsync();
        mp.seekTo(0);
        mp.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    public void pausemove(View v){
        Button bt = (Button)v;
        if(mp .isPlaying()){
            mp.pause();
            bt.setText( "继续");
        } else{
            mp.start();
            bt.setText( "暂停");
        }
    }

    public void stop(View v){
        mp.stop();
    }
    public void replay(View v){
        mp.stop();
        prepareAsync();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp.release();
        mp = null;
    }
}
d、拍照
public class MainActivity extends Activity {

    private SurfaceView surfaceView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. activity_main);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceView.setKeepScreenOn(true);// 保持屏幕高亮
        // 获取SurfaceHolder
        SurfaceHolder holder = surfaceView.getHolder();
        // 让数据直接输出
        holder. setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // 给SurfaceView 添加创建监听
        holder.addCallback( new MyCallBack());

    }

    private Camera camera ;

    private class MyCallBack implements Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            /**
             * 步骤: 1 打开相机 2 设置预览显示 3 开始预览
             */
            try {
                camera = Camera.open();
                // 2 设置预览显示
                camera.setPreviewDisplay(holder);
                // 3 开始预览
                camera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {}

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            camera.release();// 释放硬件资源
            camera = null ;
        }
    }

    public void focus(View v) {
        // 对焦
        camera.autoFocus(null);// 自动对焦都是硬件来完成
    }

    public void takepictrue(View v) {
        // 拍照
        // shutter 快门, raw 相机捕获的原始数据, jpeg 相机处理后的数据 其实这三个参数都是接口
        camera.takePicture(null, null, new MyPictureCallback());
    }

    // 相机处理完成照片后的监听类
    private class MyPictureCallback implements PictureCallback {

        // 处理完成照片后 自动调用该方法 相机只能同时干一件事件 在处理照片的时候 相机是被占用的
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                // 把相机的数据保存在 sdcard 权限
                File file = new File(Environment.getExternalStorageDirectory(),
                        "sz08.jpg");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data);
                fos.close();

                // 重新预览
                camera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}




你可能感兴趣的:(Android)