onCompletion侦听器用于交换机之间

时间:2014-08-05 20:22:37

标签: java android android-mediaplayer

我有一个问题,我认为我可以看到解决方案,但我无法解决如何实施它。我对整场比赛都很陌生。

我正在构建一个将会阅读"输入的任何数字。到目前为止,我已经让应用程序获取一个输入的数字并将其转换为4位数字符串,然后将其添加到数组中。计划是使用它来为每个数字运行切换事件,因此如果第一个数字是1,它将播放"一个"声音剪辑后跟"千"声音片段。我有点工作,但是当我试图让第二个数字通过一个开关时(例如"一个"然后是"百")我得到第二个批次第一个播放的声音片段。我需要做的是让onCompletionListener在这两个开关之间工作;我已经看到这个问题似乎在问同样的事情,但我无法解决如何使用答案...

How to get an onCompletion handler from other class?

如果你想看看我一直在做什么,这是我的代码。

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class Number_Reader extends Activity {

TextView readNumberTV;
TextView numberLengthTV;
int currentLength;

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

    readNumberTV = (TextView) findViewById(R.id.read_input);
    numberLengthTV = (TextView) findViewById(R.id.number_length);

}

public void btn_zero_click(View view) {
    add("0");}
public void btn_one_click(View view) {
    add("1");}
public void btn_two_click(View view) {
    add("2");}
public void btn_three_click(View view) {
    add("3");}
public void btn_four_click(View view) {
    add("4");}
public void btn_five_click(View view) {
    add("5");}
public void btn_six_click(View view) {
    add("6");}
public void btn_seven_click(View view) {
    add("7");}
public void btn_eight_click(View view) {
    add("8");}
public void btn_nine_click(View view) {
    add("9");}

public void add(String num)
{
    currentLength++;
    readNumberTV.append(num);
}

public void onSendRead(View view) {

    MediaPlayer one = MediaPlayer.create(Number_Reader.this, R.raw.one);
    MediaPlayer two = MediaPlayer.create(Number_Reader.this, R.raw.two);
    MediaPlayer three = MediaPlayer.create(Number_Reader.this, R.raw.three);
    MediaPlayer four = MediaPlayer.create(Number_Reader.this, R.raw.four);
    MediaPlayer five = MediaPlayer.create(Number_Reader.this, R.raw.five);
    MediaPlayer six = MediaPlayer.create(Number_Reader.this, R.raw.six);
    MediaPlayer seven = MediaPlayer.create(Number_Reader.this, R.raw.seven);
    MediaPlayer eight = MediaPlayer.create(Number_Reader.this, R.raw.eight);
    MediaPlayer nine = MediaPlayer.create(Number_Reader.this, R.raw.nine);

    int n = Integer.parseInt(readNumberTV.getText().toString());

    String newNumber = String.format("%04d", n);

    char[] digits = newNumber.toCharArray();

    String str1 = digits[0] + "";
    String str2 = digits[1] + "";
    String str3 = digits[2] + "";
    String str4 = digits[3] + "";

    int foo1 = Integer.parseInt(str1);
    int foo2 = Integer.parseInt(str2);
    int foo3 = Integer.parseInt(str3);
    int foo4 = Integer.parseInt(str4);

    switch (foo1) {
        case 1:
            one.start();
            one.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 2:
            two.start();
            two.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 3:
            three.start();
            three.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 4:
            four.start();
            four.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 5:
            five.start();
            five.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 6:
            six.start();
            six.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 7:
            seven.start();
            seven.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 8:
            eight.start();
            eight.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;
        case 9:
            nine.start();
            nine.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                MediaPlayer thousand = MediaPlayer.create(Number_Reader.this, R.raw.thousand);
                    thousand.start();
                }
            });
            break;

    }

    switch (foo2) {
        case 1:
            one.start();
            one.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 2:
            two.start();
            two.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 3:
            three.start();
            three.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 4:
            four.start();
            four.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 5:
            five.start();
            five.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 6:
            six.start();
            six.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 7:
            seven.start();
            seven.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 8:
            eight.start();
            eight.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
        case 9:
            nine.start();
            nine.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MediaPlayer hundred = MediaPlayer.create(Number_Reader.this, R.raw.hundred);
                    hundred.start();
                }
            });
            break;
    }
}



public void onSendClear(View view) {
    readNumberTV.setText(null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这可能不是最好的方式(事实上它可能不是)如果有人对此有任何建议我喜欢听。

另外,我非常确定我没有得到最简洁的代码(此时我很高兴,如果它有效),如果有人想对我提供任何指示,那么我也很感激。

对不起,这有点罗嗦,但我想确保我很清楚。

1 个答案:

答案 0 :(得分:0)

如果你编写一个递归函数,问题就可以解决,因为一个数字的公告只在前一个数字完成之后才会发生。可以使用以下方法 -

  private void announceNumber(int input){
      int numberOfDigits = countNumberOfDigits(input);
      if(numberOfDigits>1){
        int currentDigitToBeAnnounced = (input/Math.pow(10,(numberOfDigits-1)));
        int nexIterationNumber = (input%Math.pow(numberOfDigits-1));
        // The next two announce calls can be synchronized by putting them
        // in the same method
        announcedCurrentNumber(currentDigitToBeAnnounced);
        announceMultiplier(numberOfDigits);
        // make the recursive call made here in the method that you
        // use to make the sound.
        announceNumber(nextIterationNumber);
      }else{
        announceNumber(input);
      }
  }