首页 > 解决方案 > 安卓多声音媒体播放器

问题描述

我有资产文件夹。我在设计中有按钮,此代码用于在单击按钮时播放声音

我正在尝试做;我单击第一个按钮并播放第一个声音,然后单击第二个按钮并播放第二个声音但是这样做;我单击第一个按钮并播放最后一个声音,然后单击第二个按钮并停止代码

按钮在设计屏幕上可用。按下按钮时,将播放声音。我的媒体播放器有问题。我第一次按下任何按钮时,最后一个动作是在案例块中完成的,第二次程序关闭时,

在这里我怀疑媒体播放器对象卡在了最新的任务中,但我不知道如何解决它。

请帮我!

    package com.example.harf;

import androidx.appcompat.app.AppCompatActivity;


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    MediaPlayer mP1,mP2,mP3;
    AssetFileDescriptor asset;
    List<Button> list= new ArrayList<Button>();
    int flag=0;




    Button btn_harfKelimeCumle_A,btn_harfKelimeCumle_B,btn_harfKelimeCumle_C,btn_harfKelimeCumle_D;
    Button btn_harfKelimeCumle_E,btn_harfKelimeCumle_F,btn_harfKelimeCumle_G,btn_harfKelimeCumle_H;
    Button btn_harfKelimeCumle_I,btn_harfKelimeCumle_J,btn_harfKelimeCumle_K,btn_harfKelimeCumle_L;
    Button btn_harfKelimeCumle_M,btn_harfKelimeCumle_N,btn_harfKelimeCumle_O,btn_harfKelimeCumle_P;
    Button btn_harfKelimeCumle_R,btn_harfKelimeCumle_S,btn_harfKelimeCumle_T,btn_harfKelimeCumle_U;
    Button btn_harfKelimeCumle_V,btn_harfKelimeCumle_Y,btn_harfKelimeCumle_Z;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

       // mP2=MediaPlayer.create( this,R. );
        //mP3=MediaPlayer.create( this,R.raw.C );

        tanimla();

        mP1=new MediaPlayer();

    }



    public void tanimla()
    {
        btn_harfKelimeCumle_A=findViewById( R.id.btn_harfKelimeCumle_A );
        btn_harfKelimeCumle_B=findViewById( R.id.btn_harfKelimeCumle_B );
        btn_harfKelimeCumle_C=findViewById( R.id.btn_harfKelimeCumle_C );
        btn_harfKelimeCumle_D=findViewById( R.id.btn_harfKelimeCumle_D );
        btn_harfKelimeCumle_E=findViewById( R.id.btn_harfKelimeCumle_E );
        btn_harfKelimeCumle_F=findViewById( R.id.btn_harfKelimeCumle_F );
        btn_harfKelimeCumle_G=findViewById( R.id.btn_harfKelimeCumle_G );
        btn_harfKelimeCumle_H=findViewById( R.id.btn_harfKelimeCumle_H );
        btn_harfKelimeCumle_I=findViewById( R.id.btn_harfKelimeCumle_I );
        btn_harfKelimeCumle_J=findViewById( R.id.btn_harfKelimeCumle_J );
        btn_harfKelimeCumle_K=findViewById( R.id.btn_harfKelimeCumle_K );
        btn_harfKelimeCumle_L=findViewById( R.id.btn_harfKelimeCumle_L );
        btn_harfKelimeCumle_M=findViewById( R.id.btn_harfKelimeCumle_M );
        btn_harfKelimeCumle_N=findViewById( R.id.btn_harfKelimeCumle_N );
        btn_harfKelimeCumle_O=findViewById( R.id.btn_harfKelimeCumle_O );
        btn_harfKelimeCumle_P=findViewById( R.id.btn_harfKelimeCumle_P );
        btn_harfKelimeCumle_R=findViewById( R.id.btn_harfKelimeCumle_R );
        btn_harfKelimeCumle_S=findViewById( R.id.btn_harfKelimeCumle_S );
        btn_harfKelimeCumle_T=findViewById( R.id.btn_harfKelimeCumle_T );
        btn_harfKelimeCumle_U=findViewById( R.id.btn_harfKelimeCumle_U );
        btn_harfKelimeCumle_V=findViewById( R.id.btn_harfKelimeCumle_V );
        btn_harfKelimeCumle_Y=findViewById( R.id.btn_harfKelimeCumle_Y );
        btn_harfKelimeCumle_Z=findViewById( R.id.btn_harfKelimeCumle_Z );

        list.add( btn_harfKelimeCumle_A);
        list.add( btn_harfKelimeCumle_B);
        list.add( btn_harfKelimeCumle_C);
        list.add( btn_harfKelimeCumle_D);
        list.add( btn_harfKelimeCumle_E);
        list.add( btn_harfKelimeCumle_F);
        list.add( btn_harfKelimeCumle_G);
        list.add( btn_harfKelimeCumle_H);
        list.add( btn_harfKelimeCumle_I);
        list.add( btn_harfKelimeCumle_J);
        list.add( btn_harfKelimeCumle_K);
        list.add( btn_harfKelimeCumle_L);
        list.add( btn_harfKelimeCumle_M);
        list.add( btn_harfKelimeCumle_N);
        list.add( btn_harfKelimeCumle_O);
        list.add( btn_harfKelimeCumle_P);
        list.add( btn_harfKelimeCumle_R);
        list.add( btn_harfKelimeCumle_S);
        list.add( btn_harfKelimeCumle_T);
        list.add( btn_harfKelimeCumle_U);
        list.add( btn_harfKelimeCumle_V);
        list.add( btn_harfKelimeCumle_Z);

        mP1=new MediaPlayer();

        for (int i=0;i<list.size();i++)
        {

            flag=i;


            try {
                if (mP1!= null) {
                    mP1.stop();
                    mP1.release();
                    mP1 = null;

                    mP1=new MediaPlayer();

                }


            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            list.get( i ).setOnClickListener( new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    switch (flag)
                    {
                        case 0:
                        try
                        {
                            asset=getAssets().openFd("Aharf.mp3");
                        }
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                            break;
                        case 1:
                            try
                            {
                                asset=getAssets().openFd("Bharf.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 2:
                            try
                            {
                                asset=getAssets().openFd("Charf.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 3:
                            try
                            {
                                asset=getAssets().openFd("D.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 4:
                            try
                            {
                                asset=getAssets().openFd("Eharf.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 5:
                            try
                            {
                                asset=getAssets().openFd("F.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 6:
                            try
                            {
                                asset=getAssets().openFd("G.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 7:
                            try
                            {
                                asset=getAssets().openFd("H.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 8:
                            try
                            {
                                asset=getAssets().openFd("I.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 9:
                            try
                            {
                                asset=getAssets().openFd("J.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 10:
                            try
                            {
                                asset=getAssets().openFd("K.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 11:
                            try
                            {
                                asset=getAssets().openFd("L.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 12:
                            try
                            {
                                asset=getAssets().openFd("M.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 13:
                            try
                            {
                                asset=getAssets().openFd("N.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 14:
                            try
                            {
                                asset=getAssets().openFd("O.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 15:
                            try
                            {
                                asset=getAssets().openFd("P.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 16:
                            try
                            {
                                asset=getAssets().openFd("R.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 17:
                            try
                            {
                                asset=getAssets().openFd("S.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 18:
                            try
                            {
                                asset=getAssets().openFd("T.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 19:
                            try
                            {
                                asset=getAssets().openFd("U.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 20:
                            try
                            {
                                asset=getAssets().openFd("V.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 21:
                            try
                            {
                                asset=getAssets().openFd("Y.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                        case 22:
                            try
                            {
                                asset=getAssets().openFd("Z.mp3");
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            break;
                    }

                    try {
                        mP1.setDataSource( asset.getFileDescriptor(),asset.getStartOffset(),asset.getLength() );
                        mP1.prepare();
                        mP1.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
            } );
        }



    }




}

标签: android

解决方案


您的 MainActivity 有许多不必要且重复的代码。您不需要列表来存储按钮,也不需要创建这么多 mediaPlayer 对象。我已经简化了你的代码。

package com.example.harf;

import androidx.appcompat.app.AppCompatActivity;


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    MediaPlayer mP1;
    AssetFileDescriptor asset;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

    }


    public void playMusic(View w){
        if (mP1!= null) {
            mP1.stop();
            mP1.release();
        }
        else
        {

            mP1=new MediaPlayer();
        }
       switch(view.getId){
       case R.id.btn_harfKelimeCumle_A:
            asset=getAssets().openFd("Aharf.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_B:
            asset=getAssets().openFd("Bharf.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_C:
            asset=getAssets().openFd("Charf.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_D:
            asset=getAssets().openFd("D.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_E:
            asset=getAssets().openFd("Aharf.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_F:
            asset=getAssets().openFd("F.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_G:
            asset=getAssets().openFd("G.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_H:
            asset=getAssets().openFd("H.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_I:
            asset=getAssets().openFd("I.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_J:
            asset=getAssets().openFd("J.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_K:
            asset=getAssets().openFd("K.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_L:
            asset=getAssets().openFd("L.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_M:
            asset=getAssets().openFd("M.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_N:
            asset=getAssets().openFd("N.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_O:
            asset=getAssets().openFd("O.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_P:
            asset=getAssets().openFd("P.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_R:
            asset=getAssets().openFd("R.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_S:
            asset=getAssets().openFd("S.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_T:
            asset=getAssets().openFd("T.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_U:
            asset=getAssets().openFd("U.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_V:
            asset=getAssets().openFd("V.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_Y:
            asset=getAssets().openFd("Y.mp3");
            break; 
       case R.id.btn_harfKelimeCumle_Z:
            asset=getAssets().openFd("Z.mp3");
            break; 
        }
        mP1.setDataSource( asset.getFileDescriptor(),asset.getStartOffset(),asset.getLength() );
        mP1.prepare();
        mP1.start();

    }
}

现在您所要做的就是在每个按钮上添加 onClick 属性。

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_id"
        onClick="playMusic" //like this
        />

推荐阅读