音乐按钮问题

时间:2017-06-20 14:49:07

标签: android button android-music-player

我的按钮有问题。我有31个不同的mp3文件。我添加了31个按钮。当我按下第29个按钮时30和31不能在我的手机中工作。模拟器工作正常。我还有一个问题,当我进入主动,然后再次加入我的mp3页面时,有时候并没有按下所有按钮。

public class voice extends AppCompatActivity {

Button bt;

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

        bt = (Button) findViewById(R.id.button);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.voice1);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });


        bt = (Button) findViewById(R.id.button2);
        final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.voice2);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp2.start();
            }
        });

        bt = (Button) findViewById(R.id.button3);
        final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.voice3);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp3.start();
            }
        });

        bt = (Button) findViewById(R.id.button4);
        final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.voice4);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp4.start();
            }
        });

        bt = (Button) findViewById(R.id.button5);
        final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.voice5);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp5.start();
            }
        });

        bt = (Button) findViewById(R.id.button6);
        final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.voice6);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp6.start();
            }
        });

        bt = (Button) findViewById(R.id.button7);
        final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.voice7);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp7.start();
            }
        });

        bt = (Button) findViewById(R.id.button8);
        final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.voice8);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp8.start();
            }
        });

        bt = (Button) findViewById(R.id.button9);
        final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.voice9);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp9.start();
            }
        });

        bt = (Button) findViewById(R.id.button10);
        final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.voice10);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp10.start();
            }
        });

        bt = (Button) findViewById(R.id.button11);
        final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.voice11);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp11.start();
            }
        });

        bt = (Button) findViewById(R.id.button12);
        final MediaPlayer mp12 = MediaPlayer.create(this, R.raw.voice12);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp12.start();
            }
        });

        bt = (Button) findViewById(R.id.button13);
        final MediaPlayer mp13 = MediaPlayer.create(this, R.raw.voice13);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp13.start();
            }
        });

        bt = (Button) findViewById(R.id.button14);
        final MediaPlayer mp14 = MediaPlayer.create(this, R.raw.voice14);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp14.start();
            }
        });

        bt = (Button) findViewById(R.id.button15);
        final MediaPlayer mp15 = MediaPlayer.create(this, R.raw.voice15);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp15.start();
            }
        });

        bt = (Button) findViewById(R.id.button16);
        final MediaPlayer mp16 = MediaPlayer.create(this, R.raw.voice16);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp16.start();
            }
        });

        bt = (Button) findViewById(R.id.button17);
        final MediaPlayer mp17 = MediaPlayer.create(this, R.raw.voice17);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp17.start();
            }
        });

        bt = (Button) findViewById(R.id.button18);
        final MediaPlayer mp18 = MediaPlayer.create(this, R.raw.voice18);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp18.start();
            }
        });

        bt = (Button) findViewById(R.id.button19);
        final MediaPlayer mp19 = MediaPlayer.create(this, R.raw.voice19);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp19.start();
            }
        });
        bt = (Button) findViewById(R.id.button20);
        final MediaPlayer mp20 = MediaPlayer.create(this, R.raw.voice20);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp20.start();
            }
        });

        bt = (Button) findViewById(R.id.button21);
        final MediaPlayer mp21 = MediaPlayer.create(this, R.raw.voice21);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp21.start();
            }
        });

        bt = (Button) findViewById(R.id.button22);
        final MediaPlayer mp22 = MediaPlayer.create(this, R.raw.voice22);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp22.start();
            }
        });

        bt = (Button) findViewById(R.id.button23);
        final MediaPlayer mp23 = MediaPlayer.create(this, R.raw.voice23);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp23.start();
            }
        });

        bt = (Button) findViewById(R.id.button24);
        final MediaPlayer mp24 = MediaPlayer.create(this, R.raw.voice24);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp24.start();
            }
        });

        bt = (Button) findViewById(R.id.button25);
        final MediaPlayer mp25 = MediaPlayer.create(this, R.raw.voice25);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp25.start();
            }
        });

        bt = (Button) findViewById(R.id.button26);
        final MediaPlayer mp26 = MediaPlayer.create(this, R.raw.voice26);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp26.start();
            }
        });

        bt = (Button) findViewById(R.id.button27);
        final MediaPlayer mp27 = MediaPlayer.create(this, R.raw.voice27);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp27.start();
            }
        });

        bt = (Button) findViewById(R.id.button28);
        final MediaPlayer mp28 = MediaPlayer.create(this, R.raw.voice28);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp28.start();
            }
        });

        bt = (Button) findViewById(R.id.button29);
        final MediaPlayer mp29 = MediaPlayer.create(this, R.raw.voice29);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp29.start();
            }
        });

        bt = (Button) findViewById(R.id.button30);
        final MediaPlayer mp30 = MediaPlayer.create(this, R.raw.voice30);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp30.start();
            }
        });

        bt = (Button) findViewById(R.id.button31);
        final MediaPlayer mp31 = MediaPlayer.create(this, R.raw.voice31);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp31.start();
            }
        });


    }



}
}

1 个答案:

答案 0 :(得分:1)

您可以查看Media Player docs

这个想法是,你不需要创建你的玩家。只使用一个媒体播放器,但在使用下一个数据之前发布以前的数据:

+------------+---------+----------+
| DateField  | StringA | StringB  |
+------------+---------+----------+
| 01/01/2015 | xxx     | abc123   |
| 02/01/2015 | zzz     | abc12345 |
| 03/01/2015 | xxx     | abc      |
+------------+---------+----------+

您的代码:

 if (mplayer !=null) {
     mplayer.stop();
     mplayer.release();
     mplayer = null;
 }
 mplayer = new  MediaPlayer();
 mplayer.setDataSource(getApplicationContext(),
                  Uri.parse(CONSTANTS.RES_PREFIX + R.raw.id));
 mplayer.prepare();
 mplayer.start();

}

看看onClick。您需要为每个按钮创建路径并将其发送到startNewVoice方法。不要忘记批准它是否为你工作