开发者

error in video recorder android

I am developing video recorder

My code is

package com.app.UI;

import java.io.File;
import java.io.IOException;

import com.app.utils.Logger;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class main extends Activity implements SurfaceHolder.Callback{
    /** Called when the activity is first created. */
    private static final String TAG = "CAMERA_TUTORIAL";

    Button btnStart, btnStop, btnPlay;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Camera camera;
    private boolean previewRunning;

    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final long maxFileSizeInBytes = 500000;
    private final int videoFramesPerSecond = 20;

    public static String pathname = "/sdcard/recorder.amr";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startRecording();
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopRecording();
            }
        });
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        Logger.debug("surface change");
        if (previewRunning){
            camera.stopPreview();
        }
        Camera.Parameters p = camera.getParameters();
        camera.setParameters(p);

        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Logger.debug("surface created");
        camera = Camera.open();
        if (camera != null){
            Camera.Parameters params = camera.getParameters();
            camera.setParameters(params);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Logger.debug("surface destroy");
        camera.stopPreview();
        previewRunning = false;
        camera.release();
    }

    public boolean startRecording(){
        try {
            //camera.unlock();

            mediaRecorder = new MediaRecorder();

            mediaRecorder.setCamera(camera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

            mediaRecorder.setMaxDuration(maxDurationInMs);

            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File (sdCard.getAbsolutePath() + "/M开发者_JS百科y Recording");
            dir.mkdirs();
            File tempFile = new File(dir, "myvideo.mp4");

            mediaRecorder.setOutputFile(tempFile.getAbsolutePath());

            //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
            //mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

            //mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

            mediaRecorder.prepare();
            mediaRecorder.start();

            return true;
        } catch (IllegalStateException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public void stopRecording(){
        try{
            mediaRecorder.stop();
            mediaRecorder.release();
        }
        catch (IllegalStateException e) {
            // TODO: handle exception
            Logger.debug(""+e.getMessage());
        }
        //camera.lock();
    }

}

and in manifest file I have write

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.UI"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
</manifest> 

now my problem is when I click on start button it show me error like

04-25 17:47:03.817: WARN/System.err(23842): java.io.IOException: prepare failed.
04-25 17:47:03.817: WARN/System.err(23842):     at android.media.MediaRecorder._prepare(Native Method)
04-25 17:47:03.817: WARN/System.err(23842):     at android.media.MediaRecorder.prepare(MediaRecorder.java:458)
04-25 17:47:03.817: WARN/System.err(23842):     at com.app.UI.main.startRecording(main.java:143)
04-25 17:47:03.827: WARN/System.err(23842):     at com.app.UI.main$1.onClick(main.java:55)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.performClick(View.java:2344)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.onTouchEvent(View.java:4133)
04-25 17:47:03.827: WARN/System.err(23842):     at android.widget.TextView.onTouchEvent(TextView.java:6504)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.dispatchTouchEvent(View.java:3672)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
04-25 17:47:03.837: WARN/System.err(23842):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
04-25 17:47:03.847: WARN/System.err(23842):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
04-25 17:47:03.847: WARN/System.err(23842):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:47:03.847: WARN/System.err(23842):     at android.os.Looper.loop(Looper.java:123)
04-25 17:47:03.847: WARN/System.err(23842):     at android.app.ActivityThread.main(ActivityThread.java:4203)
04-25 17:47:03.847: WARN/System.err(23842):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:47:03.847: WARN/System.err(23842):     at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:47:03.847: WARN/System.err(23842):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-25 17:47:03.847: WARN/System.err(23842):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-25 17:47:03.857: WARN/System.err(23842):     at dalvik.system.NativeStart.main(Native Method)

and when I click on stop button it shows error like

04-25 17:48:03.827: ERROR/AndroidRuntime(23842): Uncaught handler: thread main exiting due to uncaught exception
04-25 17:48:03.837: ERROR/AndroidRuntime(23842): java.lang.IllegalStateException
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.media.MediaRecorder.stop(Native Method)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.app.UI.main.stopRecording(main.java:159)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.app.UI.main$2.onClick(main.java:64)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.performClick(View.java:2344)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.onTouchEvent(View.java:4133)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.widget.TextView.onTouchEvent(TextView.java:6504)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.dispatchTouchEvent(View.java:3672)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.os.Looper.loop(Looper.java:123)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.app.ActivityThread.main(ActivityThread.java:4203)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at dalvik.system.NativeStart.main(Native Method)

What I have to do, i reffered many site but my problem can not solve, please friend help me

I have test on 1.6b device and 2.2 emulater


1.<uses-permission android:name="android.permission.INTERNET" />

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

2.public int SELECT_VIDEO_FILE = 2, REQUEST_CAMERA_VIDEO = 3; 
final CharSequence[] items = {"Take Video", "Choose from Library",
            "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Add Video!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Video")) {
                userChoosenTask = "Take Video";
                if (result)
                    cameraIntentVideos();
            } else
            if (items[item].equals("Choose from Library")) {
                userChoosenTask = "Choose from Library";
                if (result)
                    galleryIntentVideos();
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

3.private void cameraIntentVideos() {
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    takeVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
    if (takeVideoIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        String fileName = "myvideo.mp4";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Video.Media.TITLE, fileName);
        videoUri = getActivity().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                values);
        takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        startActivityForResult(takeVideoIntent,REQUEST_CAMERA_VIDEO);
    }
}
4. private void galleryIntentVideos() {
    Intent intentPickVideo = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intentPickVideo, SELECT_VIDEO_FILE);
5. @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA_VIDEO) {
            onCaptureVideoResult(data);
        } else if (requestCode == SELECT_VIDEO_FILE) {
            onSelectVideoFromGalleryResult(data);
        } else {
            Toast.makeText(getActivity(), "You haven't picked Image", Toast.LENGTH_SHORT).show();
        }
    }
}
6.// TODO: 5/31/2017 capture video
public void onCaptureVideoResult(Intent data) {
    String[] projection = {MediaStore.Video.Media.DATA};
    Cursor cursor = getActivity().managedQuery(videoUri, projection, null, null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(
            MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    PicturePath = cursor.getString(column_index_data);
    String filename=PicturePath.substring(PicturePath.lastIndexOf("/")+1);
    txtFilePath.setText(filename);
}
7. // TODO: 5/31/2017 video from gallery
private void onSelectVideoFromGalleryResult(Intent data) {
    if (data != null) {
        try {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Video.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(uri,
                    filePathColumn, null, null, null);
            if (cursor == null || cursor.getCount() < 1) {
                return; // no cursor or no record. DO YOUR ERROR HANDLING
            }
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            PicturePath = cursor.getString(columnIndex);
            cursor.close(); // close cursor
            if (PicturePath != null) {
               MediaPlayer mp = MediaPlayer.create(getActivity(), Uri.parse(PicturePath));
                int duration = mp.getDuration();
                mp.release();
                if ((duration / 1000) > 30) {
                    Toast.makeText(getActivity(), "Please Select video with maximum 30 seconds limit", Toast.LENGTH_SHORT).show();
                } else {
                    //runVideo(PicturePath);
                    String filename=PicturePath.substring(PicturePath.lastIndexOf("/")+1);
                    txtFilePath.setText(filename);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(getApplicationContext(), "No Data Found", Toast.LENGTH_SHORT).show();
    }
}


Add this to manifest:<uses-permission android:name="android.permission.RECORD_VIDEO"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