Anna  Hughes
Anna Hughes

Reputation: 25

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

Upvotes: 0

Views: 116

Answers (4)

Anna  Hughes
Anna Hughes

Reputation: 25

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

Upvotes: 0

vijay surya
vijay surya

Reputation: 135

Avoid doing lot of work in UI Thread you can use Handlers or AsyncTask to accomplish the work ur doing. for more information about asyncTask or handlers please refer the link below. http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

Upvotes: 0

Reza.Abedini
Reza.Abedini

Reputation: 2257

You stream your data in UI thread so when you touch button, your app's ui thread locked for moments. You should do your data stream job in another thread. You can use Thread or asyncTask for that section of code.

Upvotes: 0

HelloSadness
HelloSadness

Reputation: 955

You should use an AsyncTask to perform your sound playing.
In onPreExecute, you would set button text and set your boolean flag (main UI thread).
In doInBackground, you will retrieve the sound file and play it (worker thread).
If user presses the stop button, cancel current asyncTask and update button text.

Upvotes: 2

Related Questions