当替换片段视频仍在android中播放背景时

Posted

技术标签:

【中文标题】当替换片段视频仍在android中播放背景时【英文标题】:when replace a fragment video is still playing background in android 【发布时间】:2017-08-17 06:03:44 【问题描述】:

感谢您的关注:D 请帮助我。 所以这就是我想做的。我有四个片段和一个活动。我在第一个片段中播放实时视频,它工作正常。但是当我用另一个片段替换第一个片段时,第一个片段的视频仍在后台播放。想要做的是在我所有的片段中播放该视频,但大小不同,并且不会相互干扰。我怎样才能做到这一点?这是我的代码。

第一个片段的代码

public class FTop2 extends Fragment  
String Tag = "FTop2";
CallbackManager callbackManager;
ShareDialog shareDialog;
ProgressDialog progressDialog;





public FTop2() 
protected void facebookSDKInitialize() 

    FacebookSdk.sdkInitialize(getContext());

    callbackManager = CallbackManager.Factory.create();

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    facebookSDKInitialize();
    Twitter.initialize(getActivity());



private class HttpUrl  extends AsyncTask<Integer, Void, ArrayList<LiveUrl>> 
    @Override
    protected ArrayList<LiveUrl> doInBackground(Integer... i) 
        return  XmlPrepareClient.getTickedId("cid0003","1","1");
    

    @Override
    protected void onPostExecute(ArrayList<LiveUrl> result) 
        for (int i=0; i<result.size();i++) 
            String txt = result.get(i).url;
            Log.e("play url", Config.B64decodeStr(txt));
        

        VideoView vid = (VideoView) getView().findViewById(R.id.videoView);

        String vid_url = Config.B64decodeStr(result.get(0).url);
        Uri vid_uri = Uri.parse(vid_url);
        vid.setVideoURI(vid_uri);
        vid.start();
        MediaController vid_control = new MediaController(getContext());
        vid_control.setAnchorView(vid);
        vid.setMediaController(vid_control);

        if (progressDialog.isShowing()) 
            progressDialog.hide();
        


    


@Override
public View onCreateView(LayoutInflater i, ViewGroup c,Bundle s) 
    Bundle bundle = this.getArguments();
    final int channel= bundle.getInt("channel");
    final String cat= bundle.getString("cat");
    final String img= bundle.getString("img");

    shareDialog = new ShareDialog(this);
    View view = i.inflate(R.layout.f_top2, c, false);

    progressDialog = Config.getProgressDialog(getContext());
    if(Config.isNetworkAvailable(getContext())) 
        if (!progressDialog.isShowing()) progressDialog.show();
        new HttpUrl().execute();
    else 
        Config.ToastNet(getContext());
    




    ImageView btnBack = (ImageView) view.findViewById(R.id.btn_dismiss);
    btnBack.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            getFragmentManager().popBackStack();

        
    );

    ImageView btnDetail = (ImageView) view.findViewById(R.id.btn_detail);
    btnDetail.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new FTop2_1()).addToBackStack(Tag).commit();

        
    );

    ImageView btnProgram = (ImageView) view.findViewById(R.id.btn_program);
    btnProgram.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new FTop2_2()).addToBackStack(Tag).commit();
        
    );

    ImageView btnComment = (ImageView) view.findViewById(R.id.btn_comment);
    btnComment.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new FTop2_3()).addToBackStack(Tag).commit();
        
    );

第二个片段的代码

public class FTop2_1 extends Fragment 
LinearLayout line;
TextView txt;
ArrayList<TV> datas = new ArrayList<>();
ListView list;
ProgressDialog progressDialog;
boolean isTablet;
public FTop2_1() 
@Override
public void onCreate(Bundle s) 
    super.onCreate(s);



private class HttpTask  extends AsyncTask<Integer, Void, ArrayList<TV>> 
    @Override
    protected ArrayList<TV> doInBackground(Integer... params) 
        return PrepareClient.postTV(params[0]);
    

    @Override
    protected void onPostExecute(ArrayList<TV> result) 
        datas = result;
        Adapter listAdapter1 = new Adapter(getContext(), R.layout.f_top2_1_item, datas);

        ViewGroup.LayoutParams layoutParams = list.getLayoutParams();
        if(!isTablet)
            layoutParams.height = 160*result.size();
        list.setLayoutParams(layoutParams);

        list.setAdapter(listAdapter1);
        if (progressDialog.isShowing()) 
            listAdapter1.notifyDataSetChanged();
            progressDialog.hide();
        
    


