当录制停止时,NullPointerException被停止

时间:2014-04-05 11:07:05

标签: java android audio recording

我试图制作一个应用程序,当点击一个按钮时开始录制,再次单击它会停止录制。我没有想到我在录制时遇到问题,但是当我再次单击该按钮时,应用程序崩溃并显示NullPointerException,并且表示在无效状态1中调用stop

我的代码:

package com.example.victwo;

import java.io.IOException;

import android.media.MediaRecorder;

public class Audio {
    private String path;

    MediaRecorder myAudioRecorder = null;

    public Audio(String path) {
        this.path = path;
    }

    public void play() {

    }

    public void startRecording() {
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(path);

        try {
            myAudioRecorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        myAudioRecorder.start();
    }

    public void stopRecording() {
        myAudioRecorder.stop();
        myAudioRecorder.release();
        myAudioRecorder = null;
    }

}

这是我调用音频类方法的活动中的代码:

    case R.id.action_record:
        Audio recAndPlay = new Audio(this.path + "/" + this.title);

        if(flag == 0) {
            Log.d("RECORD", "START");
            flag = 1;
            if(title.equals("")) {
                setTitle();
            }
            recAndPlay.startRecording();
            return true;
        }

        if(flag == 1) {
            Log.d("RECORD", "STOP");
            recAndPlay.stopRecording();
        }
        return true;

logcat:

04-05 14:02:58.909: E/AndroidRuntime(5730): FATAL EXCEPTION: main
04-05 14:02:58.909: E/AndroidRuntime(5730): java.lang.NullPointerException
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.example.victwo.Audio.stopRecording(Audio.java:41)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.example.victwo.NoteActivity.onOptionsItemSelected(NoteActivity.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:339)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:344)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:777)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:922)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:544)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:105)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.view.View.performClick(View.java:2485)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.view.View$PerformClick.run(View.java:9080)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Handler.handleCallback(Handler.java:587)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Looper.loop(Looper.java:123)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at java.lang.reflect.Method.invoke(Method.java:507)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at dalvik.system.NativeStart.main(Native Method)

6 个答案:

答案 0 :(得分:2)

在startRecording()

的外侧初始化您的媒体播放器
 myAudioRecorder = new MediaRecorder();

修改您的代码,如下所示

public class Audio {
private String path;




MediaRecorder myAudioRecorder = new MediaRecorder();

public Audio(String path) {
    this.path = path;
}

public void play() {

}

public void startRecording() {

    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(path);

    try {
        myAudioRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    myAudioRecorder.start();
}

public void stopRecording() {
    myAudioRecorder.stop();
    myAudioRecorder.release();
    myAudioRecorder = null;
}
}

希望这会对你有所帮助

答案 1 :(得分:0)

在您致电myAudioRecorder时,stopRecording()必须为空。我无法判断您的代码为什么(您是否两次致电stopRecording()?)。如果您发布更多代码,我可以帮助您。您还可以使用支票围绕stopRecording()方法中的语句:

public void stopRecording() {
    if(myAudioRecorder != null){
       myAudioRecorder.stop();
       myAudioRecorder.release();
       myAudioRecorder = null;
    }
}

答案 2 :(得分:0)

private void stopRecording() {
        if (null != recorder) {
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;
        }
    }

答案 3 :(得分:0)

请在停止之前检查MediaRecorder对象是否为空。

答案 4 :(得分:0)

我认为你没有采取正确的方法。为什么在开始时将媒体播放器声明为null。初始化和完成时使用媒体播放器类的方法来释放/重置它。

在开始使用mediaplayer之前,使用if(){...}阻止来检查它是否已初始化。以下示例;

   private void stopRecording() {
      if (myAudioRecorder != null) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();
        myAudioRecorder.release();

        myAudioRecorder = null;
     }
  }

答案 5 :(得分:0)

另一个原因可能是,它已经停止了,然后你重新使用.stop()函数,这会导致问题,检查你的逻辑和代码......