Android - 单击列表视图中的按钮时播放文件

时间:2016-09-05 13:00:39

标签: android listview audio onclick media-player

我有聊天功能,其中包括语音留言。用于记录,将文件发送到服务器以及插入列表的代码也很好。

我的布局文件(list_item_chat),我有一个播放按钮,一个搜索栏和两个文本视图,显示播放文件的当前时间和持续时间(流媒体):

list_item_chat.xml

public ChatPostAdapter adapterLoadingMore; //adapter global variable

//Binding adapter
adapterLoadingMore = (ChatPostAdapter) ((HeaderViewListAdapter) mList.getAdapter()).getWrappedAdapter();
adapterLoadingMore.setPlayPauseButtonListener(ChatActivity.this);

//method for playing audio file
public void playVoiceMessage(final View v, ChatPostItem chatPostItem) {
        if (mp == null) {
            mp = new MediaPlayer();
        } else {
            mp.reset();
        }

    String url = chatPostItem.getAudioFile();
    try {
        mp.setDataSource(url);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mp.prepareAsync();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mp = null;

                adapterLoadinMore.notifyDataSetChanged();
            }
        });

    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onPlayPauseIsClickListener(View v, int position, ChatPostItem chatPostItem, int play) {
    if (play == 1) {
        playVoiceMessage(v, chatPostItem);
    } else if (play == 2) {
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.pause();
            }
        }
    } else {
        if (mp != null) {
            mp.start();
        }
    }
}

我在我的活动(ChatActivity)中创建了一个监听器,并为我的适配器(ChatPostAdapter)中的每个PlayButton设置了监听器:

ChatActivity.java:

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

        View v = convertView;
        LayoutInflater mInflater = (LayoutInflater) this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        try {
            if (position >= 0 && position < navDrawerItems.size()) {
                final ChatPostItem item = navDrawerItems.get(position);
                if (item != null) {
                    UserData user = ((MyApplication) this.context.getApplicationContext()).getUser();
                    final ViewHolder holder;
                    if (v == null) {
                        holder = new ViewHolder();

                        v = mInflater.inflate(R.layout.list_item_chat, parent, false);

                        holder.Post = (TextView) v.findViewById(R.id.list_item_chat_post_text);
                        holder.CreationDatetime = (TextView) v.findViewById(R.id.list_item_chat_post_date);
                        holder.CurrentTime = (TextView) v.findViewById(R.id.lblCurrentDuration);
                        holder.SeekBarCurrentDuration = (SeekBar) v.findViewById(R.id.skbAudioCurrentState);
                        holder.PlayButton = (ImageButton) v.findViewById(R.id.btnPlayAudioStreaming);
                        holder.CreationDatetime = (TextView) v.findViewById(R.id.list_item_chat_post_date);
                        holder.layout_content_all_items = (LinearLayout) v.findViewById(R.id.layout_content_all_items);
                        holder.layout_main_container = (LinearLayout) v.findViewById(R.id.layout_main_container);
                        holder.chat_right_post = (ImageView) v.findViewById(R.id.chat_right_post);
                        holder.chat_left_post = (ImageView) v.findViewById(R.id.chat_left_post);
                        holder.layout_player_content = (RelativeLayout) v.findViewById(R.id.layout_player_content);
                        holder.lblTotalDuration = (TextView) v.findViewById(R.id.lblTotalDuration);

                        v.setTag(holder);
                    } else {
                        holder = (ViewHolder) v.getTag();

                        if (position != mButtonPosition) {
                            holder.PlayButton.setImageResource(R.drawable.ic_action_play);
                            holder.SeekBarCurrentDuration.setProgress(0);
                            holder.SeekBarCurrentDuration.setMax(100);
                        } else {
                            holder.PlayButton.setImageResource(R.drawable.ic_action_pause);
                            //get current progress and current playing time
                            //set it on the tag for the respective View and bla bla bla bla...

                        }
                    }

                    showHideLayoutButtons(holder, item, user);


                    holder.PlayButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (playPauseButtonListener != null) {

                                int PlayStopButtonState = Integer.parseInt(v.getTag().toString());
                                int previousPosition = mButtonPosition;

                                if (PlayStopButtonState == 1) {
                                    //necessary to control recycling
                                    mButtonPosition = position;

                                    v.setTag(2);
                                    playPauseButtonListener.onPlayPauseIsClickListener(v, position, item, PlayStopButtonState);

                                    ((ImageButton) v).setImageResource(R.drawable.ic_action_pause);
                                } else if (PlayStopButtonState == 2) {
                                    mButtonPosition = -1;

                                    v.setTag(3);
                                    playPauseButtonListener.onPlayPauseIsClickListener(v, position, item, PlayStopButtonState);

                                    ((ImageButton) v).setImageResource(R.drawable.ic_action_play);
                                } else {
                                    mButtonPosition = -1;

                                    v.setTag(2);
                                    playPauseButtonListener.onPlayPauseIsClickListener(v, position, item, PlayStopButtonState);

                                    ((ImageButton) v).setImageResource(R.drawable.ic_action_pause);
                                }

                                if (previousPosition != -1)
                                    notifyDataSetChanged();
                            }
                        }
                    });

                    try {
                        SimpleDateFormat originalFormat = new SimpleDateFormat(US_DATE_TIME, Locale.US);
                        SimpleDateFormat targetFormat = new SimpleDateFormat(STANDARD_DATE_TIME, Locale.US);
                        Date date = originalFormat.parse(item.getCreatedDateTime());

                        holder.CreationDatetime.setText(targetFormat.format(date));
                    } catch (Exception e) {
                        holder.CreationDatetime.setText(item.getCreatedDateTime());
                    }
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.w("Adaptor getView", e.toString());
        }
        return v;
    }

OnPlayPauseButtonClickListener playPauseButtonListener;

    public interface OnPlayPauseButtonClickListener {
        void onPlayPauseIsClickListener(View button, int position, ChatPostItem chatPostItem, int play);
    }

    public void setPlayPauseButtonListener(OnPlayPauseButtonClickListener playPauseButtonListener) {
        this.playPauseButtonListener = playPauseButtonListener;
    }
}

ChatPostAdapter.java:

{{1}}

单击播放时,文件开始正常播放。我使播放/暂停控制也正常。

问题是:

我在该列表中播放一些OTHER文件时遇到了麻烦!我不知道如何妥善控制这个。 我已经尝试过任何我认识的方法,多次研究过,现在我在这里寻求帮助。如果有人能够提供帮助,我将不胜感激!

非常感谢!

0 个答案:

没有答案