Android声音循环

时间:2016-12-22 08:03:05

标签: android audio time camera-flash

我正在开发一个将摩尔斯电码转换为声音和闪烁的Android应用程序。闪光灯部分效果很好,但我有声音部分的麻烦。

这是我到目前为止的代码:

SoundPool sp =new SoundPool.Builder().setMaxStreams(1).build(); // declare before onCreate()
int soundId = sp.load(this,R.raw.beep,1); // declared inside onCreate()

public void flashCode(View view){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {

        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},0);
        }
    }

    int dotTime=150;  // in milliseconds
    long ms=System.currentTimeMillis();
    long ms2=System.currentTimeMillis();

    //
    //int streamID=mSoundPool.load(this,R.raw.beep,1);
    for (int i=0;i<code.length();i++)
    {
        streamID=sp.play(soundId,1,1,1,1,1.0f);
        sp.pause(streamID);
        if (String.valueOf(code.charAt(i)).contentEquals("."))
        {
            String cameraId = null; // Usually front camera is at 0 position.
            try {
                cameraId = camManager.getCameraIdList()[0];

                ms=System.currentTimeMillis();
                ms2=System.currentTimeMillis()+dotTime;

                camManager.setTorchMode(cameraId, true);
                //mSoundPool.resume(streamID);

                while (ms<ms2){
                    ms=System.currentTimeMillis();
                }
                camManager.setTorchMode(cameraId, false);
                //mSoundPool.stop(streamID);

            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
        if (String.valueOf(code.charAt(i)).contentEquals("-"))
        {
            String cameraId = null; // Usually front camera is at 0 position.
            try {
                cameraId = camManager.getCameraIdList()[0];

                ms=System.currentTimeMillis();
                ms2=System.currentTimeMillis()+dotTime*3;

                camManager.setTorchMode(cameraId, true);
                //mSoundPool.resume(streamID);

                while (ms<ms2){
                    ms=System.currentTimeMillis();
                }
                camManager.setTorchMode(cameraId, false);
                //mSoundPool.stop(streamID);

            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
        if (String.valueOf(code.charAt(i)).contentEquals(" "))
        {
            String cameraId = null; // Usually front camera is at 0 position.
            try {
                cameraId = camManager.getCameraIdList()[0];

                ms=System.currentTimeMillis();
                ms2=System.currentTimeMillis()+dotTime;

                camManager.setTorchMode(cameraId, false);

                while (ms<ms2){
                    ms=System.currentTimeMillis();
                }


            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

我想要的是当我打开和关闭闪光灯时播放声音,但仅在此期间播放。我尝试使用SoundPool但是当我测试它时,闪光和声音没有同步,MediaPlayer它甚至跳过了一些声音。

你能告诉我我做错了什么或者告诉我你会怎么做。

0 个答案:

没有答案