英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论