1

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;
    }


}
Matthew
  • 3,886
  • 7
  • 47
  • 84

1 Answers1

3

VideoView is only used for playing back already-recorded video; it has nothing to do with recording videos using the Android Camera and MediaRecorder APIs.

If you want to display a preview stream from the camera, and hook the camera up to the media recorder to record video, start with this guide: Building a Camera App

Currently, you don't have a Camera instance at all, which you need to pass to your MediaRecorder. You also should use a SurfaceView for showing the camera preview, or a TextureView/GLSurfaceView if you're targeting > 3.0.

Once you have some video recorded, you could play it back with a VideoView.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • in my experience you cannot use a textureview or surfacetexture with mediarecorder to record video, even if you use setCamera() after setting up the texture(as of ICS) – ThumbsDP Jan 29 '13 at 02:51