Skip to content

Commit

Permalink
Merge pull request #12 from ksvc/update
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
FirmianaRain authored Aug 21, 2017
2 parents b4e2e62 + 764c8c5 commit 2a26ae2
Show file tree
Hide file tree
Showing 18 changed files with 623 additions and 91 deletions.
Binary file modified HTTPCacheDemo/app/libs/libksyhttpcache.jar
Binary file not shown.
5 changes: 4 additions & 1 deletion HTTPCacheDemo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<activity android:name=".VideoPlayerActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
<activity android:name=".activity.PreDownloadActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(intent);
}
});
proxy.shutDownServer();

}

@Override
public void onDetach() {
super.onDetach();
proxy.shutDownServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
return view ;
}


private void initView() {
proxy = App.getKSYProxy(getActivity());
proxy.startServer();
Expand Down Expand Up @@ -76,6 +75,5 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(intent);
}
});
proxy.shutDownServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ksy.Cache.demo.activity.PreDownloadActivity;

import java.util.ArrayList;
import java.util.HashMap;

Expand Down Expand Up @@ -41,7 +43,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
map1.put("url","http://test.live.ks-cdn.com/live/test_sujia.flv");
map2.put("url","http://zbvideo.ks3-cn-beijing.ksyun.com/record/live/101743_1466076252/hls/101743_1466076252.m3u8");
map3.put("url","https://mvvideo5.meitudata.com/571090934cea5517.mp4");
map4.put("url","http://kss.ksyun.com/eflakee/FLV/15702274_1474868965.flv");
map4.put("url","http://opq32kjt8.bkt.clouddn.com/7tuan_8lian_23hao_2017_06_08_11.mp4");

