AudioTrack: should I use an Asynctask, a Thread or a Handler?

社会主义新天地 提交于 2019-12-02 13:26:03

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!