Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional Android 2 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 18th, 2010, 07:29 AM
Registered User
 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Previewing VideoRecording Not working Listings 11-9 to 11-10

Hi I'm trying to get the Videorecording/previewing to work but i keep getting errors.

heres what i've done:

manifest.xml
Code:
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.RECORD_VIDEO"></uses-permission>
camera_preview.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"> 
 
<SurfaceView 
android:id="@+id/SurfaceView01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
</SurfaceView>
</RelativeLayout>
VideoCamera.java
Code:
package se.rijad.android.nikita.ui;

import java.io.IOException;

import se.rijad.android.nikita.R;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class VideoCamera extends Activity implements SurfaceHolder.Callback
{
	private MediaRecorder mediaRecorder;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.video_camera);
	
        SurfaceView surface = (SurfaceView)findViewById(R.id.SurfaceView01);
        SurfaceHolder holder = surface.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.setFixedSize(400,300);
    }
    
	public void surfaceCreated(SurfaceHolder holder) {
		if (mediaRecorder == null){
			try {
				mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
				
				//PREF THIS!
				//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
				mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
				//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
				
				mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
				mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
				
				mediaRecorder.setOutputFile("/sdcard/nikita_video_output.mp4");
				
				mediaRecorder.setPreviewDisplay(holder.getSurface());
				mediaRecorder.prepare();
			} catch (IllegalArgumentException e) {
				Log.d("NIKITA{MEDIA_PLAYER}", e.getMessage());
			} catch (IllegalStateException e) {
				Log.d("NIKITA{MEDIA_PLAYER}", e.getMessage());
			} catch (IOException e) {
				Log.d("NIKITA{MEDIA_PLAYER}", e.getMessage());
			}
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		mediaRecorder.release();
	}
	
}
Logcat
Code:
06-18 14:11:46.495: ERROR/AndroidRuntime(3458): Uncaught handler: thread main exiting due to uncaught exception
06-18 14:11:46.505: ERROR/AndroidRuntime(3458): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.rijad.android.nikita/se.rijad.android.nikita.ui.VideoCamera}: java.lang.NullPointerException
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.os.Looper.loop(Looper.java:123)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread.main(ActivityThread.java:4595)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at java.lang.reflect.Method.invoke(Method.java:521)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at dalvik.system.NativeStart.main(Native Method)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458): Caused by: java.lang.NullPointerException
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at se.rijad.android.nikita.ui.VideoCamera.onCreate(VideoCamera.java:24)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
06-18 14:11:46.505: ERROR/AndroidRuntime(3458):     ... 11 more
VideoCamera.jva:24
Code:
SurfaceView surface = (SurfaceView)findViewById(R.id.surface);
	    SurfaceHolder holder = surface.getHolder();
24:	    holder.addCallback(this);  //Null? :(
	    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	    holder.setFixedSize(400, 300);
please help.

Last edited by rinkle; June 18th, 2010 at 08:18 AM.
Reply With Quote
  #2 (permalink)  
Old June 21st, 2010, 01:03 PM
Reto's Avatar
Wrox Author
Points: 287, Level: 5
Points: 287, Level: 5 Points: 287, Level: 5 Points: 287, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2008
Location: London, United Kingdom
Posts: 61
Thanks: 1
Thanked 7 Times in 6 Posts
Default

From a quick look, it appears I've missed a line of code. The mediaRecorder variable is never being initialized.

Try adding:
Code:
mediaRecorder = new MediaRecorder();
Immediately before modifying the mediaRecorder parameters within the surfaceCreated method.
__________________
Reto Meier

Author, "Professional Android 4 Application Development", Wrox, ©2012, 2010, 2008
@retomeier
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Playing Video using listings 11-4 through 11-6 emanuel BOOK: Professional Android 2 Application Development 3 April 7th, 2010 02:16 PM
Chapter 11 icculus1 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 July 30th, 2009 05:15 PM
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 05:11 AM
Chapter 11: kiley-s BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 10 March 8th, 2007 07:19 PM



All times are GMT -4. The time now is 04:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.