Reputation: 721
I don't kno why but the start() method throw an error and crash the app:
public class Noise extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.noise);
MediaRecorder recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try{
recorder.prepare();
}catch(IllegalStateException e){
Log.d("Error",e.toString());
e.printStackTrace();
}catch(IOException e){
Log.d("Error",e.toString());
e.printStackTrace();
}
recorder.start();
Timer timer=new Timer();
timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 500);
}
private class RecorderTask extends TimerTask{
TextView risultato=(TextView) findViewById(R.id.risultato_recorder);
private MediaRecorder recorder;
public RecorderTask(MediaRecorder recorder){
this.recorder = recorder;
}
public void run(){
runOnUiThread(new Runnable() {
@Override
public void run() {
risultato.setText("" + recorder.getMaxAmplitude());
}
});
}
}
}
if i remove the prepare and the start, it work but return always 0 in the textview. anyone can help me? this thing make me crazy
this is the logcat: https://dl.dropbox.com/u/16047047/log.txt
and in the phone, it crash.
Upvotes: 1
Views: 5530
Reputation: 387
Similar problem might happens with mediaRecoder.setOutputFormat(value)
Check this post https://stackoverflow.com/a/37232920/3533092
Upvotes: 1
Reputation: 20885
It seems you miss a recorder.setOutputFile(PATH_NAME);
See the documentation for MediaRecorder
At least, this is what the stack trace tells us:
11-29 16:04:08.933: W/System.err(9323): java.io.IOException: No valid output file
This is the relevant source code for MediaRecorder.prepare()
:
public void prepare() throws IllegalStateException, IOException
{
if (mPath != null) {
FileOutputStream fos = new FileOutputStream(mPath);
try {
_setOutputFile(fos.getFD(), 0, 0);
} finally {
fos.close();
}
} else if (mFd != null) {
_setOutputFile(mFd, 0, 0);
} else {
throw new IOException("No valid output file");
}
_prepare();
}
(you can read it on grepcode) The exception is thrown if both the File
object and the FileDescriptor
are null
. So I don't think you can use MediaRecorder
without supplying a file. You can try the tricky /dev/null
but I don't know if it works, and can't test right now
Upvotes: 3
Reputation: 8176
Looks like you're calling start()
twice, once in the Activity onCreate()
, once in the RecorderTask constructor. You should only need one. Without knowing how it crashes -- like a stack trace -- it's hard to know more.
Upvotes: 1