安卓:AudioTrack不能正常工作

3
我希望能够创建一个包含ListView的活动,在点击列表项时播放不同的声音效果。我已经测试了MediaPlayer和SoundPool,它们都可以正常工作。
我尝试使用AudioTrack实现,但是当我点击ListView的某个项目时,只会播放噪音或者崩溃并显示“无效的音频缓冲区大小”的错误信息。
以下是我的代码:
public class ATSoundAdapter extends ArrayAdapter {

    private ArrayList<SoundData> mData;
    private Context context;
    private AudioTrack at;


    public ATSoundAdapter(Context context, ArrayList<SoundData> mData) {
        super(context, -1);
        this.context = context;
        this.mData = mData;
    }

    public class ViewHolder {
        private TextView mSoundNameTv;
        private LinearLayout container;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.list_item,
                            parent, false);
            holder = new ViewHolder();
            holder.mSoundNameTv = (TextView) convertView.findViewById(R.id.sound_name_tv);
            holder.container = (LinearLayout) convertView.findViewById(R.id.container);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SoundData currentSound = mData.get(position);
        if (currentSound != null) {
            holder.mSoundNameTv.setText(currentSound.getSoundName());
            holder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playAudioTrack(currentSound.getSoundResource());
                }
            });
        }
        return convertView;
    }

    private void playAudioTrack(int resource) {
        int i = 0;
        InputStream inputStream = context.getResources().openRawResource(resource);
        long bufferSize = context.getResources().openRawResourceFd(resource).getLength();
        byte[] buffer = new byte[(int)bufferSize];
        try {
            int lengthOfAudioClip = inputStream.read(buffer, 0, buffer.length);
            AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,
                    44100, AudioFormat.CHANNEL_OUT_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    lengthOfAudioClip, AudioTrack.MODE_STATIC);
            at.write(buffer, 0, lengthOfAudioClip);
            inputStream.close();
            at.play();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getCount() {
        return mData.size();
    }
}

我原以为字节数组大小有错... 但是当我在logcat中查看时,它恰好是原始文件的大小。

我是Android新手,不知道如何使用AudioTrack。

提前致谢。


1
那么 MediaPlayerSoundPool 有什么问题吗? - pskink
1
请查看此链接:https://dev59.com/imDVa4cB1Zd3GeqPgcS8 - Narendra
1个回答

2

你可以更改你的代码

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, lengthOfAudioClip, AudioTrack.MODE_STREAM);    

或者你可以查看这个链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接