使 ImageButton 的点击不再计数播放声音。

huangapple go评论69阅读模式
英文:

Making ImageButton clicks counted does not play sounds anymore

问题

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;
    ImageButton soundbutton;

    int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
    Random r = new Random();
    int rndm = r.nextInt(9);

    int clickcount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        soundbutton = (ImageButton) this.findViewById(R.id.playButton);
        mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
        soundbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (mp.isPlaying()) {
                        mp.stop();
                        mp.release();
                        rndm = r.nextInt(9);
                        mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
                    }
                    mp.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        soundbutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clickcount = clickcount + 1;
                if (clickcount == 1) {
                    // first time clicked to do this
                    Toast.makeText(getApplicationContext(), "text2", Toast.LENGTH_LONG).show();
                } else {
                    // check how many times clicked and so on
                    Toast.makeText(getApplicationContext(), "text1:" + clickcount, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
};
英文:

I'm new in android. I've implemented code to play random sounds when clicking an ImageButton, first problem is that it does not play random sounds all the time when button is clicked - it choose one sound and plays only this one, when I press the button next time it plays same sound as the first time when button is clicked - I would make this random every time when button is clicked. Also when I added function which counts how many times this button is clicked, app stopped to play sounds anymore, it only shows how many times my button was clicked, but there is no more audio. Here is my code:

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton soundbutton;
int[] sounds={R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
Random r = new Random();
//int Low = 0;
//int High = 9;
//int rndm = r.nextInt(High-Low) + Low;
int rndm = r.nextInt(9);
int clickcount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//media player
soundbutton = (ImageButton)this.findViewById(R.id.playButton);
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt(9);
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
}
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
soundbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==1)
{
//first time clicked to do this
Toast.makeText(getApplicationContext(),"text2", Toast.LENGTH_LONG).show();
}
else
{
//check how many times clicked and so on
Toast.makeText(getApplicationContext(),"text1:"+clickcount, Toast.LENGTH_LONG).show();
}
}
});
}
};

答案1

得分: 0

你不能在同一个视图上设置两个 setOnClickListener。

try {
    if (mp.isPlaying()) {
        mp.stop();
        mp.release();
    }
    rndm = r.nextInt(9);
    mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
    mp.start();
} catch (Exception e) {
    e.printStackTrace();
}

注意你之前写的用于设置 mp 的代码只会在 mp 正在播放声音时运行我刚刚将它移到了 if 语句之外
英文:

You can't set 2 setOnClickListeners on same View .

try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
rndm = r.nextInt(9);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
mp.start();
} catch (Exception e) {
e.printStackTrace();
}

see the code you wrote to set mp will run only when the mp is playing a sound.. I just moved it outside the if.

huangapple
  • 本文由 发表于 2020年9月17日 16:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63934358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定