I want to change my activity to fragment. my code is given below. help would be appreciated. I have spend two days without any success. I have modified a lot my above code but unable to solve this. Last five lines are confusing me too much. I have also used a MediaPlayerPool class which i think is not creating problem.

package com.example.soundpoolexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
private MediaPlayerPool mediaPlayerPool;

Button button;
Button button2;

class b1 implements OnTouchListener {
    b1() {
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        if (motionEvent.getAction() == 0) {
            MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
            return true;
        } else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
            return false;
        } else {
            MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
            return false;
        }
    }
}

class b2 implements OnTouchListener {
    b2() {
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        if (motionEvent.getAction() == 0) {
            MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
            return true;
        } else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
            return false;
        } else {
            MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
            return false;
        }
    }
}

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

    this.button = (Button) findViewById(R.id.button);
    this.button2 = (Button) findViewById(R.id.button2);
    this.mediaPlayerPool = new MediaPlayerPool(this);
    this.button.setOnTouchListener(new b1());
    this.button2.setOnTouchListener(new b2());

}

}

Related posts

Recent Viewed