为什么回调总是为空?

时间:2019-10-16 16:49:40

标签: android android-asynctask audiorecord continuous

我想在录制时(在AsyncTask中)获取音频缓冲区,并在视图打开时将其可视化,但是,如果这样做是正确的方法,我不是。在以下实现中,回调始终为null,不确定哪里出了问题。

这是活动片段:

public class MainPageFragment extends Fragment implements Observer {

    private AudioRecordingDbmHandler visualizerHandler;
    private GLAudioVisualizationView visualizerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_main_page, container, false);

        visualizerView = new GLAudioVisualizationView.Builder(getContext())

        rootView.setBackgroundColor(Util.getDarkerColor(color));
        rootView.addView(visualizerView, 0);

        return rootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        audioRecord = new AudioRecord();
        visualizerHandler = new AudioRecordingDbmHandler();
        audioRecord.callback(visualizerHandler);
        audioRecord.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        visualizerView.linkTo(visualizerHandler);

    }

    public static class AudioRecordingDbmHandler extends DbmHandler<byte[]> implements AudioRecord.RecordingCallback {
        @Override
        protected void onDataReceivedImpl(byte[] bytes, int layersCount, float[] dBmArray, float[] ampsArray) {...}

        public void stop() {
            calmDownAndStopRendering();
        }

        @Override
        public void onDataReady(byte[] data) {
            onDataReceived(data);
        }
    }

}

这是录音:

public class AudioRecord extends AsyncTask <Void, double[], Void> {

    public interface RecordingCallback {
        void onDataReady(byte[] data);
    }

    private RecordingCallback recordingCallback = null;

    public AudioRecord callback(RecordingCallback recordingCallback) {
        this.recordingCallback = recordingCallback;
        return this;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
        ...
        byteBuffer = new byte[bufferSize];
        ...
        record.startRecording();

        while (true) {

            bytesRead = record.read(byteBuffer, 0, bufferSize);

            try {
                if (recordingCallback != null) {
                    recordingCallback.onDataReady(byteBuffer);       
                }
            } finally {
                recordingBufferLock.unlock();
            }
        }
        record.stop();
        record.release();
        return null;
    }
}

也尝试使用onProgressUpdate导致相同的问题。在这里寻求帮助会很棒。

1 个答案:

答案 0 :(得分:0)

更改此:

public void setCallback(RecordingCallback recordingCallback) {
    this.recordingCallback = recordingCallback;
}

对此:

const pqr = [
  { name: "R", age: "20" },
  { name: "S", age: "30" },
  { name: "R", age: "18" }
];

const xyz = [3, 1, 2];

const sorted = pqr.slice().sort((a, b) => a.age - b.age);

const according = xyz.map(position => sorted[position - 1]);

const result = according.map(item => ({ [item.name]: item.age }));

console.log(result);

我不确定它是否会起作用,但是由于您像设置器一样使用回调方法,因此不需要返回此值。

如果无法帮助您,请随时投票:)