Android应用程序上的按钮是滞后的

时间:2016-11-20 14:03:50

标签: android button audio

我有一个播放音频文件的按钮,切换一个布尔术语,并为按钮设置一个新文本(从播放到停止,反之亦然)。

不幸的是,当我按下播放按钮时,按钮说“停止”会有明显的延迟,当我再次按下按钮停止音频播放时也会出现延迟。

我很感激任何想法,谢谢!

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();
        }
    }

4 个答案:

答案 0 :(得分:2)

您应该使用#include <iostream> #include <string> #include <fstream> using namespace std; #include <vector> enum ERRORFLAG{INIT, LESS_40, MORE_40, NON_INT}; int main() { ifstream in("data.txt"); char c; string sLine; int value; vector<int> vec; ERRORFLAG erFlag = INIT; while(in >> c) { if(!isspace(c) && !isdigit(c)) { erFlag = NON_INT; break; } } in.clear(); in.seekg(0, ios::beg); // jumping back the the beginning of the file moving the get pointer to the beginning while(in >> value) vec.push_back(value); if(NON_INT == erFlag) cout << "non-int found!" << endl; else { if(vec.size() < 40) cout << "les than 40 integers!" << endl; else if(vec.size() > 40) cout << "more than 40 integers found!" << endl; else for(int i(0); i < vec.size(); i++) cout << vec[i] << ", "; } std::cout << std::endl; return 0; } 来播放声音 在AsyncTask中,您可以设置按钮文本并设置布尔标志(主UI线程) 在onPreExecute中,您将检索声音文件并播放它(工作线程) 如果用户按下停止按钮,则取消当前的asyncTask并更新按钮文本。

答案 1 :(得分:0)

您在UI线程中流式传输数据,因此当您触摸按钮时,您的应用程序的ui线程会暂时锁定。 您应该在另一个线程中执行数据流作业。 您可以对该部分代码使用Thread或asyncTask。

答案 2 :(得分:0)

避免在UI线程中做大量工作,您可以使用Handler或AsyncTask来完成您的工作。有关asyncTask或处理程序的更多信息,请参阅以下链接。 http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

答案 3 :(得分:0)

我用过:

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();