如何从url中播放android中的视频

时间:2017-06-01 07:37:48

标签: android

公共类MainActivity扩展了AppCompatActivity {

@BindView(rv_home)

With html element: 0 item: ABC,DEF,GHI,JJJ,KKK,LLL  removeHtml.html:27:4
Without html element: 0 item: ABC,DEF,GHI,JJJ,KKK,LLL  removeHtml.html:28:4
With html element: 1 item: 2009,0,2941,4303,354,<a href="#"> 123 </a>  removeHtml.html:27:4
Without html element: 1 item: 2009,0,2941,4303,354, 123   removeHtml.html:28:4
With html element: 2 item: 2010,5,2905,2867,123,<a href="#"> 456456 </a>  removeHtml.html:27:4
Without html element: 2 item: 2010,5,2905,2867,123, 456456   removeHtml.html:28:4
With html element: 3 item: 2011,4,2517,4822,552,<a href="#"> 8254 </a>  removeHtml.html:27:4
Without html element: 3 item: 2011,4,2517,4822,552, 8254   removeHtml.html:28:4
With html element: 4 item: 2012,7777,7777,9999,12,<a href="#"> 456 </a>  removeHtml.html:27:4
Without html element: 4 item: 2012,7777,7777,9999,12, 456   removeHtml.html:28:4

//删除通知栏         this.getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);         的setContentView(R.layout.activity_main);         ButterKnife.bind(本);

  1. 列出项目

    AAH_CustomRecyclerView recyclerView;
    
    private final List<MyModel> modelList = new ArrayList<>();
    
    MyVideosAdapter mAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    // modelList.add(new MyModel(“/ storage / emulated / 0 / VideoPlay / myvideo.mp4”,“http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795681/2_rp0zyy.jpg”,“video18”));

    Picasso p = Picasso.with(this);
    
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795681/2_rp0zyy.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795681/2_rp0zyy.jpg", "video1"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1491561340/hello_cuwgcb.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1491561340/hello_cuwgcb.jpg", "video2"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795675/3_yqeudi.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795675/3_yqeudi.jpg", "video4"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795675/1_pyn1fm.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795675/1_pyn1fm.jpg", "video5"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1491561340/hello_cuwgcb.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1491561340/hello_cuwgcb.jpg", "video6"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795681/2_rp0zyy.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795681/2_rp0zyy.jpg", "video9"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795676/4_nvnzry.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795676/4_nvnzry.jpg", "video11"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795681/2_rp0zyy.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795681/2_rp0zyy.jpg", "video12"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795675/3_yqeudi.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795675/3_yqeudi.jpg", "video16"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795675/1_pyn1fm.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795675/1_pyn1fm.jpg", "video17"));
    modelList.add(new MyModel("http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70/v1481795681/2_rp0zyy.mp4", "http://res.cloudinary.com/krupen/video/upload/w_300,h_150,c_crop,q_70,so_0/v1481795681/2_rp0zyy.jpg", "video18"));
    
    
    //you can pass local file uri, but make sure it exists
    

    // LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);     recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));     //recyclerView.setLayoutManager(mLayoutManager);     recyclerView.setItemAnimator(new DefaultItemAnimator());

     mAdapter = new MyVideosAdapter(modelList, p);
    

    recyclerView.setPlayOnlyFirstVideo(真); //默认为false

    //todo before setAdapter
    recyclerView.setActivity(this);
    
    //optional - to play only first visible video
    

    } }

2 个答案:

答案 0 :(得分:1)

插入您的视频网址=“”;

onCreate方法输入以下代码:

try {

            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);

            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {

                videoview.start();
            }
        });

    }

或点击此链接..

http://www.androidhive.info/2014/12/how-to-play-youtube-video-in-android-app/

答案 1 :(得分:0)

声明你videoView

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

然后是deckare MediaController

        try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                YourAvtivityNAme.this);
        mediacontroller.setAnchorView(videoView);
        // Get the URL from String VideoURL
        Uri video = Uri.parse(Video_url);
        videoView.setMediaController(mediacontroller);
        videoView.setVideoURI(video);

    } catch (Exception e) {

        Log.e("Error", e.getMessage());
        e.printStackTrace();



    }

准备媒体播放器

 videoView.requestFocus();
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
           progressDialog.dismiss();
            videoView.start();
        }
    });