简体   繁体   English

Android应用程序上的按钮滞后

[英]Button on android app is lagging

I have a button that plays back an audio file, switches a boolean term, and sets a new text for the button (from play to stop and vice versa). 我有一个按钮,可播放音频文件,切换布尔值并为按钮设置新文本(从播放到停止,反之亦然)。

Unfortunately when I press the play button there is significant lag to make the button say "stop" and there is also lag when I press the button again to stop audio playback. 不幸的是,当我按下“播放”按钮时,使按钮说“停止”会有很大的滞后,而当我再次按下按钮以停止音频播放时,也存在滞后。

I would appreciate any ideas, thanks! 我将不胜感激,谢谢!

public void Playbutton(View view) {
        if (playbuttonstatus) {
            playBtn.setText(getString(R.string.stop));
            playbuttonstatus = false;

            File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

            int shortSizeInBytes = Short.SIZE / Byte.SIZE;

            int bufferSizeInBytes = (int) (file.length() / shortSizeInBytes);
            short[] audioData = new short[bufferSizeInBytes];

            try {
                InputStream inputStream = new FileInputStream(file);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

                int i = 0;
                while (dataInputStream.available() > 0) {
                    audioData[i] = dataInputStream.readShort();
                    i++;
                }

                dataInputStream.close();

                audioTrack = new AudioTrack(
                        AudioManager.STREAM_MUSIC,
                        11025,
                        AudioFormat.CHANNEL_OUT_MONO,
                        AudioFormat.ENCODING_PCM_16BIT,
                        bufferSizeInBytes,
                        AudioTrack.MODE_STREAM);

                audioTrack.play();
                audioTrack.write(audioData, 0, bufferSizeInBytes);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            playBtn.setText(getString(R.string.play));
            playbuttonstatus = true;
            audioTrack.pause();
            audioTrack.flush();
        }
    }

You should use an AsyncTask to perform your sound playing. 您应该使用AsyncTask来执行声音播放。
In onPreExecute , you would set button text and set your boolean flag (main UI thread). onPreExecute ,您将设置按钮文本并设置布尔标志(UI主线程)。
In doInBackground , you will retrieve the sound file and play it (worker thread). doInBackground ,您将检索声音文件并播放它(工作线程)。
If user presses the stop button, cancel current asyncTask and update button text. 如果用户按下停止按钮,则取消当前的asyncTask并更新按钮文本。

You stream your data in UI thread so when you touch button, your app's ui thread locked for moments. 您可以在UI线程中流式传输数据,因此当您触摸按钮时,应用的ui线程会暂时锁定。 You should do your data stream job in another thread. 您应该在另一个线程中执行数据流工作。 You can use Thread or asyncTask for that section of code. 您可以将Thread或asyncTask用于该部分代码。

Avoid doing lot of work in UI Thread you can use Handlers or AsyncTask to accomplish the work ur doing. 避免在UI Thread中进行大量工作,您可以使用Handlers或AsyncTask完成您的工作。 for more information about asyncTask or handlers please refer the link below. 有关asyncTask或处理程序的更多信息,请参考下面的链接。 http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

I used: 我用了:

Thread playThread = new Thread(new Runnable() {
                public void run() {
                    File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

                    int shortSizeInBytes = Short.SIZE / Byte.SIZE;

                    int bufferSizeInBytes = (int) (file.length() / shortSizeInBytes);
                    short[] audioData = new short[bufferSizeInBytes];

                    try {
                        InputStream inputStream = new FileInputStream(file);
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                        DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

                        int i = 0;
                        while (dataInputStream.available() > 0) {
                            audioData[i] = dataInputStream.readShort();
                            i++;
                        }

                        dataInputStream.close();

                        audioTrack = new AudioTrack(
                                AudioManager.STREAM_MUSIC,
                                11025,
                                AudioFormat.CHANNEL_OUT_MONO,
                                AudioFormat.ENCODING_PCM_16BIT,
                                bufferSizeInBytes,
                                AudioTrack.MODE_STREAM);

                        audioTrack.play();
                        audioTrack.write(audioData, 0, bufferSizeInBytes);


                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

                playThread.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM