laoyuyu
2019-07-10 20:21:42 +08:00
parent b0818537f2
commit b1bc4529c0
32 changed files with 406 additions and 248 deletions

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.simple.common;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import com.arialyy.frame.util.DensityUtils;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseDialog;
import com.arialyy.simple.databinding.DialogLoadingBinding;
@SuppressLint("ValidFragment") public class LoadingDialog extends BaseDialog<DialogLoadingBinding> {
public LoadingDialog(Object obj) {
super(obj);
}
@Override protected int setLayoutId() {
return R.layout.dialog_loading;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(DensityUtils.dp2px(120), DensityUtils.dp2px(120));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().setCanceledOnTouchOutside(false);
}
}
}

View File

@@ -29,9 +29,7 @@ import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import com.arialyy.annotations.Download;
import com.arialyy.annotations.M3U8;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTarget;
@@ -47,7 +45,6 @@ import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.common.ModifyPathDialog;
import com.arialyy.simple.common.ModifyUrlDialog;
import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.util.AppUtil;
import java.io.File;
import java.io.IOException;
@@ -243,9 +240,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Download.onTaskFail
void taskFail(DownloadTask task, Exception e) {
if (task.getKey().equals(mUrl)) {
Toast.makeText(SingleTaskActivity.this, getString(R.string.download_fail), Toast.LENGTH_SHORT)
.show();
Toast.makeText(SingleTaskActivity.this, getString(R.string.download_fail), Toast.LENGTH_SHORT)
.show();
if (task != null && task.getKey().equals(mUrl)) {
getBinding().setStateStr(getString(R.string.start));
}
}

View File

@@ -75,7 +75,7 @@ import java.util.List;
findViewById(R.id.stop).setOnClickListener(this);
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.cancel).setOnClickListener(this);
//findViewById(R.id.cancel).setOnClickListener(this);
mGroup.setText("任务组:" + mGroupHash);
mSub.setText("子任务:" + mChildName);
mPb.setProgress((int) (mChildEntity.getCurrentProgress() * 100 / mChildEntity.getFileSize()));

View File

