最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)下載m3u8視頻文件問題解決

 更新時間:2023年01月28日 17:27:51   作者:FranzLiszt1847  
這篇文章主要介紹了Android實現(xiàn)下載m3u8視頻文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

效果圖

簡介

Aria

下載器采用開源框架Aria

github

中文文檔

導入Aria

   implementation 'me.laoyuyu.aria:core:3.8.16'
    annotationProcessor 'me.laoyuyu.aria:compiler:3.8.16'
    implementation 'me.laoyuyu.aria:m3u8:3.8.16'

介紹

service在Appliaction中啟動,即啟動app即啟動service并且service只啟動一次,后序通過單例binder去調用服務

啟動Service

在Application中默認啟動Service

private void bindService(){
        DownloadService.bindService(this, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                downloadService = null;
            }
        });
    }

DownloadService

用于Aplication調用起服務

public static void bindService(Context context, ServiceConnection connection){
        Intent intent = new Intent(context, DownloadService.class);
        context.bindService(intent, connection, Service.BIND_AUTO_CREATE);
    }

注冊下載器

@Override
    public void onCreate() {
        super.onCreate();
        Aria.download(this).register();
        Log.d("DownloadService","create service");
    }

若上次有未下載完成的視頻,則恢復下載,并將binder賦給另一個單例binder,后續(xù)使用binder進行具體下載事項

@Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("DownloadService","bind service");
        long taskId = (long)SP.getInstance().GetData(BaseApplication.getContext(),"lastDownloadID",0L);
        if (taskId != 0L){
            List<DownloadEntity> entityList = Aria.download(this).getAllNotCompleteTask();
            if (entityList != null){
                HttpNormalTarget target = Aria.download(this).load(taskId);
                if (target.getTaskState() != STATE_COMPLETE){
                    target.m3u8VodOption(DownloadBinder.getInstance().getOption());
                    target.resume();
                    Log.d("DownloadService","resume download");
                } else {
                    HttpNormalTarget resume =  Aria.download(this).load( entityList.get(0).getId());
                    resume.m3u8VodOption(DownloadBinder.getInstance().getOption());
                    if ((resume.getTaskState() == STATE_FAIL) || (resume.getTaskState() == STATE_OTHER)){
                        resume.resetState();
                        Log.d("DownloadService","resetState");
                    }else {
                        resume.resume();
                        Log.d("DownloadService","resumeState");
                    }
                }
            }
        }
        return DownloadBinder.getInstance();
    }

注銷aria下載器和解除binder綁定

 @Override
    public boolean onUnbind(Intent intent) {
        Log.d("DownloadService","unbind service");
        return super.onUnbind(intent);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Aria.download(this).unRegister();
        Log.d("DownloadService","service onDestroy");
    }

下載回調

然后將Aria下載器的回調在進行一次中轉,回調至單例binder,后面在下載就不需要binder服務,直接調用單例binder即可

    @Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) {
        Log.d("DownloadService","該下載鏈接不支持斷點");
       // DownloadBinder.getInstance().onTaskStart(task);
    }
    @Download.onTaskStart public void onTaskStart(DownloadTask task) {
        Log.d("DownloadService",task.getDownloadEntity().getFileName() +":開始下載");
        DownloadBinder.getInstance().onTaskStart(task);
    }
    @Download.onTaskStop public void onTaskStop(DownloadTask task) {
        Log.d("DownloadService",task.getDownloadEntity().getFileName() +":停止下載");
        DownloadBinder.getInstance().onTaskStop(task);
    }
    @Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
        Log.d("DownloadService",task.getDownloadEntity().getFileName() +":取消下載");
        DownloadBinder.getInstance().onTaskCancel(task);
    }
    @Download.onTaskFail public void onTaskFail(DownloadTask task) {
        Log.d("DownloadService",task.getDownloadEntity().getFileName() +":下載失敗");
        DownloadBinder.getInstance().onTaskFail(task);
    }
    @Download.onTaskComplete public void onTaskComplete(DownloadTask task) {
        Log.d("DownloadService",task.getDownloadEntity().getFileName() +":下載完成");
        DownloadBinder.getInstance().onTaskComplete(task);
    }
    /**
     * @param e 異常信息
     */
    @Download.onTaskFail void taskFail(DownloadTask task, Exception e) {
        try {
            DownloadBinder.getInstance().taskFail(task,e);
            ALog.d("DownloadService", task.getDownloadEntity().getFileName() +"error:"+ALog.getExceptionString(e));
        }catch (Exception ee){
            ee.printStackTrace();
        }
    }
    @Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
        Log.d("DownloadService","pre = "+task.getPercent()+"   "+"speed = "+ task.getConvertSpeed());
        DownloadBinder.getInstance().onTaskRunning(task);
    }
}

回調接口

將服務中的Aria回調,回調至單例binder中

public interface ServiceCallback {
    void onTaskStart(DownloadTask task);
    void onTaskStop(DownloadTask task);
    void onTaskCancel(DownloadTask task);
    void onTaskFail(DownloadTask task);
    void onTaskComplete(DownloadTask task);
    void onTaskRunning(DownloadTask task);
    void taskFail(DownloadTask task, Exception e);
}

