首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >刷新exoplayer中的媒体源

刷新exoplayer中的媒体源
EN

Stack Overflow用户
提问于 2017-08-17 13:47:17
回答 1查看 5K关注 0票数 18

我正在使用Exo Player ExtractorMediaSource在我的安卓应用程序中播放视频。我正在从服务器下载媒体并保存在本地数据库中,在特定的时间报警时,我使用exo播放器中的ConcatenatingMediaSource播放此媒体。但首先,我检查所有的视频文件是否都已下载,并使用下载的媒体源启动播放器。如果任何视频没有下载,那么我想在下载时在后台下载它,然后我想将此视频添加到我已经创建的播放列表中

这是示例代码

代码语言:javascript
复制
  private void playAndUpdateVideo(ArrayList<String> mediaSourc) {



        simpleExoPlayerView.setVisibility(View.VISIBLE);
        simpleExoPlayerView.setDefaultArtwork(null);

        mainHandler = new Handler();
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector( videoTrackSelectionFactory);
        dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, "com.cloveritservices.hype"), bandwidthMeter);
// 2. Create a default LoadControl
        extractorsFactory = new DefaultExtractorsFactory();
        LoadControl loadControl = new DefaultLoadControl();

// 3. Create the player
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
        player.addListener(this);

//Set media controller
        simpleExoPlayerView.setUseController(false);
        simpleExoPlayerView.requestFocus();
// Bind the player to the view.
        simpleExoPlayerView.setPlayer(player);




        MediaSource[] mediaSources = new MediaSource[mediaSourc.size()];
        for (int i=0;i<mediaSourc.size();i++)
        {

            mediaSources[i]= buildMediaSource(Uri.parse(mediaSourc.get(i)));

        }

        MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
                : new ConcatenatingMediaSource(mediaSources);
        LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);
        player.prepare(loopingSource);
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isChecked = settings.getBoolean("switch", false);
        if (!isChecked)
        player.setVolume(0f);
        else player.setVolume(2f);
        player.setPlayWhenReady(true);

    }

在这里,我检查视频文件是否已下载

代码语言:javascript
复制
 if (CommonUtils.isExternalStorageExistAndWritable()) {
                for (int i = 0; i < videoUrl.size(); i++) {


                    if (!new File(Environment.getExternalStorageDirectory().toString() + Constants.PROFILE_VIDEO_FOLDER + CommonUtils.fileFromUrl(videoUrl.get(i))).exists() && !CommonUtils.currentlyDownloading(context,CommonUtils.fileFromUrl(videoUrl.get(i)))) {
                        downloadByDownloadManager(videoUrl.get(i), CommonUtils.fileFromUrl(videoUrl.get(i)));
                        if (flag==Constants.FLAG_PLAY){downloadFlag=true;}
                    }
                }

            } else {
                Toast.makeText(getApplicationContext(), "SD Card not mounted.Please Mount SD Card", Toast.LENGTH_SHORT).show();
            }
            if (flag==Constants.FLAG_PLAY && !downloadFlag)
            {
                playAndUpdateVideo(videoUrl);
            }

  public void downloadByDownloadManager(String url, String fileName1) {
        downloadUrl=url;
        fileName=fileName1;
        request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("video file");
        request.setTitle(fileName);

        request.setNotificationVisibility(2);
        request.allowScanningByMediaScanner();

                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
                    request.setDestinationInExternalPublicDir(Constants.PROFILE_VIDEO_FOLDER, fileName);
                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

                    manager.enqueue(request);







        // get download service and enqueue file



    }

请帮助如何将丢失的视频文件添加到播放列表,如果它没有下载。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45727457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档