0

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.

Rasmnev
  • 109
  • 2
  • 11
  • java.io.IOException: setDataSourceFD failed.: status=0x80000000 it cant find your datasource or it is unable to play the file itself. – Reenactor Rob Feb 14 '15 at 18:11

2 Answers2

2

I do not believe this has to do with your accelerometer.

0x80000000 means the file is corrupted, or incomplete.

See this: Exception when calling setDataSource(FileDescriptor) method (failed.: status=0x80000000)

Community
  • 1
  • 1
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • the file is generated in a previous activity, that records sound and launches this one. See my edit please – Rasmnev Feb 15 '15 at 10:17
0

This line is causing the Exception.

player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");

Make sure that your file is existing, is accessable and is not corrupted or damaged. If you are running on the emulator, sometimes when pushing files to the emulator it corrupts the files. Try it on your phone, if you can.

Marcus
  • 6,697
  • 11
  • 46
  • 89
  • I tried it on my phone. 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. – Rasmnev Feb 15 '15 at 10:16
  • Glad it worked out. You should post a new question addressing this new issue of yours @Rasmnev – Marcus Feb 15 '15 at 10:26
  • Yes, you need to open a new question since the original question has been solved, and a new issue has arisen. Its then easier to provide more details and a better clarity to this new issue. Good luck and happy coding! @Rasmnev – Marcus Feb 15 '15 at 10:36