Search code examples
javaandroidandroid-mediaplayer

MediaPlayer - Playing and stopping when let go and press on button


I'm gonna start by sharing the code that I have.

package com.example.onebuttonpressimage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.view.MotionEvent;    // new import library for onclick listener


public class MainActivity extends AppCompatActivity {
    // declare the objects and variables
    private ImageButton buttonJava;  // creates the JAVA object that holds button attributes and methods
    private MediaPlayer sound = null; // creates the JAVA object that holds the mediaplayer attributes and methods
    //private boolean pressed=false;

    // Creates the app window ===================================================================================
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sound = MediaPlayer.create(this, R.raw.beep);             // ties the JAVA mediaplayer object to the sound file
        buttonJava = (ImageButton) findViewById(R.id.buttonXML);  // ties the JAVA button object to the XML object


        buttonJava.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    buttonJava.setBackgroundResource(R.drawable.button_pressed);
                    sound.start();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    buttonJava.setBackgroundResource(R.drawable.button_not_pressed);
                }
             return false;
            }
        });



}}

Here is what I'm trying to do. I'm trying to make this play the sound until I let go of the button. I tried using sound.stop() but then it will only work once and then no longer work until I relaunch the app. Does anyone have any ideas? I want to try making it behave the way I need it without adding too much more to it. If anyone needs more clarification on what I need, please let me know. The following is my XML layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.onebuttonpressedimage.MainActivity">

    <ImageButton
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:id="@+id/buttonXML"
        android:background="@drawable/blue"
        android:layout_centerHorizontal="true"
        android:onClick="onButtonClick" />

</RelativeLayout>

Solution

  • I was able to do it myself after some modifications to the buttonJava java code with some of the seekTo, Looping, and stopping when let go to make it work better.