Android 框架错误问题

发布于 2024-10-28 13:02:06 字数 8244 浏览 0 评论 0原文

这个问题与多媒体框架有关,

我使用的是带有Android 2.1的索尼爱立信Xperia X8 (E15i)。

我面临与视频录制相关的严重问题。我的具体目标是实时从视频中获取帧。

当我尝试打开应用程序时,它崩溃了。

我在这里发布了我的代码。

帮我解决这个问题。

我的代码::::::::::::::::::::::::::

package com.tcs.video;

import java.io.IOException;

import dalvik.system.TemporaryDirectory;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.graphics.*;
//import android.view.Window;
import android.graphics.BitmapFactory.Options;

public class VideoRecorder extends Activity{

    //Create objects of MediaRecorder and Preview class  
    private MediaRecorder recorder;
    private Preview mPreview;

    boolean flag=false; 
    boolean startedRecording=false;
    boolean stoppedRecording=false;

    // In this method, create an object of MediaRecorder class. Create an object of 
    // RecorderPreview class(Customized View). Add RecorderPreview class object
    // as content of UI.     
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        recorder = new MediaRecorder();
        //recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        recorder.setCamera(Camera.open());
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mPreview = new Preview(VideoRecorder.this,recorder);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(mPreview);
        recorder.setPreviewDisplay(mPreview.getSurface());

   }

     /*!
    <p>
         Initialize the contents of the Activity's standard options menu. Menu items are to be placed in to menu.
         This is called on each press of menu button. In this options to start and stop recording are provided. 
         Option for start recording  has group id 0 and option to stop recording is 1.
         (first parameter of menu.add method). Start and stop have different group id, if recording is already 
         started then it shows stop option else it shows start option.
    </p>*/   
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        super.onPrepareOptionsMenu(menu);
        menu.clear(); 
        menu.add(0, 0, 0, "Start Recording"); 
        menu.add(1, 1, 0, "Stop Recording");

        menu.setGroupVisible(0, false);
        menu.setGroupVisible(1, false);

        if(startedRecording==false)
            menu.setGroupVisible(0, true);
        else if(startedRecording==true&&stoppedRecording==false)
            menu.setGroupVisible(1, true);

        return true;
    }


     /*!
        <p>
        This method receives control when Item in menu option is selected. It contains implementations
        to be performed on selection of menu item. 
        </p>*/

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) 
        {
        case 0:
             /*
             try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            */
            //start the recorder
                recorder.start();
                startedRecording=true;

            break;

        case 1: 
            //stop the recorder
            recorder.stop();
            recorder.release();
            recorder = null;
            stoppedRecording=true;
            break;


        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    class Preview extends SurfaceView implements SurfaceHolder.Callback//,PreviewCallback
    {
        //Create objects for MediaRecorder and SurfaceHolder.
        SurfaceHolder mHolder;
        MediaRecorder tempRecorder;
        Camera  mCamera;
        Bitmap currentprev;

        //Create constructor of Preview Class. In this, get an object of 
        //surfaceHolder class by calling getHolder() method. After that add   
        //callback to the surfaceHolder. The callback will inform when surface is 
        //created/changed/destroyed. Also set surface not to have its own buffers.
        public Preview(Context context,MediaRecorder recorder) {
            super(context);
            tempRecorder=recorder;
            mHolder=getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            // TODO Auto-generated constructor stub 

         Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
            {
                    public void onPreviewFrame( byte[] data, Camera Cam ) {
                            Log.d("CombineTestActivity", "Preview started");
                            Log.d("CombineTestActivity", "Data length = " + data.length );
                            //currentprev.recycle();
                           /* 
                            currentprev = BitmapFactory.decodeByteArray( data, 0, data.length );

                           if( currentprev == null )
                               Log.d("CombineTestActivity", "currentprev is null" );

                            Log.d("CombineTestActivity", "Preview Finished" );
                            */

                    }
            };

            mCamera =Camera.open();
            Camera.Parameters   camParam = mCamera.getParameters();
            camParam.setPreviewFormat(PixelFormat.YCbCr_420_SP );
            camParam.setPreviewFrameRate(2);
            mCamera.setParameters( camParam );
            mCamera.setPreviewCallback( mPrevCallback );

        }

        public Surface getSurface()
        {
            return mHolder.getSurface();
        }

        // Implement the methods of SurfaceHolder.Callback interface

        // SurfaceCreated : This method gets called when surface is created.
        // In this, initialize all parameters of MediaRecorder object.
        //The output file will be stored in SD Card.

        public void surfaceCreated(SurfaceHolder holder){
            //tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");

            tempRecorder.setOutputFile("/sdcard/recordvideooutput.mp4");

            tempRecorder.setPreviewDisplay(mHolder.getSurface());

            try{
                //mCamera.release();
                //mCamera.unlock();
                //mCamera = Camera.open();
                //Thread.sleep(1000);
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
                //Thread.sleep(1000);
                //mCamera.unlock();
                tempRecorder.setCamera(Camera.open());
                //tempRecorder.prepare();
            } catch (Exception e) {
                String message = e.getMessage().toString();
                Log.e("Surface created :Error", message);
                tempRecorder.release();
                tempRecorder = null;
            }

        }

        public void surfaceDestroyed(SurfaceHolder holder) 
        {
            if(tempRecorder!=null)
            {
                tempRecorder.release();
                tempRecorder = null;
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
        {
             Log.v("surfaceChanged method", "");
             mCamera.startPreview();

        }


        public  void onPreviewFrame(byte [] data, Camera camera)
        {
              Log.v("onpreviewframe method", "data:"+data.toString());
        }
    }   
}

This issue is related with multimedia framework,

I am using Sony Ericsson Xperia X8 (E15i) with Android 2.1.

I face a serious problem related with Video recording. My specific goal is to fetch frames from Video at real time.

While I try to open an application it crashes.

I posted my code here.

Help me for this issue.

MY CODE:::::::::::::::::::::::::

package com.tcs.video;

import java.io.IOException;

import dalvik.system.TemporaryDirectory;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.graphics.*;
//import android.view.Window;
import android.graphics.BitmapFactory.Options;

public class VideoRecorder extends Activity{

    //Create objects of MediaRecorder and Preview class  
    private MediaRecorder recorder;
    private Preview mPreview;

    boolean flag=false; 
    boolean startedRecording=false;
    boolean stoppedRecording=false;

    // In this method, create an object of MediaRecorder class. Create an object of 
    // RecorderPreview class(Customized View). Add RecorderPreview class object
    // as content of UI.     
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        recorder = new MediaRecorder();
        //recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        recorder.setCamera(Camera.open());
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mPreview = new Preview(VideoRecorder.this,recorder);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(mPreview);
        recorder.setPreviewDisplay(mPreview.getSurface());

   }

     /*!
    <p>
         Initialize the contents of the Activity's standard options menu. Menu items are to be placed in to menu.
         This is called on each press of menu button. In this options to start and stop recording are provided. 
         Option for start recording  has group id 0 and option to stop recording is 1.
         (first parameter of menu.add method). Start and stop have different group id, if recording is already 
         started then it shows stop option else it shows start option.
    </p>*/   
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        super.onPrepareOptionsMenu(menu);
        menu.clear(); 
        menu.add(0, 0, 0, "Start Recording"); 
        menu.add(1, 1, 0, "Stop Recording");

        menu.setGroupVisible(0, false);
        menu.setGroupVisible(1, false);

        if(startedRecording==false)
            menu.setGroupVisible(0, true);
        else if(startedRecording==true&&stoppedRecording==false)
            menu.setGroupVisible(1, true);

        return true;
    }


     /*!
        <p>
        This method receives control when Item in menu option is selected. It contains implementations
        to be performed on selection of menu item. 
        </p>*/

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) 
        {
        case 0:
             /*
             try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            */
            //start the recorder
                recorder.start();
                startedRecording=true;

            break;

        case 1: 
            //stop the recorder
            recorder.stop();
            recorder.release();
            recorder = null;
            stoppedRecording=true;
            break;


        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    class Preview extends SurfaceView implements SurfaceHolder.Callback//,PreviewCallback
    {
        //Create objects for MediaRecorder and SurfaceHolder.
        SurfaceHolder mHolder;
        MediaRecorder tempRecorder;
        Camera  mCamera;
        Bitmap currentprev;

        //Create constructor of Preview Class. In this, get an object of 
        //surfaceHolder class by calling getHolder() method. After that add   
        //callback to the surfaceHolder. The callback will inform when surface is 
        //created/changed/destroyed. Also set surface not to have its own buffers.
        public Preview(Context context,MediaRecorder recorder) {
            super(context);
            tempRecorder=recorder;
            mHolder=getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            // TODO Auto-generated constructor stub 

         Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
            {
                    public void onPreviewFrame( byte[] data, Camera Cam ) {
                            Log.d("CombineTestActivity", "Preview started");
                            Log.d("CombineTestActivity", "Data length = " + data.length );
                            //currentprev.recycle();
                           /* 
                            currentprev = BitmapFactory.decodeByteArray( data, 0, data.length );

                           if( currentprev == null )
                               Log.d("CombineTestActivity", "currentprev is null" );

                            Log.d("CombineTestActivity", "Preview Finished" );
                            */

                    }
            };

            mCamera =Camera.open();
            Camera.Parameters   camParam = mCamera.getParameters();
            camParam.setPreviewFormat(PixelFormat.YCbCr_420_SP );
            camParam.setPreviewFrameRate(2);
            mCamera.setParameters( camParam );
            mCamera.setPreviewCallback( mPrevCallback );

        }

        public Surface getSurface()
        {
            return mHolder.getSurface();
        }

        // Implement the methods of SurfaceHolder.Callback interface

        // SurfaceCreated : This method gets called when surface is created.
        // In this, initialize all parameters of MediaRecorder object.
        //The output file will be stored in SD Card.

        public void surfaceCreated(SurfaceHolder holder){
            //tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");

            tempRecorder.setOutputFile("/sdcard/recordvideooutput.mp4");

            tempRecorder.setPreviewDisplay(mHolder.getSurface());

            try{
                //mCamera.release();
                //mCamera.unlock();
                //mCamera = Camera.open();
                //Thread.sleep(1000);
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
                //Thread.sleep(1000);
                //mCamera.unlock();
                tempRecorder.setCamera(Camera.open());
                //tempRecorder.prepare();
            } catch (Exception e) {
                String message = e.getMessage().toString();
                Log.e("Surface created :Error", message);
                tempRecorder.release();
                tempRecorder = null;
            }

        }

        public void surfaceDestroyed(SurfaceHolder holder) 
        {
            if(tempRecorder!=null)
            {
                tempRecorder.release();
                tempRecorder = null;
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
        {
             Log.v("surfaceChanged method", "");
             mCamera.startPreview();

        }


        public  void onPreviewFrame(byte [] data, Camera camera)
        {
              Log.v("onpreviewframe method", "data:"+data.toString());
        }
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-11-04 13:02:06

似乎你忘记了这一行:

recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

或这个:

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

并且不要忘记在你的清单中添加类似的内容:

uses-permission android:name="android.permission.CAMERA" 
uses-permission android:name="android.permission.RECORD_AUDIO" 
uses-permission android:name="android.permission.RECORD_VIDEO" 
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

尝试并祈祷;)

Seems you forgot this line:

recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

or this:

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

And don't forget to add something like this in your manifest:

uses-permission android:name="android.permission.CAMERA" 
uses-permission android:name="android.permission.RECORD_AUDIO" 
uses-permission android:name="android.permission.RECORD_VIDEO" 
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

Try it and pray ;)

破晓 2024-11-04 13:02:06

您是否尝试过使用其他设备?
我注意到 MediaRecorder 在 xperia x8 上运行有很多问题,所以我有这段代码:

try {
        // test availability
        MediaRecorder mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorderAvailable = true;
    } catch (Exception e) {
        Log.d("SOUND_METER", "setAudioSource failed");
        recorderAvailable = false;
    }

我也想知道是否有人真正有一个真正的解释,因为我的很多用户都有这个设备,所以这很烦人...

Have you tried with another device ?
I've noticed that MediaRecorder has lot of problem running on xperia x8, so I had this piece of code :

try {
        // test availability
        MediaRecorder mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorderAvailable = true;
    } catch (Exception e) {
        Log.d("SOUND_METER", "setAudioSource failed");
        recorderAvailable = false;
    }

I too would like to know if anyone has actually a real explaination, because lot of my users have this device, so it's very annoying...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文