urllist.add(map1);
urllist.add(map2);
Expand All @@ -55,8 +57,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(),VideoPlayerActivity.class);
intent.putExtra("path", urllist.get(i).get("url"));
Intent intent = new Intent(getActivity(),PreDownloadActivity.class);
startActivity(intent);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
* @author [email protected]
* 2015/5/19.
*/
public class VideoPlayerActivity extends Activity implements View.OnClickListener,
OnCacheStatusListener, OnErrorListener, OnLogEventListener {
public class VideoPlayerActivity extends Activity implements View.OnClickListener, OnCacheStatusListener, OnErrorListener, OnLogEventListener {

private static final String TAG = "VideoPlayerActivity";

Expand All @@ -62,7 +61,6 @@ public class VideoPlayerActivity extends Activity implements View.OnClickListene

private SharedPreferences settings;
private String choosedecode;
private String choosecache;

private long length;
private int cachelength;
Expand Down Expand Up @@ -447,7 +445,6 @@ protected void onDestroy() {

proxy.unregisterCacheStatusListener(this, mDataSource);
proxy.unregisterErrorListener(this);
proxy.shutDownServer();
}

@Override
Expand All @@ -463,11 +460,14 @@ protected void onPause() {
@Override
protected void onResume() {
super.onResume();
if (mVideoView != null)
if(mVideoView != null)
{
mVideoView.runInForeground();
mPause = false;
mPlayerStartBtn.setBackgroundResource(R.drawable.qyvideo_start_btn);
}
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
Expand Down Expand Up @@ -726,18 +726,6 @@ protected void startcache(){
proxy.registerCacheStatusListener(this, mDataSource);
proxy.registerErrorListener(this);
proxy.registerLogEventListener(this);
choosecache= settings.getString("choose_cache","undefind");
if(choosecache.equals(Settings.USENUM)){
proxy.setMaxFilesCount(500);
Log.d(TAG,"文件数量");
}else{
proxy.setMaxCacheSize(500*1024*1024);
Log.d(TAG,"文件大小");
}

proxy.startServer();

// proxy.setCacheRoot(VideoPlayerActivity.this.getExternalCacheDir());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package com.ksy.Cache.demo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.kingsoft.media.httpcache.KSYProxyService;
import com.kingsoft.media.httpcache.OnCacheStatusListener;
import com.kingsoft.media.httpcache.OnErrorListener;
import com.ksy.Cache.demo.App;
import com.ksy.Cache.demo.MyVideo;
import com.ksy.Cache.demo.R;
import com.ksy.Cache.demo.Settings;
import com.ksy.Cache.demo.VideoPlayerActivity;
import com.ksy.Cache.demo.adapter.PreDownloadAdapter;
import com.ksy.Cache.demo.fragment.SelectDialogFragment;
import com.ksy.Cache.demo.model.PreDownloadStatus;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

/**
* Created by xbc on 2017/8/15.
*/

public class PreDownloadActivity extends Activity {

private ListView mListView;
private SelectDialogFragment mSelectDialogFragment;

private PreDownloadAdapter mPreDownloadAdapter;
private ArrayList<PreDownloadStatus> mPreDownloadList;

private KSYProxyService mProxyService;

private SharedPreferences mSettings;

private int mSelectIndex;
private String[] mPlayUrls = {
"https://ks3-cn-beijing.ksyun.com/mobile/264/264_1200_30_720P_yx.mp4",
"https://mvvideo5.meitudata.com/571090934cea5517.mp4",
"https://ks3-cn-beijing.ksyun.com/mobile/170621.MOV"};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_url);

mProxyService = App.getKSYProxy(this);

mListView = (ListView) findViewById(R.id.list_url);

mSettings = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);

setDownloadListInfo();

mPreDownloadAdapter = new PreDownloadAdapter(this, mPreDownloadList);
mListView.setAdapter(mPreDownloadAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);
}

@Override
protected void onDestroy() {
super.onDestroy();
mPreDownloadList.clear();

for (String url : mPlayUrls)
mProxyService.unregisterCacheStatusListener(mCacheStatusListener, url);
mProxyService.unregisterErrorListener(mOnErrorListener);

mProxyService.shutDownServer();
}

private void setDownloadListInfo() {
ArrayList<String> urls = new ArrayList<>(Arrays.asList(mPlayUrls));
mPreDownloadList = new ArrayList<>();

mProxyService.registerErrorListener(mOnErrorListener);
String cacheMethod= mSettings.getString("choose_cache","undefind");
if(cacheMethod.equals(Settings.USENUM)){
mProxyService.setMaxFilesCount(500);
}else{
mProxyService.setMaxCacheSize(1000*1024*1024);
}

String url = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheTest";
File file = new File(url);
if (!file.exists())
file.mkdirs();

mProxyService.setCacheRoot(file);
mProxyService.startServer();

Iterator iterator = mProxyService.getCachedFileList().entrySet().iterator();
while (iterator != null && iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
url = (String)entry.getKey();
if (urls.contains(url)) {
PreDownloadStatus status = new PreDownloadStatus(url, 100);
mPreDownloadList.add(status);
urls.remove(url);
}
}
for(String url1 : urls) {
int percent = mProxyService.getCachingPercent(url1);
PreDownloadStatus status = new PreDownloadStatus(url1, percent);
mPreDownloadList.add(status);
mProxyService.registerCacheStatusListener(mCacheStatusListener, url1);
}
}

private void showDialogFragment() {
if (mSelectDialogFragment == null) {
mSelectDialogFragment = new SelectDialogFragment();
mSelectDialogFragment.setOnSelectItemListener(mOnSelectItemListener);
}

mSelectDialogFragment.show(getFragmentManager(), "select_dialog_fragment");
}

private AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mSelectIndex = i;
showDialogFragment();
}
};

private OnCacheStatusListener mCacheStatusListener = new OnCacheStatusListener() {
@Override
public void OnCacheStatus(String url, long sourceLength, int percentsAvailable) {
for(PreDownloadStatus status : mPreDownloadList) {
if (url.equals(status.url))
{
status.progress = percentsAvailable;
mPreDownloadAdapter.notifyDataSetChanged();
break;
}
}

Log.e("PreDownloadActivity", "===== OnCacheStatusListener url:"+url+", percent:"+percentsAvailable);
}
};

private OnErrorListener mOnErrorListener = new OnErrorListener() {
@Override
public void OnError(int errCode) {
Toast.makeText(PreDownloadActivity.this, "HttpCache meet an error:"+errCode, Toast.LENGTH_SHORT).show();
}
};

private SelectDialogFragment.OnSelectItemListener mOnSelectItemListener = new SelectDialogFragment.OnSelectItemListener() {
@Override
public void onStartPreLoad() {
if (mProxyService != null) {
mPreDownloadList.get(mSelectIndex).isCaching = true;
mPreDownloadAdapter.notifyDataSetChanged();
mProxyService.startPreDownload(mPreDownloadList.get(mSelectIndex).url);
}
}

@Override
public void onStopPreLoad() {
if (mProxyService != null) {
mPreDownloadList.get(mSelectIndex).isCaching = false;
mPreDownloadAdapter.notifyDataSetChanged();
mProxyService.stopPreDownload(mPreDownloadList.get(mSelectIndex).url);
}
}

@Override
public void onPlayVideo() {
Intent intent = new Intent(PreDownloadActivity.this, VideoPlayerActivity.class);
intent.putExtra("path", mPreDownloadList.get(mSelectIndex).url);
startActivity(intent);
}
};
}
Loading

0 comments on commit 2a26ae2

Please sign in to comment.