@@ -23,6 +23,7 @@ import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTarget;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.inf.IHttpFileLenAdapter;
import com.arialyy.aria.util.ALog;
@@ -40,18 +41,23 @@ import java.util.Map;
*/
public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBinding> {
SubStateLinearLayout mChildList;
List<String> mUrls;
private SubStateLinearLayout mChildList;
private List<String> mUrls;
private DownloadGroupTarget mTarget;
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
Aria.download(this).register();
setTitle("任务组");
mChildList = getBinding().childList;
mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupEntity entity = Aria.download(this).getDownloadGroupEntity(mUrls);
if (entity != null) {
mTarget = Aria.download(this).loadGroup(entity);
mChildList.addData(entity.getSubEntities());
getBinding().setStateStr(
mTarget.isRunning() ? getString(R.string.stop) : getString(R.string.resume));
getBinding().setFileSize(entity.getConvertFileSize());
if (entity.getFileSize() == 0) {
getBinding().setProgress(0);
@@ -59,7 +65,8 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
getBinding().setProgress(entity.isComplete() ? 100
: (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
}
ALog.d(TAG, "size = " + entity.getSubEntities().size() + "; len = " + entity.getConvertFileSize());
ALog.d(TAG,
"size = " + entity.getSubEntities().size() + "; len = " + entity.getConvertFileSize());
}
mChildList.setOnItemClickListener(new SubStateLinearLayout.OnItemClickListener() {
@@ -82,33 +89,36 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.loadGroup(mUrls)
.setDirPath(
//Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_5")
Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_2")
.setGroupAlias("任务组测试")
//.setSubFileName(getModule(GroupModule.class).getSubName2())
.setSubFileName(getModule(GroupModule.class).getSubName())
.unknownSize()
.setFileLenAdapter(new IHttpFileLenAdapter() {
@Override public long handleFileLen(Map<String, List<String>> headers) {
if (mTarget == null || !mTarget.isRunning()) {
Aria.download(this)
.loadGroup(mUrls)
.setDirPath(
//Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_5")
Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_2")
.setGroupAlias("任务组测试")
//.setSubFileName(getModule(GroupModule.class).getSubName2())
.setSubFileName(getModule(GroupModule.class).getSubName())
.unknownSize()
.setFileLenAdapter(new IHttpFileLenAdapter() {
@Override public long handleFileLen(Map<String, List<String>> headers) {
List<String> sLength = headers.get("Content-Length");
if (sLength == null || sLength.isEmpty()) {
return -1;
List<String> sLength = headers.get("Content-Length");
if (sLength == null || sLength.isEmpty()) {
return -1;
}
String temp = sLength.get(0);
return Long.parseLong(temp);
}
String temp = sLength.get(0);
return Long.parseLong(temp);
}
})
//.setFileSize(114981416)
//.updateUrls(temp)
.start();
break;
case R.id.stop:
Aria.download(this).loadGroup(mUrls).stop();
})
//.setFileSize(114981416)
//.updateUrls(temp)
.start();
getBinding().setStateStr(getString(R.string.stop));
} else {
Aria.download(this).loadGroup(mUrls).stop();
getBinding().setStateStr(getString(R.string.resume));
}
break;
case R.id.cancel:
Aria.download(this).loadGroup(mUrls).cancel(true);
@@ -166,10 +176,12 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
L.d(TAG, "group task cancel");
getBinding().setSpeed("");
getBinding().setProgress(0);
getBinding().setStateStr(getString(R.string.start));
}
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
L.d(TAG, "group task fail");
getBinding().setStateStr(getString(R.string.resume));
}
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
@@ -178,6 +190,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
mChildList.updateChildProgress(task.getEntity().getSubEntities());
T.showShort(this, "任务组下载完成");
L.d(TAG, "任务组下载完成");
getBinding().setStateStr(getString(R.string.start));
}
@DownloadGroup.onSubTaskRunning void onSubTaskRunning(DownloadGroupTask groupTask,

View File

@@ -45,9 +45,13 @@ import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.common.ModifyPathDialog;
import com.arialyy.simple.common.ModifyUrlDialog;
import com.arialyy.simple.databinding.ActivityM3u8VodBinding;
import com.arialyy.simple.to.PeerIndex;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class M3U8VodDLoadActivity extends BaseActivity<ActivityM3u8VodBinding> {
@@ -105,6 +109,21 @@ public class M3U8VodDLoadActivity extends BaseActivity<ActivityM3u8VodBinding> {
dialog.show(getSupportFragmentManager(), "ModifyPathDialog");
}
@Override protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void jumpIndex(PeerIndex index) {
Aria.download(this).load(mUrl).asM3U8().asVod().jumPeerIndex(index.index);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_single_task_activity, menu);
@@ -303,8 +322,6 @@ public class M3U8VodDLoadActivity extends BaseActivity<ActivityM3u8VodBinding> {
mModule.uploadUrl(this, String.valueOf(data));
} else if (result == ModifyPathDialog.MODIFY_PATH_RESULT) {
mModule.updateFilePath(this, String.valueOf(data));
} else if (result == VideoPlayerFragment.SEEK_BAR_PROGRESS_KEY) {
Aria.download(this).load(mUrl).asM3U8().asVod().jumPeerIndex((Integer) data);
}
}
}

View File

@@ -14,24 +14,29 @@ import android.view.View;
import android.widget.SeekBar;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.m3u8.M3U8Entity;
import com.arialyy.aria.util.ALog;
import com.arialyy.frame.base.BaseFragment;
import com.arialyy.simple.R;
import com.arialyy.simple.common.LoadingDialog;
import com.arialyy.simple.databinding.FragmentVideoPlayerBinding;
import com.arialyy.simple.to.PeerIndex;
import java.io.IOException;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
@SuppressLint("ValidFragment")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class VideoPlayerFragment extends BaseFragment<FragmentVideoPlayerBinding> {
static final int SEEK_BAR_PROGRESS_KEY = 0xC1;
private M3U8VodModule mModule;
private DownloadEntity mEntity;
private int mPeerIndex;
SparseArray<String> mPlayers = new SparseArray<>();
private SparseArray<String> mPlayers = new SparseArray<>();
private SurfaceHolder mSurfaceHolder;
private NextMediaPlayer nexPlayer;
private NextMediaPlayer mNextPlayer;
private MediaPlayer mCurrentPlayer;
private LoadingDialog mLoadingDialog;
private int mJumpIndex = -1;
VideoPlayerFragment(int peerIndex, DownloadEntity entity) {
mEntity = entity;
@@ -46,13 +51,11 @@ public class VideoPlayerFragment extends BaseFragment<FragmentVideoPlayerBinding
@Override protected void init(Bundle savedInstanceState) {
mModule = ViewModelProviders.of(this).get(M3U8VodModule.class);
final MediaPlayer firstPlayer = new MediaPlayer();
firstPlayer.setScreenOnWhilePlaying(true);
getBinding().surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override public void surfaceCreated(SurfaceHolder holder) {
mSurfaceHolder = holder;
firstPlayer.setSurface(holder.getSurface());
startNewPlayer(mPlayers.valueAt(0));
}
@Override
@@ -61,7 +64,12 @@ public class VideoPlayerFragment extends BaseFragment<FragmentVideoPlayerBinding
}
@Override public void surfaceDestroyed(SurfaceHolder holder) {
if (mCurrentPlayer != null) {
mCurrentPlayer.release();
}
if (mNextPlayer != null) {
mNextPlayer.getPlayer().release();
}
}
});
getBinding().seekBar.setMax(mEntity.getM3U8Entity().getPeerNum());
@@ -75,36 +83,66 @@ public class VideoPlayerFragment extends BaseFragment<FragmentVideoPlayerBinding
}
@Override public void onStopTrackingTouch(SeekBar seekBar) {
dataCallback(SEEK_BAR_PROGRESS_KEY, seekBar.getProgress());
PeerIndex index = new PeerIndex();
index.index = seekBar.getProgress();
EventBus.getDefault().post(index);
if (mCurrentPlayer != null && mCurrentPlayer.isPlaying()) {
mCurrentPlayer.stop();
mCurrentPlayer.setDisplay(null);
mCurrentPlayer.release();
}
showLoadingDialog();
mJumpIndex = seekBar.getProgress();
}
});
getBinding().controlBt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try {
firstPlayer.setDataSource(mPlayers.valueAt(0));
firstPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
if (mCurrentPlayer != null) {
if (mCurrentPlayer.isPlaying()) {
getBinding().controlBt.setSelected(true);
mCurrentPlayer.start();
} else {
getBinding().controlBt.setSelected(false);
mCurrentPlayer.stop();
}
}
}
});
firstPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override public void onPrepared(MediaPlayer mp) {
mp.start();
setNextMediaPlayer(mp);
}
});
firstPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override public void onCompletion(MediaPlayer mp) {
mp.setDisplay(null);
nexPlayer.getPlayer().setSurface(mSurfaceHolder.getSurface());
nexPlayer.start();
}
});
}
private void startNewPlayer(String source) {
if (TextUtils.isEmpty(source)) {
ALog.e(TAG, "资源路径为空");
return;
}
try {
mCurrentPlayer = new MediaPlayer();
mCurrentPlayer.setScreenOnWhilePlaying(true);
mCurrentPlayer.setSurface(mSurfaceHolder.getSurface());
mCurrentPlayer.setDataSource(source);
mCurrentPlayer.prepareAsync();
mCurrentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override public void onPrepared(MediaPlayer mp) {
mp.start();
setNextMediaPlayer(mp);
}
});
mCurrentPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override public void onCompletion(MediaPlayer mp) {
mp.setDisplay(null);
mNextPlayer.getPlayer().setSurface(mSurfaceHolder.getSurface());
mNextPlayer.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置下一个分片
*/
@@ -112,33 +150,53 @@ public class VideoPlayerFragment extends BaseFragment<FragmentVideoPlayerBinding
mPeerIndex++;
String nextPeerPath = mPlayers.get(mPeerIndex);
if (!TextUtils.isEmpty(nextPeerPath)) {
nexPlayer = new NextMediaPlayer(nextPeerPath);
nexPlayer.prepareAsync();
mNextPlayer = new NextMediaPlayer(nextPeerPath);
mNextPlayer.prepareAsync();
nexPlayer.setListener(new NextMediaPlayer.StateListener() {
mNextPlayer.setListener(new NextMediaPlayer.StateListener() {
@Override public void onStart(MediaPlayer mp) {
mCurrentPlayer = mp;
setNextMediaPlayer(mp);
}
@Override public void onCompletion(MediaPlayer mp) {
mp.setDisplay(null);
nexPlayer.getPlayer().setSurface(mSurfaceHolder.getSurface());
nexPlayer.start();
//mp.setDisplay(null);
//mNextPlayer.getPlayer().setSurface(mSurfaceHolder.getSurface());
mNextPlayer.start();
}
@Override public void onPrepared(MediaPlayer mp) {
lastPlayer.setNextMediaPlayer(nexPlayer.getPlayer());
lastPlayer.setNextMediaPlayer(mNextPlayer.getPlayer());
}
});
}
}
private void showLoadingDialog() {
if (mLoadingDialog == null) {
mLoadingDialog = new LoadingDialog(this);
}
if (!mLoadingDialog.isHidden()) {
mLoadingDialog.show(getChildFragmentManager(), "mLoadingDialog");
}
}
private void dismissDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
}
@Override protected int setLayoutId() {
return R.layout.fragment_video_player;
}
public void addPlayer(int peerIndex, String peerPath) {
mPlayers.put(peerIndex, peerPath);
if (mJumpIndex != -1 && mJumpIndex == peerIndex) {
startNewPlayer(mPlayers.get(peerIndex));
dismissDialog();
}
}
private static class NextMediaPlayer implements MediaPlayer.OnPreparedListener,

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.simple.to;
public class PeerIndex {
public int index;
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#99000000"/>
<corners android:radius="8dp"/>
</shape>

View File

@@ -15,6 +15,11 @@
name="progress"
type="int"
/>
<variable
name="stateStr"
type="String"
/>
</data>
<RelativeLayout
@@ -26,7 +31,7 @@
<include layout="@layout/layout_bar"/>
<include
layout="@layout/content_single"
layout="@layout/layout_content_single"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -34,6 +39,7 @@
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
bind:stateStr="@{stateStr}"
/>
<com.arialyy.simple.widget.SubStateLinearLayout

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/bg_loading"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</layout>

View File

@@ -12,7 +12,7 @@
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_height="240dp"
/>
@@ -31,7 +31,8 @@
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:clickable="true"
app:srcCompat="@drawable/ic_start"
android:visibility="visible"
app:srcCompat="@drawable/selector_start_stop_bt"
/>
<SeekBar