Search code examples
androidmedia-playeronlongclicklistener

android media player with long click


I have one button. On a long click I want the user to be prompted to assign a song. On a normal click I want the song to play, and if the song is already playing I want it to reset. I have some toasting action going on to help keep things clear to the user. I need another button to pause.

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.media.MediaPlayer;
    import android.view.View;
    import android.widget.Toast;
    import java.io.IOException;


    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void button_one(View v) {

            try {
                Toast myToast = Toast.makeText(
                        getApplicationContext(),
                        "playing",
                        Toast.LENGTH_LONG);
                //MediaPlayer should Reset
                //MediaPlayer should Play
                myToast.show();

            }catch //something needs to go here to catch all errors
                {
                Toast myToast = Toast.makeText(
                        getApplicationContext(),
                        "add a song first",
                        Toast.LENGTH_LONG);
                myToast.show();

            }

        }
    }

Solution

  • I'm not sure what the question here is.

    Do you want to know how to make handlers for a button on a long and normal click? Do you want to know how to play/pause music. Or something else?

    For the first case I may have an answer:

    Button b = (Button) findViewById(R.id.btnStartMusic);
        b.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //Assign a song code here
                return false;
            }
        });
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Play song code here
            }
        });