我正在尝试将CamCoder集成为有点像instagram,但作为首发,我正在尝试使用Commonsguy lib中的以下代码进行记录器预览
MainActivity如下
@SuppressLint("SimpleDateFormat")
public class RecorderActivity extends BaseActivity implements CamCoderView.Contract
{
//private CamCoderView ffc = null;
//private CamCoderView std = null;
private CamCoderView camcoder = null;
private boolean hasTwoCameras = (Camera.getNumberOfCameras() > 1);
private boolean singleShot = true;
private ImageButton captureBtn;
private ImageButton rotateBtn;
private ImageButton doneBtn;
private int cameraType = Camera.CameraInfo.CAMERA_FACING_BACK;
private boolean isCapturePressed = false;
private ArrayList<String> pathNames = new ArrayList<String>();
private String randomPathName;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_record_layout);
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
camcoder = CamCoderView.newInstance(false);
getFragmentManager().beginTransaction()
.replace(R.id.container, camcoder ).commit();
randomPathName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
captureBtn = (ImageButton) findViewById(R.id.captureBtn);
doneBtn = (ImageButton) findViewById(R.id.doneBtn);
rotateBtn = (ImageButton) findViewById(R.id.rotateBtn);
if (hasTwoCameras)
rotateBtn.setEnabled(true);
rotateBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (cameraType == Camera.CameraInfo.CAMERA_FACING_BACK)
{
cameraType = Camera.CameraInfo.CAMERA_FACING_FRONT;
camcoder = CamCoderView.newInstance(true);
getFragmentManager().beginTransaction()
.replace(R.id.container, camcoder ).commit();
}
else
{
cameraType = Camera.CameraInfo.CAMERA_FACING_BACK;
camcoder = CamCoderView.newInstance(false);
getFragmentManager().beginTransaction()
.replace(R.id.container, camcoder ).commit();
}
}
});
doneBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
captureBtn.setOnTouchListener(new OnTouchListener()
{
@SuppressLint("SdCardPath")
@Override
public boolean onTouch(View arg0, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(isCapturePressed == false)
{
isCapturePressed = true;
String path = getOutputMediaFile().getAbsolutePath();
pathNames.add(path);
camcoder.startRecording(path);
}
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
isCapturePressed = false;
try {
camcoder.stopRecording();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
});
}
@SuppressLint("DefaultLocale")
@Override
public void onBackPressed()
{
super.onBackPressed();
if (pathNames != null)
{
for (int i = 0 ; i < pathNames.size(); i++)
{
File file = new File(pathNames.get(i));
if(file.exists())
{
file.delete();
}
}
}
finish();
}
@SuppressLint("SimpleDateFormat")
private File getOutputMediaFile()
{
File filesDir = getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if(!filesDir.isDirectory())
{
if (!filesDir.mkdirs())
{
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
File mediaFile;
mediaFile = new File(filesDir.getPath() + File.separator
+ "vid_" + randomPathName + "_" + String.valueOf(pathNames.size()) +".mp4");
return mediaFile;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public boolean isSingleShotMode()
{
return(singleShot);
}
@Override
public void setSingleShotMode(boolean mode)
{
singleShot = mode;
}
@Override
public void callUIMethodForStore(Intent intent) {
}
}
CameraView如下
public class CamCoderView extends CameraFragment
{
private FrameLayout preview;
private static final String KEY_USE_FFC = "USE_FFC";
static CamCoderView newInstance(boolean useFFC)
{
CamCoderView f = new CamCoderView();
Bundle args = new Bundle();
args.putBoolean(KEY_USE_FFC, useFFC);
f.setArguments(args);
return(f);
}
@Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setHost(new MyCameraHost(getActivity()));
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View cameraView = super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment, container, false);
preview = (FrameLayout)view.findViewById(R.id.camera);
preview.addView(cameraView);
if(isAutoFocusAvailable())
{
autoFocus();
}
return view;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
public class MyCameraHost extends SimpleCameraHost
{
boolean supportsFaces=false;
public MyCameraHost(Context _ctxt) {
super(_ctxt);
}
@Override
public boolean useFrontFacingCamera() {
return(getArguments().getBoolean(KEY_USE_FFC));
}
@Override
public boolean useSingleShotMode() {
return false;
}
@Override
public void saveImage(byte[] image)
{
}
@Override
public void autoFocusAvailable() {
}
@Override
public void autoFocusUnavailable() {
}
@Override
public void onCameraFail(CameraHost.FailureReason reason) {
super.onCameraFail(reason);
Toast.makeText(getActivity(),
"Sorry, but you cannot use the camera now!",
Toast.LENGTH_LONG).show();
}
@Override
public Parameters adjustPreviewParameters(Parameters parameters)
{
return(super.adjustPreviewParameters(parameters));
}
}
Contract getContract()
{
return((Contract)getActivity());
}
interface Contract
{
boolean isSingleShotMode();
void setSingleShotMode(boolean mode);
}
public void startRecording(String path)
{
try {
record();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在我的HTC SENSE上,它的工作方式就像一个魅力,但我的s3根本不起作用。
答案 0 :(得分:0)
好的,问题已解决,似乎是
<style name="AppBaseTheme" parent="android:style/Theme.Light.NoTitleBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- <item name="android:background">@drawable/main_bg</item> -->
<!-- <item name="android:windowBackground">@drawable/main_bg</item> -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
</style>
应用程序上的设置主题导致此问题,我删除了这个,现在它的工作正常!奇怪