我认为我已经接近实现所需的功能,但是在尝试将当前的幅度整数转换为textview中的字符串时遇到错误。
所以我的目的是:显示摄像机预览(工作),检测声音(可能工作),并在textview(textPowerUp)中显示该声音的当前幅度级别。后来我想用一个计数器替换textview,该计数器在检测到声音时会向上计数,而在没有声音时会慢慢向下计数,但是会踩到婴儿。
因此,如果我注释掉该行,则不会出错:
txtPowerLevel.setText(Integer.toString(currentAmplitude));
但这是我正在尝试的用于将幅度整数转换为字符串并将其插入到我的textview中的行。并且此行给出了空引用错误。
我什至尝试通过以下方式强制currentAmplitude的值至少为0:
if (mRecorder.getMaxAmplitude() > 0) {
currentAmplitude = mRecorder.getMaxAmplitude();
} else {
currentAmplitude = 0;
}
但这似乎无济于事。我仍然收到空引用错误。我什至尝试将else设置为值“ 1”,以防万一“ 0”被视为“空”,但也没有运气。
这是整个班级...请让我知道您是否能找到问题!
public class DBZPowerUp extends AppCompatActivity implements TextureView.SurfaceTextureListener {
MediaRecorder mRecorder = new MediaRecorder();
private Handler mHandler = new Handler();
private Camera mCamera;
private TextureView mTextureView;
private int currentAmplitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbzpowerup);
// Variables for textureview
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
// Build the camera preview onto textureview
setContentView(mTextureView);
// Run methods to start audio capture
startAudioCapture();
getAmplitude();
// Repeat audio methods constantly
mHandler.postDelayed(runnable, 100);
// Input the current amplitude level into the power level textview
setCurrentAmplitude();
TextView txtPowerLevel = (TextView) findViewById(R.id.txtPowerLevel);
txtPowerLevel.setText(Integer.toString(currentAmplitude));
}
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open();
try {
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
} catch(IOException e) {
Log.e("DBZ_", "Camera broke");
}
mCamera.setDisplayOrientation(90);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, Camera does all the work for us
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
stopAudioCapture();
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Invoked every time there's a new Camera preview frame
}
public void startAudioCapture() {
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mRecorder.setOutputFile("/dev/null");
try {
mRecorder.prepare();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mRecorder.start();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopAudioCapture() {
mRecorder.stop();
mRecorder.release();
}
public double getAmplitude() {
if (mRecorder != null)
return mRecorder.getMaxAmplitude();
else
return 0;
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
/* do what you need to do */
getAmplitude();
/* and here comes the "trick" */
mHandler.postDelayed(this, 100);
}
};
private void setCurrentAmplitude() {
if (mRecorder.getMaxAmplitude() > 0) {
currentAmplitude = mRecorder.getMaxAmplitude();
} else {
currentAmplitude = 0;
}
}
}
非常感谢高级!
李
回答(感谢Blackbelt):
我变了:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbzpowerup);
// Variables for textureview
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
// Build the camera preview onto textureview
setContentView(mTextureView);
至:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbzpowerup);
// Define the textureview in XML and apply the surface listener for camera preview
TextureView mTextureView = (TextureView) findViewById(R.id.textureView1);
mTextureView.setSurfaceTextureListener(this);
最佳答案
您的NPE问题是由于
// Build the camera preview onto textureview
setContentView(mTextureView);
这样,您将覆盖他先前对
setContentView
的调用,其中可能包含您的TextView
。您可以将TextureView
添加到dbzpowerup.xml
并使用findViewById
来获取对其的引用关于android - 在文本 View 中显示当前音频幅度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32987236/