單例Binder

構造單例

public class DownloadBinder extends Binder implements ServiceCallback {
 private static DownloadBinder binder;
    private DownloadBinder() {
    }
    public static DownloadBinder getInstance() {
        if (binder == null) {
            binder = new DownloadBinder();
            downloadReceiver = Aria.download(BaseApplication.getContext());
        }
        return binder;
    }

下載

將下載信息傳入,并以視頻type+id+name等構件下載文件夾名稱,確保唯一性,然后通過配置Aria Option,使其切換至m3u8文件下載模式,具體配置文件還可配置下載速度、最大下載文件數(shù)量、線程數(shù)等等。

Aria自帶數(shù)據(jù)庫,可通過其數(shù)據(jù)庫保存一些數(shù)據(jù),但讀取數(shù)據(jù)較慢

    public void startDownload(DownloadBean downloadBean) {
        if (downloadBean == null) return;
        String locationDir = FileUtils.getInstance().mainCatalogue();
        String name = downloadBean.getVideoType()+downloadBean.gettId() + downloadBean.getsId() + downloadBean.geteId();
        String subFile = FileUtils.getInstance().createFile(locationDir, name);
        String path = subFile + File.separator + name + ".m3u8";
        Log.d("DownloadService", "start download");
        boolean isExist = IsExist(path);
        if (isExist) {
            Log.d("DownloadService", "exist same item");
            if (repeatTaskId != 0) {
                if (Aria.download(this).load(repeatTaskId).getTaskState() != STATE_RUNNING) {
                    if (downloadReceiver.load(repeatTaskId).getEntity().getRealUrl().equals(downloadBean.getVideoUrl())) {
                        downloadReceiver.load(repeatTaskId).m3u8VodOption(DownloadBinder.getInstance().getOption());
                        downloadReceiver.load(repeatTaskId).resume();
                    } else {
                        downloadReceiver.load(repeatTaskId).m3u8VodOption(DownloadBinder.getInstance().getOption());
                        downloadReceiver.load(repeatTaskId).updateUrl(downloadBean.getVideoUrl()).resume();
                    }
                }
                Log.d("DownloadService", "resume exist same item");
                return;
            }
        }
        HttpBuilderTarget target = downloadReceiver.load(downloadBean.getVideoUrl())
                .setFilePath(path)
                .ignoreFilePathOccupy()
                .m3u8VodOption(getOption());
        List<DownloadEntity> downloadEntityList = downloadReceiver.getDRunningTask();
        if (downloadEntityList == null) {
            repeatTaskId = target.create();
        } else {
            repeatTaskId = target.add();
        }
        try {
            repeatTaskId = target.getEntity().getId();
            downloadBean.setTaskId(repeatTaskId);
            SP.getInstance().PutData(BaseApplication.getContext(),"lastDownloadID",repeatTaskId);
            target.setExtendField(new Gson().toJson(downloadBean)).getEntity().save();
        }catch (NullPointerException e){
            e.printStackTrace();
        }
    }

輻射

再一次將service回調的接口回調至binder的接口,通過EventBus輻射至外部,通過一層層封裝,在外部監(jiān)聽當前文件下載狀態(tài),只需通過監(jiān)聽EventBus事件即可

