I'm trying to create an app that play a certain audio file, that was previously recorded, when I press a button or shake my phone. Here is my code.
public class Reproduzir extends Activity implements SensorEventListener{
MediaPlayer player = new MediaPlayer();
SensorManager sensor;
@Override
protected void onCreate(Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
sensor= (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.reproduzir);
Button reproduzir = (Button) findViewById(R.id.reproduzir);
reproduzir.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
play();
}
});
Button fechar= (Button) findViewById(R.id.fechar);
fechar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (player.isPlaying()) {
player.stop();
player.release();
}
finish();
}
});
}
public void play(){
try {
player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onStart(){
super.onStart();
sensor.registerListener(this,sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.values[0]>2.2||event.values[1]>2.2||event.values[2]>2.2){
play();
}
}
}
The app was working fine untill I added the last bit of code (the onSensorChanged method) and if I comment it the app runs fine. But when I run the code with that code I get the following error
02-14 17:59:15.425: E/MediaPlayer(12895): Unable to create media player
02-14 17:59:15.426: W/System.err(12895): java.io.IOException: setDataSourceFD failed.: status=0x80000000
02-14 17:59:15.426: W/System.err(12895): at android.media.MediaPlayer._setDataSource(Native Method)
02-14 17:59:15.426: W/System.err(12895): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1035)
02-14 17:59:15.427: W/System.err(12895): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1019)
02-14 17:59:15.427: W/System.err(12895): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:998)
02-14 17:59:15.427: W/System.err(12895): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:954)
02-14 17:59:15.427: W/System.err(12895): at com.example.segundocasopratico.Reproduzir.play(Reproduzir.java:54)
02-14 17:59:15.427: W/System.err(12895): at com.example.segundocasopratico.Reproduzir.onSensorChanged(Reproduzir.java:90)
02-14 17:59:15.427: W/System.err(12895): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:505)
02-14 17:59:15.427: W/System.err(12895): at android.os.MessageQueue.nativePollOnce(Native Method)
02-14 17:59:15.427: W/System.err(12895): at android.os.MessageQueue.next(MessageQueue.java:138)
02-14 17:59:15.427: W/System.err(12895): at android.os.Looper.loop(Looper.java:150)
02-14 17:59:15.427: W/System.err(12895): at android.app.ActivityThread.main(ActivityThread.java:5327)
02-14 17:59:15.427: W/System.err(12895): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 17:59:15.427: W/System.err(12895): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 17:59:15.428: W/System.err(12895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
02-14 17:59:15.428: W/System.err(12895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
02-14 17:59:15.428: W/System.err(12895): at dalvik.system.NativeStart.main(Native Method)
any thoughts on a reason for this?
EDIT Ok I managed to solve this. The problem was, I think, that the value for the acceleration was to small. I upped it to 10 and it's working. The problem now is that it only plays once... if I shake it again or press the button a second time it won't play.