安卓 多媒体之视频,拍照

简单的几种播视频方式:
1、自定义SeekBar+Surfaceview ,其代码如下:

xml:


<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.an_playvidio.MainActivity">

    <SurfaceView  android:layout_width="wrap_content" android:layout_height="100dp" android:id="@+id/surfaceview" />

    <SeekBar  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/seekBar" android:layout_weight="1" />

    <ImageButton  android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_media_play" android:onClick="isplayorpause" />

LinearLayout>

activity:

package com.example.an_playvidio;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;


public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceview;

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

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



//根据音乐的进度

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //获取拖动后的进度

                int progress = seekBar.getProgress();
                mediaPlayer.seekTo(progress);
                Log.i("test", "调用了吗");

            }
        });

    }

    public  void isplayorpause(View view) throws IOException {

        final ImageButton imageButton= (ImageButton) view;

        //判断是否在播放
        if (mediaPlayer==null){
            //实例化 播放应用内的音频文件
            // mediaPlayer = MediaPlayer.create(this, R.raw.tf);

            //实例化
           mediaPlayer=new MediaPlayer();

            //播放内存卡的音频文件
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            //设置音源
            mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Movies/advideo.mp4"));

            //准备
            mediaPlayer.prepare();

            //将播发器捕捉的画面展示到surfaceview
            mediaPlayer.setDisplay(surfaceview.getHolder());

            //开始
            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
            //获取音乐的时长
            int   duration=mediaPlayer.getDuration();
            new mythread().start();


            //设置进度条的最大值与音乐时长相等
            seekBar.setMax(duration);

        }else if (mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }else {
            //继续开始 从暂停的位置开始
            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }
    }
    class  mythread extends Thread{
        @Override
        public void run() {
            super.run();
            //拿到进度条的当前进度
            while (seekBar.getProgress()<=seekBar.getMax()){
                //获取音乐播放的当前位置
                int   currentPosition=mediaPlayer.getCurrentPosition();
                seekBar.setProgress( currentPosition);

            }
        }
    }


    }

由于是读取内存卡的资源,需配置权限:


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

第二种:Videoview

xhtml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_palyvideo2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.an_playvidio.palyvideo2">

    <VideoView  android:layout_width="wrap_content" android:layout_height="200dp" android:id="@+id/v_video" />
LinearLayout>

activity:

package com.example.an_playvidio;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class palyvideo2 extends AppCompatActivity {

    private VideoView v_video;

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

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

   //设置视频来源
        v_video.setVideoPath("file://mnt/sdcard/Movies/advideo.mp4");


//实例化媒体控制器(安卓自带界面)
        MediaController mediaController=new MediaController(this);
        //互设
        mediaController.setMediaPlayer(v_video);
        v_video.setMediaController(mediaController);




    }
}

拍照:

xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_takephoto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.an_playvidio.takephoto">

<SurfaceView  android:layout_width="wrap_content" android:layout_height="200dp" android:id="@+id/s_SurfaceView" />
<Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="takephoto" android:text="拍照" />




LinearLayout>

activity:

package com.example.an_playvidio;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class takephoto extends AppCompatActivity {

    private SurfaceView s_surfaceView;
    private Camera camera;
    private Camera.Parameters parameters;

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

        s_surfaceView = (SurfaceView) findViewById(R.id.s_SurfaceView);

        //回调函数
        s_surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //打开
                camera = Camera.open();

                //设置参数
                parameters = camera.getParameters();
                parameters.setPictureFormat(PixelFormat.JPEG);
                parameters.set("jpeg-quality",85);
                camera.setParameters(parameters);

                //将画面展示到SurfaceView
                try {
                    camera.setPreviewDisplay(s_surfaceView .getHolder());

                    //开启预览
                    camera.startPreview();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

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


            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                //关闭
                //关闭照相机
                if(camera!=null){
                    camera.release();
                    camera.stopPreview();
                    camera=null;
                }
            }
        });

    }


    public void takephoto(View view){
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes, Camera camera) {

                // 压缩(图片)将字节数组转成Bitmap
                Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                try {
                    //实例化文件输出流
                    //写
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/camera/zuoye_"+System.currentTimeMillis()+".png");
                    bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);

                    //重新继续拍照
                    camera.stopPreview();
                    camera.startPreview();

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

            }
        });

}
}

最后别忘了配置权限:

    
<uses-permission android:name="android.permission.CAMERA">uses-permission>

你可能感兴趣的:(安卓)