English 中文(简体)
Android的AsyncTask -- -- 音频录音时UI屏蔽
原标题:AsyncTask in Android - UI blocking during audio recording

我搜索过很多次, 我无法找到解决这个问题的办法。 我将创建一个实时处理音频的应用程序。 因此, 我创建了一个 Asynctask, 初始化麦克风, 处理收到的音频 :

public class SoundSampler extends AsyncTask<Void, Void, Void>{

AudioRecord recorder;
int bufferSize;
final short[] audioData;

public SoundSampler() {
    int samplerate = 44100;
    int encoding = AudioFormat.ENCODING_PCM_16BIT;
    int config = AudioFormat.CHANNEL_IN_MONO;
    bufferSize = AudioRecord.getMinBufferSize(samplerate, config, encoding);
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, samplerate, config, encoding, bufferSize);

    final int finalBufferSize = bufferSize;
    audioData = new short[finalBufferSize];             

    OnRecordPositionUpdateListener positionUpdater = new OnRecordPositionUpdateListener() {
        @Override
        public void onPeriodicNotification(AudioRecord recorder) {
        // do something amazing with audio data (long operations)
        }

        @Override
        public void onMarkerReached(AudioRecord recorder) {                            Log.d("Process", "marker reached");
        }
    };
    recorder.setRecordPositionUpdateListener(positionUpdater);
    recorder.setPositionNotificationPeriod(64);
    Log.d("Process", "starTAGt recording, bufferSize: " + bufferSize);
    Log.d("Process", "state = " + recorder.getState() + " aud = " + audioData.length);
    }

    @Override
    protected Void doInBackground(Void... params) {
        recorder.startRecording();
        Log.d("Process", "rec = " + recorder.getRecordingState());
        while (!isCancelled()) {
             recorder.read(audioData, 0, bufferSize);
        }
        recorder.stop();
        return null;
    }

}

这是我在Create的主要活动:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button on = (Button) findViewById(R.id.on);
        Button off = (Button) findViewById(R.id.off);

        final SoundSampler smp =  new SoundSampler();

        on.setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(View v) {
                 smp.execute();
            }
        });

        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smp.cancel(true);
            }
        });


    }

现在,声音已经注册了, 但是在我开始音频Sampler Asynctask 之后, Ui 就会被封住。 我希望音频Sampler 注册数据, 但我希望他停止, 正如我想从主界面中停止那样。 您对这样做有什么建议吗?

问题回答

on Recordpotition UpdateListener 方法被调用到 UI 线索上。 您可以在听众方法中插入以下日志语句来确认 :

Log.i(TAG, "OnRecordPositionUpdateListener on thread: " + Thread.currentThread().getName());

您必须生成一条新线, 才能在 on Periodic通知 (AudioRecord record recorder) / code 内执行您的长期操作 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签