Java Android:同时录制视频和播放mp3?
当摄像机在安卓4.x上录制视频(没有声音)时,有没有办法播放mp3(或其他)音频文件?Java Android:同时录制视频和播放mp3?,java,android,audio,video,android-camera,Java,Android,Audio,Video,Android Camera,当摄像机在安卓4.x上录制视频(没有声音)时,有没有办法播放mp3(或其他)音频文件? 有人试过实现这一点吗?我试过在录制声音的同时播放声音,而且效果很好,所以我很确定在录制视频时也能做到这一点。你只需要有单独的视频录制和声音播放线程 package com.technologies.mash.MySounds.Music; package com.technologies.mash.MySounds.Music; import android.app.Activity; import andr
有人试过实现这一点吗?我试过在录制声音的同时播放声音,而且效果很好,所以我很确定在录制视频时也能做到这一点。你只需要有单独的视频录制和声音播放线程
package com.technologies.mash.MySounds.Music;
package com.technologies.mash.MySounds.Music;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.CamcorderProfile;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.technologies.mash.CameraPreview;
import com.technologies.mash.PlayVideo;
import com.technologies.mash.R;
import com.technologies.mash.Sound;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class AlbumDetailVideo extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mediaRecorder;
private Button capture, switchCamera;
private Context myContext;
private RelativeLayout cameraPreview;
AudioManager audioManager;
private boolean cameraFront = false;
// private ArrayList<Sound> mSounds = null;
int z = 0;
Sound s1;
MediaPlayer mPlayer;
int sound;
String urls;
String audio, root, video, video_new, output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_album_detail_video);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// getting songpath from server
Intent i = getIntent();
urls = i.getStringExtra("songpath");
// String urls = getIntent().getExtras().getString("songpath");
Toast.makeText(AlbumDetailVideo.this, "Sound id" + urls, Toast.LENGTH_SHORT).show();
Log.e("url", urls);
// mSounds = new ArrayList<Sound>();
myContext = this;
initialize();
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
cameraFront = true;
break;
}
}
return cameraId;
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(z);//getting the .txt file
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
private int findBackFacingCamera() {
int cameraId = -1;
// Search for the back facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
cameraFront = false;
break;
}
}
return cameraId;
}
public void onResume() {
super.onResume();
if (!hasCamera(myContext)) {
Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
toast.show();
finish();
}
if (mCamera == null) {
// if the front facing camera does not exist
if (findFrontFacingCamera() < 0) {
Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
switchCamera.setVisibility(View.GONE);
}
mCamera = Camera.open(findBackFacingCamera());
mCamera.setDisplayOrientation(90);
mPreview.refreshCamera(mCamera);
}
}
public void initialize() {
cameraPreview = (RelativeLayout) findViewById(R.id.camera_preview);
mPreview = new CameraPreview(myContext, mCamera);
cameraPreview.addView(mPreview);
capture = (Button) findViewById(R.id.button_capture);
capture.setOnClickListener(captrureListener);
switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
switchCamera.setOnClickListener(switchCameraListener);
}
View.OnClickListener switchCameraListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// get the number of cameras
if (!recording) {
int camerasNumber = Camera.getNumberOfCameras();
if (camerasNumber > 1) {
// release the old camera instance
// switch camera, from the front and the back and vice versa
releaseCamera();
chooseCamera();
} else {
Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
toast.show();
}
}
}
};
public void chooseCamera() {
// if the camera preview is the front
if (cameraFront) {
int cameraId = findBackFacingCamera();
if (cameraId >= 0) {
// open the backFacingCamera
// set a picture callback
// refresh the preview
mCamera = Camera.open(cameraId);
mCamera.setDisplayOrientation(90);
// mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
}
} else {
int cameraId = findFrontFacingCamera();
if (cameraId >= 0) {
// open the backFacingCamera
// set a picture callback
// refresh the preview
mCamera = Camera.open(cameraId);
mCamera.setDisplayOrientation(90);
// mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
}
}
}
@Override
protected void onPause() {
super.onPause();
// when on Pause, release camera in order to be used from other
// applications
releaseCamera();
}
private boolean hasCamera(Context context) {
// check if the device has camera
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}
boolean recording = false;
View.OnClickListener captrureListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AlbumDetailVideo.this, "Playing.." + urls, Toast.LENGTH_SHORT).show();
if (recording) {
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mPlayer.stop();
Intent intent = new Intent(getApplicationContext(), PlayVideo.class);
startActivity(intent);
finish();
Toast.makeText(AlbumDetailVideo.this, "Video captured!", Toast.LENGTH_LONG).show();
recording = false;
} else {
if (!prepareMediaRecorder()) {
Toast.makeText(AlbumDetailVideo.this, "Fail in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
finish();
}
// work on UiThread for better performance
runOnUiThread(new Runnable() {
public void run() {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(urls);
} catch (IllegalArgumentException e) {
Toast.makeText(AlbumDetailVideo.this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(AlbumDetailVideo.this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(AlbumDetailVideo.this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(AlbumDetailVideo.this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(AlbumDetailVideo.this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mediaRecorder.start();
mPlayer.start();
}
});
recording = true;
}
}
};
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
// CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
// mediaRecorder.setAudioSource(MediaRecorder.AudioEncoder.AAC_ELD);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
/* mediaRecorder.setOutputFormat(profile.fileFormat);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);*/
mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
} catch (Exception e) {
}
mediaRecorder.setOutputFile("/sdcard/rohit.mp4");
mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
/* private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
//mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
try {
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
}catch (Exception e){
}
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}*/
private void releaseCamera() {
// stop and release camera
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.hardware.Camera;
导入android.media.AudioManager;
导入android.media.CamcorderProfile;
导入android.media.MediaPlayer;
导入android.media.MediaRecorder;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.WindowManager;
导入android.widget.Button;
导入android.widget.RelativeLayout;
导入android.widget.Toast;
导入com.technologies.mash.CameraPreview;
导入com.technologies.mash.PlayVideo;
导入com.technologies.mash.R;
导入com.technologies.mash.Sound;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
公共类AlbumDetailVideo扩展活动{
私人摄像机麦卡梅拉;
私人摄像审查;
专用媒体记录器;
私人按钮捕捉,切换摄像头;
私人语境;
私人关系审查;
AudioManager AudioManager;
私有布尔cameraFront=false;
//private ArrayList mSounds=null;
int z=0;
声音s1;
媒体播放器mPlayer;
int声音;
字符串URL;
字符串音频,根,视频,视频新,输出;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u album\u detail\u video);
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u保持屏幕打开);
//从服务器获取歌曲路径
Intent i=getIntent();
URL=i.getStringExtra(“songpath”);
//字符串URL=getIntent().getExtras().getString(“songpath”);
Toast.makeText(AlbumDetailVideo.this,“声音id”+URL,Toast.LENGTH_SHORT.show();
Log.e(“url”,url);
//mSounds=newarraylist();
myContext=这个;
初始化();
}
专用int findFrontFacingCamera(){
int cameraId=-1;
//搜索前向摄像头
int numberOfCameras=Camera.getNumberOfCameras();
对于(int i=0;i1){
//释放旧的摄影机实例
//从前面和后面切换摄像头,反之亦然
松开摄像头();
chooseCamera();
}否则{
Toast Toast=Toast.makeText(myContext,“对不起,您的手机只有一个摄像头!”