Question

I am trying to record a video on my android project.

I have found some exemples on the internet but nothing is working. Normaly, i have to use the MediaRecorder

Can you give me a simple exemple, i don't need to visualise the video when is token, only to register it on my sdcard, that's all.

Here is an exemple of what I am trying to do:

public class VideoCapture extends Activity {  

    MediaRecorder recorder;  
    Button buttonStart = (Button)findViewById(R.id.buttonstart);  
    Button buttonStop = (Button)findViewById(R.id.buttonstop);  

    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        recorder = new MediaRecorder();  
        initRecorder();  
    }  

    private void initRecorder() {  
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);  
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);  
        recorder.setProfile(cpHigh);  
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");  
        recorder.setMaxDuration(50000);  
        recorder.setMaxFileSize(5000000);  
    }  

    private void prepareRecorder() {  
        try {  
            recorder.prepare();  
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
            finish();  
        } catch (IOException e) {  
            e.printStackTrace();  
            finish();  
        }  
    }  

    buttonStart.setOnClickListener(new Button.OnClickListener()

            {  
        @Override  
        public void onClick(View v)  
        {  
        initRecorder();  
            prepareRecorder();  
        }});  

    buttonStop.setOnClickListener(new Button.OnClickListener(){  
        @Override  
        public void onClick(View v)  
        {  
            recorder.stop();  
        }
    }); 
}

Stacktrace:

12-25 22:04:24.188: E/AndroidRuntime(1361): FATAL EXCEPTION: main
12-25 22:04:24.188: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{test.milos.net/test.milos.net.TestVideoNetActivity}: java.lang.ClassNotFoundException: test.milos.net.TestVideoNetActivity in loader dalvik.system.PathClassLoader[/data/app/test.milos.net-2.apk]
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.os.Looper.loop(Looper.java:126)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread.main(ActivityThread.java:3997)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invokeNative(Native Method)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invoke(Method.java:491)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at dalvik.system.NativeStart.main(Native Method)
12-25 22:04:24.188: E/AndroidRuntime(1361): Caused by: java.lang.ClassNotFoundException: test.milos.net.TestVideoNetActivity in loader dalvik.system.PathClassLoader[/data/app/test.milos.net-2.apk]
12-25 22:04:24.188: E/AndroidRuntime(1361):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
12-25 22:04:24.188: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-25 22:04:24.188: E/AndroidRuntime(1361):     ... 11 more
Was it helpful?

Solution

As guessed, you do a few things wrong that don't have anything to do with the actual video recording.

First problem:

Button buttonStart = (Button)findViewById(R.id.buttonstart);  
Button buttonStop = (Button)findViewById(R.id.buttonstop);  

You can't call findViewById() here. findViewById() only searches in the activities current view hierachy. At the point where your class gets instatiated, there is no view hierachy present. These class members will always be null, because that is what gets returned if the view cannot be found. You can keep the variable declaration, but move the search part into onCreate(), after you call setContentView(), for example like this:

setContentView(R.layout.main);
buttonStart = (Button)findViewById(R.id.buttonstart);  
buttonStop = (Button)findViewById(R.id.buttonstop); 

Same goes for your two setOnClickListener() calls on the bottom. They have to be inside a method. I'd suggest moving these two blocks into onCreate() as well, after the findViewById() calls.


Edit: The stacktrace indicates that your app tries to run an activity named TestVideoNetActivity. Your actual activity is named VideoCapture.

Open your AndroidManifest and look for a part that looks like this:

    <activity
        android:name=".TestVideoNetActivity"
        ... >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Change TestVideoNetActivity to VideoCapture. Also make sure that your packages match, there should be a line package="X" at the top of your manifest and a line package X; at the top of your java source file. Make sure these have the same value for X.


Edit 2: Your second stacktrace indicates that setAudioSource() failed. All I can think of is that you don't have the correct permissions set to access the microphone. Add the following lines to your android-manifest (this includes the camera permission too, since you need that as well):

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />

Add them inside the <manifest> tag, but outside of the <application> tag. E.g. after the <uses-sdk> line, if you got one (is included by default I believe).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top