Android多媒体——(MidiaRecorder音频录制/视频播放surface/Soundpool提示音/调用系统摄像头camera)

MidiaRecorder音频录制

工作流程图

这里写图片描述

权限

这里写图片描述

写一个小例子来具体说下

layout代码这里就不写了,只放两个按钮,一个开始录音,一个结束录音

MainActivity

import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtStartRecorder;
    private Button mBtStopRecorder;
    private MediaRecorder mediaRecorder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtStartRecorder= (Button) findViewById(R.id.button_start_recorder);
        mBtStartRecorder.setOnClickListener(this);
        mBtStopRecorder= (Button) findViewById(R.id.button_stop_recorder);
        mBtStopRecorder.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_start_recorder:
                mediaRecorder=new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频来源
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置外放格式
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/myrecord.3gp");
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.button_stop_recorder:
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder.release();
                break;
        }
    }
}

视频播放

权限

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

VideoView

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_start_playvideo"
        android:text="开始播放"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <VideoView
        android:id="@+id/videoview"
        android:layout_width="wrap_content"
        android:layout_height="400dp" />
</LinearLayout>

主函数MyPlayvideo

package com.example.administrator.mediarecorder.mediaplayvideo;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import com.example.administrator.mediarecorder.R;

/**
 * Created by Administrator on 2015/9/15.
 */
public class MyPlayvideo extends AppCompatActivity {
    private Button mBtPlayvideo;
    private VideoView mVideoview;
     //VideoView 是android 系统提供的一个媒体播放显示和控制的控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myplayvideo);
        mBtPlayvideo= (Button) findViewById(R.id.button_start_playvideo);
        mVideoview= (VideoView) findViewById(R.id.videoview);
        mBtPlayvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //找到视频地址,还可以通过setVideoUri来找到    mVideoview.setVideoPath(Environment.getExternalStorageDirectory()+"/bb.MP4");
                //设置播放位置
                mVideoview.setMediaController(new MediaController(MyPlayvideo.this));
                mVideoview.start();
            }
        });
    }
}

SurfaceView

同样的我们在布局中加入一个按钮和一个SurfaceView,点击按钮,播放视频!用法和前边将播放音频差不多。

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button_start_playersurface"
        android:text="播放视频"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MyPlayersurface

package com.example.administrator.mediarecorder.Playersurface;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.example.administrator.mediarecorder.R;

import java.io.IOException;

/**
 * Created by Administrator on 2015/9/15.
 */
public class MyPlayersurface extends AppCompatActivity{
    private Button mBtStartSurface;
    private SurfaceView mSurfaceView;
    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myplayersurface);
        mSurfaceView= (SurfaceView) findViewById(R.id.surfaceView);
        mBtStartSurface= (Button) findViewById(R.id.button_start_playersurface);
        mBtStartSurface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(player==null){
                    player=new MediaPlayer();
                }
                player.reset();
                try {
                    player.setDataSource(Environment.getExternalStorageDirectory()+"/myvideoview.mp4");
                    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    player.setDisplay(mSurfaceView.getHolder());
                    player.prepare();
                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Soundpool提示音

在Android中播放音频文件一般都使用MediaPlayer,但是一般都是比较长的音频文件,我们可以用soundpool可以播一些短的反应速度要求高的声音,比如通知提示音,消息提示音等。

  1. SoundPool载入音乐文件使用了独立的线程,不会阻塞UI主线程的操作。但是这里如果音效文件过大没有载入完成,我们调用play方法时可能产生严重的后果,这里Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解媒体文件是否载入完成,我们重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可获得。
  2. 从上面的onLoadComplete方法可以看出该类有很多参数,比如类似id,是的SoundPool在load时可以处理多个媒体一次初始化并放入内存中,这里效率比MediaPlayer高了很多。
  3. SoundPool类支持同时播放多个音效,这对于游戏来说是十分必要的,而MediaPlayer类是同步执行的只能一个文件一个文件的播放。

MainActivity

package com.example.administrator.soundpool;

import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mBtSoundpool;
    private int voiceID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtSoundpool= (Button) findViewById(R.id.button_soundpool);
        mBtSoundpool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSound();
            }
        });
        voiceID=initSoundPool();
    }

    private SoundPool pool=null;
    public void playSound(){
        pool.play(voiceID,1,1,0,-1,-1);
    }
   private int initSoundPool(){
      if(Build.VERSION.SDK_INT>=21){
          SoundPool.Builder builder=new SoundPool.Builder();
          builder.setMaxStreams(2);
          AudioAttributes.Builder atterBuilder=new AudioAttributes.Builder();
          atterBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
          builder.setAudioAttributes(atterBuilder.build());
          pool=builder.build();
      }else {
          pool=new SoundPool(2,AudioManager.STREAM_MUSIC,0);
      }
       return pool.load(getApplicationContext(),R.raw.msg,1);
   }

}

调用系统摄像头/从相册中选取照片

权限

这里写图片描述

例子

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
    android:id="@+id/button_start_camera"
    android:text="调用摄像头"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button_opean_gallery"
        android:text="打开相册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

MyCameraActivity

package com.example.administrator.mediarecorder.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.administrator.mediarecorder.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

/**
 * Created by Administrator on 2015/9/15.
 */
public class MyCameraActivity extends AppCompatActivity {
    private Button mBtStartCamera;
    private ImageView mImageView;
    private File file;
    private static final int GET_PIC_FORM_CAMERA = 0X23;
    private Button mBtOpeanGallery;
    private static final int GET_PIC_FROM_GALLERY = 0X24;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycamera);
        mImageView = (ImageView) findViewById(R.id.imageview);
        mBtStartCamera = (Button) findViewById(R.id.button_start_camera);
        mBtStartCamera.setOnClickListener(new View.OnClickListener() {
            @Override//点击按钮,打开摄像头
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
                /*
                创建file将拍摄的照片存储
                 */
                //设置创建的文件路径,和名称(名称是当前时间,保证不会冲突)
                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//告诉系统相机将照片保存的位置
                startActivityForResult(intent, GET_PIC_FORM_CAMERA);//开始启动
            }
        });
        //从相册得到照片
        mBtOpeanGallery = (Button) findViewById(R.id.button_opean_gallery);
        mBtOpeanGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, GET_PIC_FROM_GALLERY);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //得到刚拍的照片
//        if (requestCode==GET_PIC_FORM_CAMERA){
//            if(resultCode==RESULT_OK){
//                ImageZip.zipImage(file.getAbsolutePath());//压缩图片
//                mImageView.setImageURI(Uri.fromFile(file));//得到图片
//            }
//
//        }
        //从相册选取照片
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case GET_PIC_FORM_CAMERA:
                    ImageZip.zipImage(file.getAbsolutePath());//压缩图片
                    mImageView.setImageURI(Uri.fromFile(file));//得到图片
                    break;
                case GET_PIC_FROM_GALLERY:
                    Uri uri=data.getData();
                    mImageView.setImageURI(uri);
                    break;
            }
        }
    }
}

压缩图片类ImageZip

package com.example.administrator.mediarecorder.camera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

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

/**
 * Created by Administrator on 2015/9/16.
 */
public class ImageZip {
    /*
   * 压缩图片
   * */
    public static void zipImage(String savePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(savePath, options);
        options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
        try {
            FileOutputStream fos = new FileOutputStream(savePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmap.recycle();
        bitmap = null;
        System.gc();
    }
    public static int computeSampleSize(BitmapFactory.Options options,
                                 int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,
                                         int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值