我可以从AudioFocusRequest的其他类中创建OnAudioFocusChangeListener吗?

时间:2020-03-27 14:57:58

标签: java android

我正在创建一个包含var styles = document.createElement('link'); styles.type="text/css"; styles.rel="stylesheet"; styles.href='../scss/Overlay.scss'; document.getElementsByTagName('head')[0].appendChild(styles); Fragment的应用,其中ListView包含ListView。看起来像这样: every ListView item includes button

要在ListView中显示项目,我有一个button。似乎将每个按钮都裁剪为自己的ArrayAdapter的唯一正确方法是在MediaPlayer中将onClickListener设置为按钮右:

ArrayAdapter

我想在我的应用程序中控制AudioFocus,所以我需要一个public class PlaceAdapter extends ArrayAdapter<Place> { private MediaPlayer mMediaPlayer; @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //... Button buttonVoice = listItemView.findViewById(R.id.place_button_voice); if (currentPlace.isHaveVoice()) { buttonVoice.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { releaseMediaPlayer(); mMediaPlayer = MediaPlayer.create(mContext, currentPlace.getVoiceResourceID()); mMediaPlayer.start(); mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { releaseMediaPlayer(); } }); } }); } } //... } 。如果我在AudioManager中创建AudioManager,则会占用大量内存。这就是为什么我需要在我的ArrayAdapter中创建它的原因:

Fragment

public class HotelsFragment() extends Fragment { private AudioManager mAudioManager; private AudioFocusRequest mAudioFocusRequest; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_GAME) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(audioAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(placeAdapter.onAudioFocusChangeListener()).build(); //... } } 中创建onAudioFocusChangeListener是否正确 因为我需要访问mMediaPlayer变量?

ArrayAdapter

0 个答案:

没有答案