冠亚体育手机网站android开发之调用手提式有线电话机的摄像头使用MediaRecorder录制并播放

我们娱乐手提式有线话机的拍录作用吗。做个DEMO。
拜望摄像进度

package com.yconme.callphone.Activity;

import android.media.MediaRecorder;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import com.yconme.callphone.Utils.ToastUtils;

import java.io.File;

import java.io.IOException;

import static android.R.attr.name;

/**

* Created by saksamaa on 2017/6/14.

*/

public class PhoneThead extends Handler {

private MediaRecorder mRecorder = null;

private String dir;

private String s;

@Override

public void handleMessage(Message msg) {

//        mRecorder = new MediaRecorder();

//        Recorder recorder = new Recorder();

switch (msg.what) {

//初阶录音

case 1:

if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{

String sdpath = Environment.getExternalStorageDirectory() + “/”;

dir = sdpath + “Luyin”;

File file = new File(dir);

if (!file.exists()) {

file.mkdir();

//                        fileName = new File(dir, “创建.MP3”);

} else {

}

}

//设置sdcard的路径

File FileName = Environment.getExternalStorageDirectory();

s = FileName.getPath() + “/Luyin”;

s += “/audiorecordtest3.MP3”;

if (mRecorder == null) {

mRecorder = new MediaRecorder();

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mRecorder.setOutputFile(s);

try {

mRecorder.prepare();

mRecorder.start();

} catch (IllegalStateException e) {

System.out.print(e.getMessage());

} catch (IOException e) {

System.out.print(e.getMessage());

}

} else {

mRecorder = new MediaRecorder();

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mRecorder.setOutputFile(s);

try {

mRecorder.prepare();

} catch (IllegalStateException | IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

mRecorder.start();

}

//                   
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

//               
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

//                mRecorder.setOutputFile(s);

//               
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

//                try {

//                    mRecorder.prepare();

//                } catch (IOException e) {

//                    Log.e(“TAG”, “prepare() failed”);

//                }

//                mRecorder.start();

break;

case 2:

Log.e(“TAG”,
“handleMessagehandleMessagehandleMessagehandleMessagehandleMessage: “);

//消灭办法在这里间

if (mRecorder != null) {

try {

mRecorder.stop();

mRecorder.release();

} catch (IllegalStateException e) {

e.printStackTrace();

}

}

break;

}

}

}

复制代码 代码如下:

mediarecorder = new MediaRecorder();// 创建mediarecorder对象
// 设置录像摄像源为Camera(相机卡塔尔(قطر‎
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置摄像完结后录像的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置录像的录制编码h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// 设置录制摄像的分辨率。必得放在设置编码和格式的背后,不然报错
mediarecorder.setVideoSize(176, 144);
// 设置摄像的录像帧率。必得放在设置编码和格式的前面,不然报错
mediarecorder.setVideoFrameRate(20);
mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
// 设置录制文件输出的路线
lastFileName = newFileName();
mediarecorder.setOutputFile(lastFileName);
try {
// 构思录制
mediarecorder.prepare();
// 开端摄像
mediarecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
得了拍戏:
if (mediarecorder != null) {
// 停止
mediarecorder.stop();
mediarecorder.release();
mediarecorder = null;
}

贴出俺自身的封装类

复制代码 代码如下:

package zyf.demo.moviedemo;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.media.MediaRecorder;
import android.view.SurfaceView;
public class MovieRecorder {
private MediaRecorder mediarecorder;
boolean isRecording;
public void startRecording(SurfaceView surfaceView) {
mediarecorder = new MediaRecorder();// 创建mediarecorder对象
// 设置录像录制源为Camera(相机卡塔尔
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置录像实现后录像的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置录制的摄像编码h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// 设置录制录像的分辨率。必需放在设置编码和格式的前边,不然报错
mediarecorder.setVideoSize(176, 144);
// 设置摄像的录像帧率。必需放在设置编码和格式的前边,不然报错
mediarecorder.setVideoFrameRate(20);
mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
// 设置录像文件输出的路径
lastFileName = newFileName();
mediarecorder.setOutputFile(lastFileName);
try {
// 计划摄像
mediarecorder.prepare();
// 此前录像
mediarecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isRecording = true;
timeSize = 0;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
timeSize++;
}
}, 0,1000);
}
Timer timer;
int timeSize = 0;
private String lastFileName;
public void stopRecording() {
if (mediarecorder != null) {
// 停止
mediarecorder.stop();
mediarecorder.release();
mediarecorder = null;
timer.cancel();
if (null != lastFileName && !””.equals(lastFileName)) {
File f = new File(lastFileName);
String name = f.getName().substring(0,
f.getName().lastIndexOf(“.3gp”));
name += “_” + timeSize + “s.3gp”;
String newPath = f.getParentFile().getAbsolutePath() + “/”

  • name;
    if (f.renameTo(new File(newPath))) {
    int i = 0;
    i++;
    }
    }
    }
    }
    public String newFileName() {
    try {
    return File.createTempFile(“/mov_”, “.3gp”).getAbsolutePath();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }
    }
    public void release() {
    if (mediarecorder != null) {
    // 停止
    mediarecorder.stop();
    mediarecorder.release();
    mediarecorder = null;
    }
    } }

那正是说与此同不常间 大家还恐怕会播放它:

复制代码 代码如下:

public void play(String fileName, SurfaceView view) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDisplay(view.getHolder(卡塔尔(قطر‎卡塔尔(قطر‎; // 定义二个SurfaceView播放它
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
stop();
// canvas.drawColor(Color.TRANSPARENT,
// PorterDuff.Mode.CLEAR);
}
});
try {
mPlayer.setDataSource(fileName);
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer.start();
}
终止播放时:
public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

提供示范代码下载

感谢那三个共享手艺经验的人。

你大概感兴趣的篇章:

  • Android实现调用摄像头拍照与录制效果
  • Android完成录像头拍照效果
  • Android调用摄像头拍照开拓教程
  • Android实现调用录制头进行摄影效率
  • Android
    开垦随手笔记之使用录制头拍照
  • Android开采教程之调用录制头成效的诀要详细解释
  • Android中决断是不是有停放摄像头、前置录制头的艺术
  • Android判定客商是还是不是允许了录制头权限实例代码
  • Android完毕调用摄像头
  • Android调用系统摄像头拍照并体现在ImageView上

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注