0

我已经查看了这个问题变体的所有建议答案,但没有一个有帮助。我的应用程序应该教摩尔斯电码。唯一不工作的部分是 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();
    }
4

4 回答 4

0

首先,我会支持您应该使用 OnCompletionListener 来释放您的播放器或至少在您的活动 onPause 中执行此操作的所有建议。

有一次我的程序启动了几个播放器并且没有发布(泄露),所以当我再次尝试运行时它无法创建新播放器。我不确定它是否对你来说是一样的,但是手机重启解决了我的问题,我的代码没有任何变化(我试图杀死我的应用程序和其他服务,但它没有帮助)。

重要的旁注:

  • 您不需要提前创建异常对象,因为这很浪费内存。抛出异常时只需创建异常对象。

例如

 throw(new Exception("Unsupported command")); // Also you should provide a descriptive message

您的代码中还有一个错误

 while(finished = false) // it should be ==

或者更好,你应该使用

 while(!finished)
于 2012-12-29T04:33:46.863 回答
0

尝试像这样更改您的 playSound 功能,不要忘记打开音量

private void playSound(int pfile, Context pcontext){

    final int file = pfile;
    final Context context = pcontext;
    MediaPlayer mp = MediaPlayer.create(context, file);
    mp.start();
    mp.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.release();
        }

    });
}
于 2012-12-29T02:26:11.387 回答
0

您可能应该使用 anOnCompletionListener而不是空的 while 循环。它可能无法解决您的问题,但肯定不会受到伤害,并且是更正确的方法。

//////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();
    mp.setOnCompletionListener(this);
}

public void onCompletion(MediaPlayer mp)
{
    mp.release();
}

//////END PERTINENT BIT//////
于 2012-12-29T02:26:31.590 回答
0

一个糟糕的解决方案,......但有用的解决方案

MediaPlayer mp = MediaPlayer.create(context, file);
//write these after
mp.start();
mp.pause();
于 2018-10-05T13:02:38.133 回答