 /**
     * download status
     * 0:prepare
     * 1:starting
     * 2:pause
     * 3:cancel
     * 4:failed
     * 5:completed
     * 6:running
     * 7:exception*/
    @Override
    public void onTaskStart(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(1,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void onTaskStop(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(2,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void onTaskCancel(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(3,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void onTaskFail(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(4,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void onTaskComplete(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(5,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void onTaskRunning(DownloadTask task) {
        EventBus.getDefault().postSticky(new DownloadStatusBean(6,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
    }
    @Override
    public void taskFail(DownloadTask task, Exception e) {
        try {
            EventBus.getDefault().postSticky(new DownloadStatusBean(4,task.getDownloadEntity().getId(), task.getConvertSpeed(), task.getPercent(),task.getConvertFileSize(),task.getFilePath()));
        }catch (NullPointerException ee){
            ee.printStackTrace();
        }
    }
}

創(chuàng)建下載實例

一句話我們就可以實現(xiàn)視頻下載,然后后天服務自動回調給binder,然后binder回調給EventBus

 DownloadBean bean = new DownloadBean(0L,m_id,"","",sourceBean.getLink(),detailBean.getCover(),detailBean.getTitle(),"","","movie","",0);
  DownloadBinder.getInstance().startDownload(bean);

監(jiān)聽下載狀態(tài)

然后只需要在需要更新界面的地方注冊EventBus即可,通過封裝,不同的類做不同的事情,將數(shù)據(jù)處理和UI更新進行隔離,可以提高代碼閱讀和執(zhí)行效率

 /**
     * download status
     * 0:prepare
     * 1:starting
     * 2:pause
     * 3:cancel
     * 4:failed
     * 5:completed
     * 6:running
     * 7:exception*/
    /**
     * 下載item狀態(tài)監(jiān)聽*/
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void OnEvent(DownloadStatusBean bean) {
        taskID = bean.getTaskID();
        switch (bean.getStatus()) {
            case 1:
                getRunningItem();
                Log.d("DownloadService", "status start");
                break;
            case 2:
                updateStatus(bean);
                Log.d("DownloadService", "status pause");
                break;
            case 3:
                if ((index == -1) && (beanList.size() > 0)){
                    index = 0;
                }
                Log.d("DownloadService", "status cancel"+bean.getTaskID());
                break;
            case 4:
                //update url
                failCount++;
                if (failCount >= 3){
                    failedReconnect(bean);
                    failCount = 0;
                    isRunning = true;
                    Log.d("DownloadService", "status fail in");
                }
                Log.d("DownloadService", "status fail");
                break;
            case 5:
                removeDownloadBead(bean.getTaskID());
                startDownload();
                Log.d("DownloadService", "status complete");
                break;
            case 6:
                if (isRunning) {
                    getRunningItem();
                }
                updateCurItem(bean);
                Log.d("DownloadService", "status running: "+index);
                break;
        }
    }

到此這篇關于Android實現(xiàn)下載m3u8視頻文件問題解決的文章就介紹到這了,更多相關Android下載m3u8視頻內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android TabLayout(選項卡布局)簡單用法實例分析

    Android TabLayout(選項卡布局)簡單用法實例分析

    這篇文章主要介紹了Android TabLayout(選項卡布局)簡單用法,結合實例形式簡單分析了Android選項卡布局的界面布局與功能實現(xiàn)具體相關技巧,需要的朋友可以參考下
    2016-01-01
  • API處理Android安全距離詳情

    API處理Android安全距離詳情

    這篇文章主要介紹了API處理Android安全距離詳情,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-06-06
  • Android實現(xiàn)視頻圖片輪播功能

    Android實現(xiàn)視頻圖片輪播功能

    視頻與圖片輪播功能是一種常見的用戶交互方式,廣泛應用于多媒體展示、廣告輪播、資訊閱讀、產品推薦等場景,在這種模式下,應用能同時支持圖片和視頻的自動或手動輪播播放,本項目旨在基于 Android 平臺,構建一個支持視頻與圖片混合輪播的解決方案,需要的朋友可以參考下
    2025-04-04
  • Android自定義懸浮按鈕效果

    Android自定義懸浮按鈕效果

    這篇文章主要為大家詳細介紹了Android自定義懸浮按鈕效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android APP與媒體存儲服務的交互

    Android APP與媒體存儲服務的交互

    本文介紹如何在 Android 中,開發(fā)者的 APP 如何使用媒體存儲服務(包含MediaScanner、MediaProvider以及媒體信息解析等部分),包括如何把 APP 新增或修改的文件更新到媒體數(shù)據(jù)庫、如何在多媒體應用中隱藏 APP 產生的文件、如何監(jiān)聽媒體數(shù)據(jù)庫的變化等等。
    2013-10-10
  • Android利用BitMap獲得圖片像素數(shù)據(jù)的方法

    Android利用BitMap獲得圖片像素數(shù)據(jù)的方法

    這篇文章主要介紹了Android利用BitMap獲得圖片像素數(shù)據(jù)的方法,結合實例對比分析了Android獲取圖片像素數(shù)據(jù)的相關技巧,需要的朋友可以參考下
    2016-02-02
  • Flutter深色模式適配的實現(xiàn)

    Flutter深色模式適配的實現(xiàn)

    這篇文章主要介紹了Flutter深色模式適配的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Android控件BottomSheet實現(xiàn)底邊彈出選擇列表

    Android控件BottomSheet實現(xiàn)底邊彈出選擇列表

    這篇文章主要介紹了Android控件BottomSheet實現(xiàn)底邊彈出選擇列表,比較常用的選擇條件或跳轉方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • RecyclerView自定義分割線

    RecyclerView自定義分割線

    這篇文章主要為大家詳細介紹了RecyclerView自定義分割線的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 安卓(android)怎么實現(xiàn)下拉刷新

    安卓(android)怎么實現(xiàn)下拉刷新

    這里我們將采取的方案是使用組合View的方式,先自定義一個布局繼承自LinearLayout,然后在這個布局中加入下拉頭和ListView這兩個子元素,并讓這兩個子元素縱向排列。對安卓(android)怎么實現(xiàn)下拉刷新的相關知識感興趣的朋友一起學習吧
    2016-04-04

最新評論

宁夏| 温州市| 梁平县| 绥棱县| 东方市| 长兴县| 莆田市| 新余市| 布拖县| 贵阳市| 海丰县| 鞍山市| 葫芦岛市| 玉龙| 三穗县| 聊城市| 临高县| 班戈县| 托克托县| 绥宁县| 故城县| 南阳市| 西城区| 长汀县| 上蔡县| 玉田县| 曲阳县| 黄浦区| 澄城县| 富蕴县| 个旧市| 郁南县| 潮安县| 微博| 东源县| 宁晋县| 南宁市| 青浦区| 县级市| 浪卡子县| 什邡市|