【问题标题】:AudioTrack: should I use an Asynctask, a Thread or a Handler?AudioTrack:我应该使用 Asynctask、Thread 还是 Handler?
【发布时间】:2018-04-15 11:06:08
【问题描述】:

在 Android 中:我正在尝试播放大小为 230mb 和 20 分钟的 wav 文件,其属性如下:

ffmpeg -i 1.wav

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

以下是android中的代码: 浮动音量=0.5f; @Override 公共无效 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();
            }
        }
    })
}

我想在播放时调节音量。但是由于 Auditrack 正在使用主线程,所以我的 volup 和 voldown 按钮无法更改音量。

我已在单独的线程中播放音频。

我听说 Asynctask 只用于短跨度的事情。在线程中我无法访问 UI

有人可以建议使用哪个,这样如果我改变音量,它也应该改变音轨流字节中的音量。

【问题讨论】:

    标签: android pcm audiotrack


    【解决方案1】:

    我创建了一个线程并将音轨相关代码放入其中。 volup 和 voldown 的变化会立即生效。

    最终的工作代码:

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

    【讨论】:

    • AudioTrack 是否支持 .mp3 文件?
    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 2017-02-13
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多