我已经查看了这个问题变体的所有建议答案,但没有一个有帮助。我的应用程序应该教摩尔斯电码。唯一不工作的部分是 Player.java 文件,它应该采用莫尔斯“字母”对象,读取其代码,并播放一系列音调(以 .mp3 格式存储在 res.raw 文件夹中)。这段代码的相关位是 playSound(int pfile, Context pcontext) 函数。这里是:
package com.ewg.morseCode;
import java.io.IOException;
import com.ewg.morseCode.morse_alphabet.letter;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
public class Player extends Activity{
public static boolean finished = false;
public static class player{
public int dit;
public int dah;
public player(){
this.dit = R.raw.morse_dit;
this.dah = R.raw.morse_dah;
}
//////PERTINENT BIT//////
private void playSound(int pfile, Context pcontext){
final int file = pfile;
final Context context = pcontext;
MediaPlayer mp =MediaPlayer.create(context, file);
mp.start();
while(mp.isPlaying()){
}
mp.reset();
mp.release();
mp = null;
}
//////END PERTINENT BIT//////
public void pause(int dur){
finished = false;
CountDownTimer timer1 = new CountDownTimer(dur, dur){
public void onTick(long blah){
}
public void onFinish(){
finished = true;
}
}.start();
while(finished = false){
}
}
public void play(letter x, Context context) throws Exception{
Exception e = new Exception();
for(int i = 0; i< x.code.length-1; i++){
switch(x.code[i]){
case 0:
playSound(dit, context);
pause(49);
break;
case 1:
playSound(dah, context);
pause(49);
break;
default:
throw(e);
}
}
}
}
}
对不起,任何糟糕的编码,我仍然只是一个脚本小子。
编辑这是调用播放器类的代码,@Geobits
player player = new player();
letter Letter = morse_alphabet.hardLetters.get(0);//Letter has a String name property of "b" and a int[] code property of {1, 0, 0, 0}
try {
player.play(Letter, context);//context =main_activity.this
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}