Sound not replaying on onTouchListener - java

So I have a button that, when held, will play a sound. However, it does not seem to "reset" and never plays that sound again when I hold down the button again. I get a
E/MediaPlayer﹕ start called in state 0
E/MediaPlayer﹕ error (-38, 0)
error in log when I try to hold the button down again. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
final MediaPlayer bark = MediaPlayer.create(this, R.raw.bark);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()) bark.stop();
break;
}
return true;
}
});
}

Instead of calling start/stop you can call start/pause with seekTo():
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()){
bark.pause();
bark.seekTo(0);
}
break;
If you want to use start/stop, make sure you call prepare() before calling start() again.
The reason why your code isn't working is because according to the state diagram stop() stops playing and puts the MP in a stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
EDIT : Place your music file in the assets folder and use the file descriptor. This way you can reset the MP and replay:
Using START/STOP:
#Override
protected void onCreate(Bundle savedInstanceState) {
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("bark.mp3"); //replace with right extension
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()){
bark.reset();
bark.setDataSource(fileDescriptor.getFileDescriptor());
bark.prepare();
bark.start();
}
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying())
bark.stop();
break;
}
return true;
}
});
}

Related

Button long click does not work

I'm working on a soundboard and I want to implement a long click to share the sound.
I am working with a switch Case for each button
public void MainMMP(View view){
switch (view.getId()) {
case R.id.button1:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx1);
MainMMP.start();
break;
case R.id.button2:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx2);
MainMMP.start();
break;
case R.id.button3:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx3);
MainMMP.start();
break;
And now I want to implement the long click. I tried a lot of different code here but it is not working for me.
I do not know where to put the onLongClick statement and how.
Can somebody show me a working method and in case of long click it should just send me a Toast that I know the method works?
You could add the OnLongClickListener where you want, in the onCreate method for example.
Try to use the following code:
Button button = (Button)findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Your code
return false; // True if you want to execute simple click code too
}
});
You can use this
private View.OnLongClickListener listener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
switch (view.getId())
case R.id.button1:
// Do something...
break;
case R.id.button2:
// Do something else...
break;
// If you still want to get normal click callbacks return true,
// if you do not then return false.
return true;
}
}
Somewhere in your code
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnLongClickListener(listener);
button2.setOnLongClickListener(listener);
Or better this
One common recommended way to get onClick/onLongClick/whatever callbacks is to make the Activity implement the callback interfaces.
class YourActivity extend Activity implements View.OnLongClickListener {
#Override
public boolean onCreate(/* ... */) {
// ...
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
}
#Override
public boolean onLongClick(View view) {
// Same code as the one above
}
}

How do I get an image view to continually move across the screen when a button is held down?

I have searched high and low for an answer to this and can't find one anywhere that works.
For my university assignment i need to create an adventure game in android studio. I want it so when I click down and hold an arrow button (so in the case given its the up button), that the ImageView (player) will continually move across the screen until I release the button. Ive tried OnTouchListeners and mouse events using ACTION_UP and ACTION_DOWN and that works but not for what i need as it still only moves one step when clicked.
ImageView IV_player;
Button ButtonUp;
IV_player = (ImageView) findViewById(R.id.IV_player);
ButtonUp = (Button) findViewById(R.id.ButtonUp);
ButtonUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IV_player.setY(IV_player.getY() - 32);
}
});
Treat your touch listener like a state machine. When an ACTION_DOWN event occurs, start doing whatever action you want to do. When an ACTION_UP/ACTION_CANCEL event occurs stop your action. So how do you go about implementing this?
Your state flag can be a simple boolean:
boolean shouldCharacterMove = false;
Define your touch listener for the view.
ButtonUp.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
setShouldCharacterMove(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setShouldCharacterMove(false);
break;
}
return true;
}
});
Before we define setShouldCharacterMove we need to figure out a way to move items. We can do this through a Runnable that runs after X milliseconds.
private final Runnable characterMoveRunnable = new Runnable() {
#Override
public void run() {
float y = IV_player.getTranslationY();
IV_player.setTranslationY(y + 5); // Doesn't have to be 5.
if (shouldCharacterMove) {
IV_player.postDelayed(this, 16); // 60fps
}
}
};
Now we can define setShouldCharacterMove:
void setShouldCharacterMove(boolean shouldMove) {
shouldCharacterMove = shouldMove;
IV_player.removeCallbacks(characterMoveRunnable);
if (shouldMove) {
IV_player.post(characterMoveRunnable);
}
}

Launch application on 2 finger swipe ANDROID

I have this code which doesn't return any errors. However it doesn't work like it's supposed to. I'm trying to launch an application when I swipe up with two fingers but nothing is happening. Can anyone see what the problem with my code?
public class HomeActivity extends Activity {
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
}
}
else {
//do something
}
}
Try debugging handleTouch() to see exactly why its not firing. My guess would be because you have your check on MotionEvent.ACTION_DOWN, which in real world is a bit difficult to happen exactly at the same time for two fingers.
Move the check to MotionEvent.ACTION_MOVE (which is what you want anyway cause you want swipe, not tap) and try again.
MotionEvent.ACTION_DOWN means one finger. MotionEvent.ACTION_POINTER_DOWN means secondary touches (eg. two fingers).
So moving your code to case MotionEvent.ACTION_POINTER_DOWN: should solve your issue.
Documentation.

Android OnTouchListener Does Not Work

It's simple. The OnTouchListener does not work at all.
I'm fairly certain it's being initialized. I'm using a custom view to draw my UI, and as usual, I'm being impeded by Android's touchy API (pun intended):
public class ViewInterface extends View implements OnTouchListener{
public ViewInterface(Context context){
super(context);
...
}
public void update(){
...
}
#Override
public void onDraw(Canvas canvas){
...
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
keyboardEnabled = true;
return true;
case MotionEvent.ACTION_UP:
keyboardEnabled = false;
return true;
default: break;
}
return false;
}
Code for initializing the view in the main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.activity_aidan);
activity = this;
viewInterface = new ViewInterface(this);
setContentView(viewInterface);
initializeSpeechRecognition();
findMe();
run.start();
}
What am I missing? Upon touching the screen and holding it, keyboardEnabled should be set to true (its just a debug value) - yet it does not do anything. The touch events don't seem to be responding at all.
I've attempted to use the onTouchEvent method built into View with the following code:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
keyboardEnabled = true;
return true;
case MotionEvent.ACTION_UP:
keyboardEnabled = false;
return true;
default: break;
}
return false;
}
However this did not work either. The touch events are not responding at all - tapping the screen and releasing should be setting keyboardEnabled to false - which it does not.
You should override the onTouchEvent() method of the View class.
Right now you are simply implementing the OnTouchListener interface which should be used in conjunction with the setOnTouchListener() method.
For example:
#Override
public boolean onTouchEvent(MotionEvent event) {
// do whatever
}
Don't forget to call super where appropriate!
Alternatively, you could call setOnTouchListener(this); somewhere in your code, but that seems redundant.

issue with if/else and switch statements

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Categories

Resources