There is such code (main_activity.xml):

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/b_button" android:text="@string/click_me" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 

(MainActivity.java):

 import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button; private MediaPlayer sound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.b_button); sound = MediaPlayer.create(MainActivity.this, R.raw.cat_muc); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sound.start(); } }); } } 

The code works like this - you press the button and the sound (meow) is played, the test is on a virtual device from android studio, everything works there, threw it on a real phone, it also robs there. But why does the sound work on genymotion only when the button is clicked for the first time, and with further presses there is nothing?

    1 answer 1

    Gennymotion clearly speaks about known issues in its FAQ https://www.genymotion.com/help/desktop/faq/#sound-windows

    For your code, I would add:

    • checking that the player is no longer playing (most likely GennyMotion does not complete)
    • do release in onStop

    Read more https://developer.android.com/reference/android/media/MediaPlayer#StateDiagram