Problem when loading audio with Android Studio

1

I'm doing a metronome and the problem I have is the following. I was using a 1 second sound file to make the metronome sound. The problem comes when the rhythm of the metronome exceeds 60 beats per second, because being the audio of 1 second can not exceed that speed. I have tried to use an audio file of about 0.25 seconds and when loading it in the Media Player it does not detect it and it gives the error of null pointer. I have listened to the file with the windows player and it works well, it is also well imported. Does anyone know what the problem is?

public class MainActivity extends AppCompatActivity {
private Button start, stop, tap;
private MediaPlayer mp;
private EditText e;
private boolean on = false;
CountDownTimer cdt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mp = MediaPlayer.create(this, R.raw.sonidometronomo);
    start = (Button) findViewById(R.id.Start);
    stop = (Button) findViewById(R.id.Stop);
    tap = (Button) findViewById(R.id.Tap);
    e = (EditText) findViewById(R.id.editText);

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int x = Integer.parseInt(e.getText().toString());
                if(x > 0) {
                    long tiempoEnMilisegundos = 60000/x;
                    Log.d("MILIS", String.valueOf(tiempoEnMilisegundos));
                    cdt = new CountDownTimer(tiempoEnMilisegundos * 10, tiempoEnMilisegundos) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            mp.start();
                        }

                        @Override
                        public void onFinish() {
                            this.start();
                        }
                    };
                    cdt.start();
                }
            } catch (Exception e1) {
                e.setText("Error");
            }
        }
    });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cdt!=null) {
                cdt.cancel();
            }
        }
    });

The error he gives me is this:

  

FATAL EXCEPTION: main                                                                          Process: window.pruebaventanas, PID: 27973                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start ()' on a null object reference                                                                              at window.pruebaventanas.MainActivity $ 1.onClick (MainActivity.java:36)                                                                              at android.view.View.performClick (View.java:5246)                                                                              at android.widget.TextView.performClick (TextView.java:10618)                                                                              at android.view.View $ PerformClick.run (View.java:21256)                                                                              at android.os.Handler.handleCallback (Handler.java:739)                                                                              at android.os.Handler.dispatchMessage (Handler.java:95)                                                                              at android.os.Looper.loop (Looper.java:145)                                                                              at android.app.ActivityThread.main (ActivityThread.java:6934)                                                                              at java.lang.reflect.Method.invoke (Native Method)                                                                              at java.lang.reflect.Method.invoke (Method.java:372)                                                                              at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1404)                                                                              at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)

    
asked by Mikel Molinuevo 01.07.2017 в 20:15
source

1 answer

1

The problem is that when you click the button, the instance of MediaPlayer has a value of null.

You must correctly initialize mp ( MediaPlayer ) in this way:

mp = MediaPlayer.create(this, R.raw.sonidometronomo);
    
answered by 01.07.2017 / 20:38
source