private class HttpUrl  extends AsyncTask<Integer, Void, ArrayList<LiveUrl>> 
    @Override
    protected ArrayList<LiveUrl> doInBackground(Integer... i) 
        return  XmlPrepareClient.getTickedId("cid0003","1","1");
    

    @Override
    protected void onPostExecute(ArrayList<LiveUrl> result) 
        for (int i=0; i<result.size();i++) 
            String txt = result.get(i).url;
            Log.e("play url", Config.B64decodeStr(txt));
        

        VideoView vid = (VideoView) getView().findViewById(R.id.videoView);

        String vid_url = Config.B64decodeStr(result.get(0).url);
        Uri vid_uri = Uri.parse(vid_url);
        vid.setVideoURI(vid_uri);
        vid.start();
        MediaController vid_control = new MediaController(getActivity());
        vid_control.setAnchorView(vid);
        vid.setMediaController(vid_control);

        if (progressDialog.isShowing()) 
            progressDialog.hide();
        


    

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle s) 
    View view =  inflater.inflate(R.layout.f_top2_1, container, false);
    Button btnBack = (Button) view.findViewById(R.id.back);
    btnBack.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            getFragmentManager().popBackStack();
        
    );
    isTablet = getResources().getBoolean(R.bool.isTablet);
    list  = (ListView) view.findViewById(R.id.list);
    if(!isTablet)
        list.setEnabled(false);

    progressDialog = Config.getProgressDialog(getContext());

    if(Config.isNetworkAvailable(getContext())) 
        if (!progressDialog.isShowing()) progressDialog.show();
        new HttpTask().execute(1);
        new HttpUrl().execute();
    else
        Config.ToastNet(getContext());
    

    line = (LinearLayout) view.findViewById(R.id.line);
    line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
    ViewGroup.LayoutParams params = line.getLayoutParams();
    params.height = 300;
    line.setLayoutParams(params);

    txt = (TextView) view.findViewById(R.id.text_toggle);
    ImageView toggle = (ImageView) view.findViewById(R.id.toggle);
    toggle.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            if (Integer.parseInt(String.valueOf(line.getHeight())) == 300) 
                line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                ViewGroup.LayoutParams params = line.getLayoutParams();
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                line.setLayoutParams(params);
             else 

                line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                ViewGroup.LayoutParams params = line.getLayoutParams();
                params.height = 300;
                line.setLayoutParams(params);
            
        
    );

    return view;


public class Adapter  extends ArrayAdapter<TV> 
    private LayoutInflater inflater;
    private ArrayList<TV> records;

    public Adapter(Context context, int resource, ArrayList<TV> datas) 
        super(context, resource, datas);
        this.records = datas;
        inflater     = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    public int getCount() 
        return records.size();
    

    public TV getItem(int position) 
        return records.get(position);
    

    public long getItemId(int position) 
        return position;
    

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) 
        if(convertView == null)
            convertView = inflater.inflate(R.layout.f_top2_1_item, parent, false);
        TV result = records.get(position);

        ((TextView)convertView.findViewById(R.id.desc1)).setText(result.time);
        ((TextView)convertView.findViewById(R.id.desc2)).setText(result.subtitle);
        ImageView img = (ImageView) convertView.findViewById(R.id.img2);
        Picasso.with(getContext())
                .load(result.icon)
                .placeholder(R.drawable.no_image)
                .error(R.drawable.no_image)
                .into(img);
        return convertView;
    




【问题讨论】:

【参考方案1】:

@覆盖 公共无效 onDetach()

if(vid.isPlaying()) 
    vid.stopPlayback();

【讨论】:

以上是关于当替换片段视频仍在android中播放背景时的主要内容,如果未能解决你的问题,请参考以下文章

Android主流视频播放及缓存实现原理调研

滚动时在另一个片段视频视图中重叠一个片段的视频拇指

Bootstrap 模式关闭时 Youtube 视频仍在播放

在 IE 中使用 jQuery 删除 iframe 后,Youtube 音频仍在播放

切换到下一个视图控制器后,AVPlayer 视频仍在播放

Android - 通过 FragmentManager 替换片段后,片段返回到它们的第一个状态