多次按下按钮时 Mediaplayer 重叠
Mediaplayer overlaps when the button is pressed several times
我正在 Android Studio 中使用 MediaPlayer 制作一个应用程序来使用我的音频剪辑,当我按下一个按钮时,音频播放正确,当我按下另一个按钮时,前一个停止,新的停止开始,但如果我重复按同一个按钮,它就会开始重叠,当我按下“后退”按钮时,只有一个音频停止,而其他音频仍在播放。
我该如何避免这种情况,多次按下按钮时,它会重叠,而是“重新启动”而不重叠。
谢谢!
这是我的 Java 代码
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Infomezquita extends AppCompatActivity {
private MediaPlayer AZASOUND;
private MediaPlayer AZASOUNDUK;
private MediaPlayer AZARAB;
private MediaPlayer AZAFRENCH;
private MediaPlayer AZAGER;
private MediaPlayer AZAIT;
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mezquita);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Mosque");
@Override
public void onBackPressed() {
if (AZASOUNDUK != null && AZASOUNDUK.isPlaying()) {
AZASOUNDUK.stop(); // or mp.pause();
AZASOUNDUK.release();
}
if(AZASOUND != null && AZASOUND.isPlaying()) {
AZASOUND.stop();
//AZASOUND.release();
}
if(AZARAB != null && AZARAB.isPlaying()) {
AZARAB.stop();
//AZASOUND.release();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying()) {
AZAFRENCH.stop();
//AZASOUND.release();
}
if(AZAGER != null && AZAGER.isPlaying()) {
AZAGER.stop();
//AZASOUND.release();
}
if(AZAIT != null && AZAIT.isPlaying()) {
AZAIT.stop();
//AZASOUND.release();
}
else {
finish();
}
super.onBackPressed();
}
@Override
protected void onStop() {
super.onStop();
if (handler != null)
handler.removeCallbacks(null);
}
public void SONIDO (View view)
{
AZASOUND = MediaPlayer.create(this, R.raw.mezquitaes);
AZASOUND.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onStop();
}
public void SONIDOUK (View view) {
AZASOUNDUK = MediaPlayer.create(this, R.raw.mezquitauk);
AZASOUNDUK.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOARAB (View view) {
AZARAB = MediaPlayer.create(this, R.raw.mezquitaarab);
AZARAB.start();
Toast.makeText(Infomezquita.this, "ارفع الصوت", Toast.LENGTH_SHORT).show(); //OK
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
super.onPause();
}
public void SONIDOGER (View view) {
AZAGER = MediaPlayer.create(this, R.raw.mezquitager);
AZAGER.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOFRENCH (View view) {
AZAFRENCH = MediaPlayer.create(this, R.raw.mezquitafr);
AZAFRENCH.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOITA (View view) {
AZAIT = MediaPlayer.create(this, R.raw.mezquitait);
AZAIT.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void MAPA(View view) {
Intent intent = new Intent(this, MapaROMANO.class);
startActivity(intent);
}
public void FOTOS(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void ROM2(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void MAPAMEZCA(View view) {
Intent intent = new Intent(this, Mapa1.class);
startActivity(intent);
}
public void URLMEZCA (View view) {
Uri uriUrl = Uri.parse("example");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
[1]: https://www.youtube.com/watch?v=BlUql0PqSgQ
问题是很多MediaPlayer创作没有任何发布。请检查以下代码,它可能会帮助您确定正确的方法。
private MediaPlayer mediaPlayer;
public void play(int rawSoundId){
stop();
mediaPlayer = MediaPlayer.create(this, rawSoundId);
mediaPlayer.start();
}
public void pause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
public void stop() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
只需从您的视图函数中调用 play(R.raw.mezquitait)。
我正在 Android Studio 中使用 MediaPlayer 制作一个应用程序来使用我的音频剪辑,当我按下一个按钮时,音频播放正确,当我按下另一个按钮时,前一个停止,新的停止开始,但如果我重复按同一个按钮,它就会开始重叠,当我按下“后退”按钮时,只有一个音频停止,而其他音频仍在播放。
我该如何避免这种情况,多次按下按钮时,它会重叠,而是“重新启动”而不重叠。
谢谢!
这是我的 Java 代码
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Infomezquita extends AppCompatActivity {
private MediaPlayer AZASOUND;
private MediaPlayer AZASOUNDUK;
private MediaPlayer AZARAB;
private MediaPlayer AZAFRENCH;
private MediaPlayer AZAGER;
private MediaPlayer AZAIT;
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mezquita);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Mosque");
@Override
public void onBackPressed() {
if (AZASOUNDUK != null && AZASOUNDUK.isPlaying()) {
AZASOUNDUK.stop(); // or mp.pause();
AZASOUNDUK.release();
}
if(AZASOUND != null && AZASOUND.isPlaying()) {
AZASOUND.stop();
//AZASOUND.release();
}
if(AZARAB != null && AZARAB.isPlaying()) {
AZARAB.stop();
//AZASOUND.release();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying()) {
AZAFRENCH.stop();
//AZASOUND.release();
}
if(AZAGER != null && AZAGER.isPlaying()) {
AZAGER.stop();
//AZASOUND.release();
}
if(AZAIT != null && AZAIT.isPlaying()) {
AZAIT.stop();
//AZASOUND.release();
}
else {
finish();
}
super.onBackPressed();
}
@Override
protected void onStop() {
super.onStop();
if (handler != null)
handler.removeCallbacks(null);
}
public void SONIDO (View view)
{
AZASOUND = MediaPlayer.create(this, R.raw.mezquitaes);
AZASOUND.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onStop();
}
public void SONIDOUK (View view) {
AZASOUNDUK = MediaPlayer.create(this, R.raw.mezquitauk);
AZASOUNDUK.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOARAB (View view) {
AZARAB = MediaPlayer.create(this, R.raw.mezquitaarab);
AZARAB.start();
Toast.makeText(Infomezquita.this, "ارفع الصوت", Toast.LENGTH_SHORT).show(); //OK
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
super.onPause();
}
public void SONIDOGER (View view) {
AZAGER = MediaPlayer.create(this, R.raw.mezquitager);
AZAGER.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOFRENCH (View view) {
AZAFRENCH = MediaPlayer.create(this, R.raw.mezquitafr);
AZAFRENCH.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOITA (View view) {
AZAIT = MediaPlayer.create(this, R.raw.mezquitait);
AZAIT.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void MAPA(View view) {
Intent intent = new Intent(this, MapaROMANO.class);
startActivity(intent);
}
public void FOTOS(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void ROM2(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void MAPAMEZCA(View view) {
Intent intent = new Intent(this, Mapa1.class);
startActivity(intent);
}
public void URLMEZCA (View view) {
Uri uriUrl = Uri.parse("example");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
[1]: https://www.youtube.com/watch?v=BlUql0PqSgQ
问题是很多MediaPlayer创作没有任何发布。请检查以下代码,它可能会帮助您确定正确的方法。
private MediaPlayer mediaPlayer;
public void play(int rawSoundId){
stop();
mediaPlayer = MediaPlayer.create(this, rawSoundId);
mediaPlayer.start();
}
public void pause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
public void stop() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
只需从您的视图函数中调用 play(R.raw.mezquitait)。