按钮单击android时的Media Player音频

时间:2018-03-20 14:28:44

标签: android android-layout android-fragments

我有一种情况,当我按下图像按钮时,它会突出显示(滤色镜)并播放其音频。我在一个图像中有2个字母,并且在1.mp3文件中包含两个字母的音频。最终我需要当图像按钮单击音频逐个播放时,相应的字母突出显示,一次点击音频。有人在这帮我这个。

case R.id.pg2_4: {

            clearcolor();
            if (mp.isPlaying()) {
                mp.stop();
                clearcolor();
            }
            try {
                pg2_4.setColorFilter(0xff00ff00, PorterDuff.Mode.LIGHTEN);
                mp.reset();
                AssetFileDescriptor afd = null;

                if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 0) {
                    afd = getActivity().getAssets().openFd("q1p2_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 1) {
                    afd = getActivity().getAssets().openFd("q2p2_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 2) {
                    afd = getActivity().getAssets().openFd("q3p2_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 3) {
                    afd = getActivity().getAssets().openFd("q4p2_1.mp3");
                }
                mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    clearcolor();
                }
            });
            break ;
        }
        case R.id.pg2_5: {

            if (mp.isPlaying()) {
                mp.stop();
                clearcolor();
            }
            try {
                pg2_5.setColorFilter(0xff00ff00, PorterDuff.Mode.LIGHTEN);
                mp.reset();
                AssetFileDescriptor afd = null;

                if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 0) {
                    afd = getActivity().getAssets().openFd("q1p2_2.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 1) {
                    afd = getActivity().getAssets().openFd("q2p2_2.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 2) {
                    afd = getActivity().getAssets().openFd("q3p2_2.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 3) {
                    afd = getActivity().getAssets().openFd("q4p2_2.mp3");
                }
                mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    clearcolor();
                }
            });
            break ;
        }

这是onclick代码

0 个答案:

没有答案