[JAVA] The road to creating a music game 2

Progress from the last time

From the last time, you can make a sound by shaking it from side to side! ... that's it m (_ _) m Before I completed it, I thought it would be easy to make a sound by shaking it from side to side, but it's terribly annoying ... For some reason, there is no sound even if I do not shake it, or if I think that there is no sound even if I do not shake it, there is no sound even if I shake it, it causes multiple reactions, I devised various things, and finally shake it left and right Now makes a sound! Shown below.

Source code

MainActivity.java


public class MainActivity extends AppCompatActivity implements SensorEventListener {
    ImageView Image1, Image2, Image3, Image4;
    int i,j, Delay;
    float X_Data, Y_Data, Z_Data;

    private Timer timer1;
    private CountUpTimerTask timerTask1;
    private Handler handler1 = new Handler();
    private Runnable runnable;
    private final Handler handler = new Handler();

    private SensorManager sensorManager;
    private TextView X_Data_TextView , Y_Data_TextView , Z_Data_TextView;

    SoundPool soundPool;
    int mp3_1,mp3_x;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        X_Data_TextView = findViewById(R.id.X_Data_Text);
        Y_Data_TextView = findViewById(R.id.Y_Data_Text);
        Z_Data_TextView = findViewById(R.id.Z_Data_Text);
        StartCyclicHandler();

        Image1 = findViewById(R.id.image1);
        Image2 = findViewById(R.id.image2);
        Image3 = findViewById(R.id.image3);
        Image4 = findViewById(R.id.image4);
        Button startButton = findViewById(R.id.strat);
        Button endButton = findViewById(R.id.End);
        EditText et = findViewById(R.id.BpM);

        Image1.setImageResource(R.drawable.en);
        Image2.setImageResource(R.drawable.en);
        Image3.setImageResource(R.drawable.en);
        Image4.setImageResource(R.drawable.en);

        //Allow only numbers to be entered in EditText
        et.setInputType(InputType.TYPE_CLASS_NUMBER);

        //Convert input value to int type
        String bpm = ((EditText) findViewById(R.id.BpM)).getText().toString();
        int BPM = Integer.parseInt((bpm));

        //The one you need to add sound effects
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        } else {
            AudioAttributes attr = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build();
            soundPool = new SoundPool.Builder()
                    .setAudioAttributes(attr)
                    .setMaxStreams(5)
                    .build();
        }



        mp3_1 = soundPool.load(this, R.raw.pop, 1);
        mp3_x = soundPool.load(this,R.raw.feed1,1 );

        //STARTButton
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i = 0;

                String bpm = ((EditText) findViewById(R.id.BpM)).getText().toString();
                int BPM = Integer.parseInt((bpm));
                Delay = 60000 / BPM;

                if (null != timer1) {
                    timer1.cancel();
                    timer1 = null;
                }

                Image1.setImageResource(R.drawable.en);
                Image2.setImageResource(R.drawable.en);
                Image3.setImageResource(R.drawable.en);
                Image4.setImageResource(R.drawable.en);

                timer1 = new Timer();

                timerTask1 = new CountUpTimerTask();

                timer1.schedule(timerTask1, 0, Delay);
            }

        });

        //ENDButton
        endButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != timer1) {
                    timer1.cancel();
                    timer1 = null;
                }


                Image1.setImageResource(R.drawable.en);
                Image2.setImageResource(R.drawable.en);
                Image3.setImageResource(R.drawable.en);
                Image4.setImageResource(R.drawable.en);
            }
        });
    }

    class CountUpTimerTask extends TimerTask {
        @Override
        public void run() {
            handler1.post(new Runnable() {
                @Override
                public void run() {
                    if (i == 0) {
                        Image1.setImageResource(R.drawable.eng);
                        Image4.setImageResource(R.drawable.en);
                        soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
                        i++;
                    } else if (i == 1) {
                        Image1.setImageResource(R.drawable.en);
                        Image2.setImageResource(R.drawable.eng);
                        soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
                        i++;
                    } else if (i == 2) {
                        Image2.setImageResource(R.drawable.en);
                        Image3.setImageResource(R.drawable.eng);
                        soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
                        i++;
                    } else {
                        Image3.setImageResource(R.drawable.en);
                        Image4.setImageResource(R.drawable.eng);
                        soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
                        i = 0;
                    }
                }
            });

        }
    }


    @Override protected void onResume() {
        super.onResume();
        //Event Listener registration
        Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override protected void onPause() {
        super.onPause();
        //Event Listener unregistered
        sensorManager.unregisterListener(this);
        StoptCyclicHandler();
    }



    @Override public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
            X_Data = (500+(event.values[0]*25));
            Y_Data = (500+(event.values[1]*25));
            Z_Data = (500+(event.values[2]*25));
        }
    }

    protected void StartCyclicHandler(){
        runnable = new Runnable() {
            @Override
            public void run() {

                X_Data_TextView.setText(String.format("%.3f",X_Data));
                Y_Data_TextView.setText(String.format("%.3f",Y_Data));
                Z_Data_TextView.setText(String.format("%.3f",Z_Data));

                if(X_Data>800){
                    soundPool.play(mp3_x, 2, 2, 0, 0, 1f);
                }
                else if (X_Data<150){
                    soundPool.play(mp3_x, 2, 2, 0, 0, 1f);
                }
            }

                handler.postDelayed(this,200); //200ms delay
            }
        };
        handler.post(runnable);
    }

    protected void StoptCyclicHandler(){
        handler.removeCallbacks(runnable);
    }

    @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

}

What I added this time is the part that handles the accelerometer and the part that makes a sound when shaken left and right (that's right). The point that I devised in the part that swings from side to side is to add a delay. By adding a delay, the sounds are no longer duplicated.

Recommended Posts

The road to creating a music game 2
The road to creating a music game 3
The road to creating a music game 1
The road to creating a Web service (Part 1)
The road from JavaScript to Java
The road to Web service creation (Part 2)
Java SE8 Silver ~ The Road to Pass ~
Creating a Servlet in the Liberty environment
A memorandum to clean up the code Ruby
The road to Japaneseizing Rails devise error messages
Make a margin to the left of the TextField
Set the time of LocalDateTime to a specific time
Try to make a music player using Basic Player
Introduce docker to the application you are creating
3. Create a database to access from the web module
A brief introduction to terasoluna5, see the text below
How to run the SpringBoot app as a service
I tried to decorate the simple calendar a little
Creating a Cee-lo game with Ruby 4th Creating a game progress process
Things to watch out for when creating a framework
How to make a mod for Slay the Spire
How to get started with creating a Rails app
I want to add a delete function to the comment function
Creating a local repository
Road to REPL (?) Creation (3)
Creating a test case
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
Road to REPL (?) Creation (1)
Road to REPL (?) Creation (2)
[Rails] Processing after adding a column to the devise table
How to take a screenshot with the Android Studio emulator
SDWebImage: How to clear the cache for a particular UIImageView
[Beginner] Try to make a simple RPG game with Java ①
How to create a form to select a date from the calendar
How to create a placeholder part to use in the IN clause
[IOS] What you need to know before creating a widget
I want to call a method and count the number
I want to create a form to select the [Rails] category
Create a method to return the tax rate in Java
[Ruby] How to retrieve the contents of a double hash
A validation error occurred when saving to the intermediate table.
How to add the same Indexes in a nested array
I want to give a class name to the select attribute
Add a shadow to the Swift Button (and also the circle)
A memorandum to reach the itchy place for Java Gold
[ruby] Creating a program that responds only to specific conditions
[jsoup] How to get the full amount of a document