I am having difficulty displaying the camera feed in an Activity that uses the VideoView view. I am simply trying to view my camera (later of which I hope to either record or take a photo) I don't want to use an intent, and I want the actual view to be in a box 50dp X 50dp. The code compiles and I see a blank activity with a black box that is 50X50 dps. The only problem is that it is now displaying the live feed of the camera. This is the code I have...
The Manifest file
I made sure to include the proper permissions
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Next in my layout file I create a VideoView and set the attributes. (Am I setting all the correct attributes?)
The Layout File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.myview"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/vvv"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"/>
</LinearLayout>
Finally in my activity...
My Activity File
package com.example.myview;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.*;
import android.view.TextureView.SurfaceTextureListener;
import android.widget.VideoView;
public class MainActivity extends Activity
{
private MediaRecorder mediarecorder;
private VideoView vv;
@Override
public void onCreate(Bundle savedInstanceState)
{
//Be sure to load the activity view before I try to grab the sub view widets...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediarecorder = new MediaRecorder();
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediarecorder.setOutputFile("blah.mp4");
vv = (VideoView)findViewById(R.id.vvv);
vv.setKeepScreenOn(true);
vv.start();
SurfaceHolder holder = vv.getHolder();
mediarecorder.setPreviewDisplay(holder.getSurface());
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();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}