视频在应用程序运行时开始播放。如何避免这种情况?

问题描述 投票:0回答:1

我制作了带有视频视图的列表视图。当应用程序运行时,视频开始自动播放。我不要当我单击该视频时,它应该开始播放。我做了一个适配器。

ArrayAdapter

MediaController mc;

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

        LayoutInflater LayoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View listViewItem = LayoutInflater.inflate(R.layout.symptomlistitem, parent, false);
        TextView txtSymptom = (TextView)listViewItem.findViewById(R.id.txtSymptom);
        VideoView videoSymptom = (VideoView) listViewItem.findViewById(R.id.videoSymptom);

        Symptom symptom = this.getItem(position);

        txtSymptom.setText(symptom.getSymptom());
        videoSymptom.setVideoURI(Uri.parse("android.resource://com.example.speechrecogniser/" + (symptom.getVideo())));
        videoSymptom.setVisibility(View.VISIBLE);
        videoSymptom.setMediaController(mc);
        mc.setAnchorView(videoSymptom);
        videoSymptom.start();
        return listViewItem;
    }

我添加了MediaController,但是没有用。请帮助我!

android-studio android-listview android-videoview
1个回答
0
投票

由于您在videoSymptom.start()方法上添加了getView(),因此视频立即开始播放,因此,当列表中填充视频时,它们将立即开始播放。

一种可能的解决方案是删除该语句,并确保仅在单击视频的窗口小部件时才调用.start()方法:

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

    LayoutInflater LayoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View listViewItem = LayoutInflater.inflate(R.layout.symptomlistitem, parent, false);
    TextView txtSymptom = (TextView)listViewItem.findViewById(R.id.txtSymptom);
    final VideoView videoSymptom = (VideoView) listViewItem.findViewById(R.id.videoSymptom);

    Symptom symptom = this.getItem(position);

    txtSymptom.setText(symptom.getSymptom());
    videoSymptom.setVideoURI(Uri.parse("android.resource://com.example.speechrecogniser/" + (symptom.getVideo())));
    videoSymptom.setVisibility(View.VISIBLE);
    videoSymptom.setMediaController(mc);
    videoSymptom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            videoSymptom.start();        
        }
    });
    mc.setAnchorView(videoSymptom);
    return listViewItem;
}
© www.soinside.com 2019 - 2024. All rights reserved.