AudioTrack: should I use an Asynctask, a Thread or

2019-03-07 03:05发布

In Android: I am trying to play a wav file of size 230mb and 20 min whose properties are as below:

ffmpeg -i 1.wav

Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s

The following is the code in android: float volchange=0.5f; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    // Code to set the volume change variable for the audiotrack


    Button volup = (Button) findViewById(R.id.volup);
    Button voldown = (Button) findViewById(R.id.voldown);

    volup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            volchange = volchange+0.1f;
        }
    });

    voldown.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            volchange = volchange-0.1f;
        }
    });

    // Code to play the Audio track on pressing the button playraw


    Button playraw = (Button) findViewById(R.id.playraw);

    playraw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int frequency = 44100;
            int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
            int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

            int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration,audioEncoding);

            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize,
                    AudioTrack.MODE_STREAM);

            int count = 0;
            byte[] data = new byte[bufferSize];
            byte[] datavol = new byte[bufferSize];

            try{
                FileInputStream fileInputStream = new FileInputStream(listMusicFiles.get(0).listmusicfiles_fullfilepath);
                DataInputStream dataInputStream = new DataInputStream(fileInputStream);
                audioTrack.play();

                while((count = dataInputStream.read(data, 0, bufferSize)) > -1){
                    for (int i=0;i<data.length;i++){

                        // *** using the global variable change volume ***

                        datavol[i] = (byte)((float) data[i]*volchange);
                    }
                    audioTrack.write(datavol, 0, count);
                }

                audioTrack.stop();
                audioTrack.release();
                dataInputStream.close();
                fileInputStream.close();
            }
            catch (FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    })
}

I want to adjust the volume while its playing. But because the main thread is being used by Auditrack my volup and voldown button are not able to change the volume.

I have play the audio in a seprate thread.

I heard Asynctask is only used for short span things. And in threads i cant access UI

Can someone suggest which to use so that if i change the volume it should also change the volume in the audiotrack stream bytes.

1条回答
疯言疯语
2楼-- · 2019-03-07 03:46

I have created a thread and put the audiotrack related code inside that. and the changes of volup and voldown are immediately applied.

the final working code:

public class MainActivity extends AppCompatActivity {

    float volchange=0.5f;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Code to set the volume change variable for the audiotrack


        Button volup = (Button) findViewById(R.id.volup);
        Button voldown = (Button) findViewById(R.id.voldown);

        volup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                volchange = volchange+0.1f;
            }
        });

        voldown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                volchange = volchange-0.1f;
            }
        });

        // Code to play the Audio track on pressing the button playraw


        Button playraw = (Button) findViewById(R.id.playraw);

        playraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startplaying();
            }
        });
    }

    public void startplaying() {
        // do something long
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int frequency = 44100;
                int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
                int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

                int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration,audioEncoding);

                AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
                        channelConfiguration, audioEncoding, bufferSize,
                        AudioTrack.MODE_STREAM);

                int count = 0;
                byte[] data = new byte[bufferSize];
                byte[] datavol = new byte[bufferSize];

                try{
                    FileInputStream fileInputStream = new FileInputStream(listMusicFiles.get(0).listmusicfiles_fullfilepath);
                    DataInputStream dataInputStream = new DataInputStream(fileInputStream);
                    audioTrack.play();

                    while((count = dataInputStream.read(data, 0, bufferSize)) > -1){
                        for (int i=0;i<data.length;i++){

                            // *** using the global variable change volume ***

                            datavol[i] = (byte)((float) data[i]*volchange);
                        }
                        audioTrack.write(datavol, 0, count);
                    }

                    audioTrack.stop();
                    audioTrack.release();
                    dataInputStream.close();
                    fileInputStream.close();
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(runnable).start();
    }
}
查看更多
登录 后发表回答