gpt4 book ai didi

java - MediaPlayer 没有在每个按钮同时单击时播放音频

转载 作者:行者123 更新时间:2023-12-01 19:50:06 26 4
gpt4 key购买 nike

我正在使用 MediaPlayer 播放字母表。单击单个按钮时正在播放音频。每次按钮点击均不负有责任。当音频结束时,然后使用另一个按钮。但我想设置每个按钮同时单击播放音频。这个怎么做?请告诉我如何解决这个问题。

sor_borno.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:padding="10dp"
android:layout_gravity="center"
android:background="@drawable/onlybg"
tools:context="net.gurujibd.bornomala.alphabet.SorBorno">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout"
android:layout_above="@+id/linearLayout3"
android:layout_alignLeft="@+id/linearLayout3"
android:layout_alignStart="@+id/linearLayout3">
<Button
android:id="@+id/Soreo"
android:onClick="soreo"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/soreo"/>

<Button
android:id="@+id/Sorea"
android:onClick="sorea"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/sorea"/>

<Button
android:id="@+id/Roshshoi"
android:onClick="roshshoi"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/roshshoi"/>
<Button
android:id="@+id/Dirghoi"
android:onClick="dirghoi"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/dirghoi"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout2"
android:layout_below="@+id/linearLayout3"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/Oi"
android:onClick="oi"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/oi"/>

<Button
android:id="@+id/Oo"
android:onClick="oo"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/oo"/>

<Button
android:id="@+id/Ou"
android:onClick="ou"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/ou"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/Roshshou"
android:onClick="roshshou"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/roshshou"/>

<Button
android:id="@+id/Durghou"
android:onClick="durghou"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/durghou"/>

<Button
android:id="@+id/Ree"
android:onClick="ree"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/ree"/>
<Button
android:id="@+id/Aa"
android:onClick="aa"
android:layout_width="100dp"
android:layout_height="85dp"
android:background="@drawable/aa"/>

</LinearLayout>

</RelativeLayout>

SorBorno.java

package net.gurujibd.bornomala.alphabet;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class SorBorno extends AppCompatActivity {

InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;

Button Soreo, Sorea, Roshshoi, Dirghoi, Roshshou, Durghou, Ree, Aa, Oi, Oo, Ou;

MediaPlayer player;

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

AdRequest adRequest = new AdRequest.Builder().build();
interstitial = new InterstitialAd(SorBorno.this);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
}

public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}

public void soreo (View v){
Button button = (Button)findViewById(R.id.Soreo);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.soreo);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void sorea (View v){
Button button = (Button)findViewById(R.id.Sorea);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.sorea);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void roshshoi (View v){
Button button = (Button)findViewById(R.id.Roshshoi);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.roshshoi);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void dirghoi (View v){
Button button = (Button)findViewById(R.id.Dirghoi);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.dirghoi);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void roshshou (View v){
Button button = (Button)findViewById(R.id.Roshshou);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.roshshou);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void durghou (View v){
Button button = (Button)findViewById(R.id.Durghou);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.durghou);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void ree (View v){
Button button = (Button)findViewById(R.id.Ree);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.ree);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void aa (View v){
Button button = (Button)findViewById(R.id.Aa);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.aa);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void oi (View v){
Button button = (Button)findViewById(R.id.Oi);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.oi);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void oo (View v){
Button button = (Button)findViewById(R.id.Oo);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.oo);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void ou (View v){
Button button = (Button)findViewById(R.id.Ou);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){
player = MediaPlayer.create(this, R.raw.ou);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

public void stopPlayer (){
if (player!=null){
player.stop();
player.reset();
player.release();
player=null;
}
}

@Override
protected void onStop (){
super.onStop();
stopPlayer();
}

}

希望我能从这里得到帮助。我只想播放语音,同时点击彼此的按钮。

最佳答案

在初始化新音频或媒体之前,请在您的中调用此方法

public void sorea (View v)
{
Button button = (Button)findViewById(R.id.Sorea);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
button.startAnimation(myAnim);
if (player == null){

//Call the StopPlaying() to release player
stopPlaying();
player = MediaPlayer.create(this, R.raw.sorea);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}

private void stopPlaying() {
if (player != null) {
player.stop();
player.release();
player= null;
}
}

关于java - MediaPlayer 没有在每个按钮同时单击时播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com