Skip to content

Commit

Permalink
1.修复直接全屏模式下controller和navigation bar重叠问题
Browse files Browse the repository at this point in the history
2.将清理缓存的方法返回值改为boolean型,以便确认缓存是否删除成功
3.添加恢复默认屏幕大小的方法
4.给demo增加权限申请
5.升级版本至1.0.3
  • Loading branch information
Doikki committed May 23, 2017
1 parent 73ace13 commit 7f1b1be
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Step 1.Add it in your root build.gradle at the end of repositories:
Step 2. Add the dependency

dependencies {
compile 'com.github.DevlinChiu:MagicPlayer:1.0.2'
compile 'com.github.DevlinChiu:MagicPlayer:1.0.3'
}
### ProGuard

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.setTitle("这是一个标题")
.setUrl("http://flv2.bn.netease.com/videolib3/1611/28/GbgsL3639/HD/movie_index.m3u8")
.setVideoController(MagicVideoView.VOD)
.setScreenType(MagicVideoView.SCREEN_TYPE_4_3)
.setScreenType(MagicVideoView.SCREEN_TYPE_16_9)
.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class LivePlayerActivity extends AppCompatActivity {

private MagicVideoView magicVideoView;
private static final String URL = "http://ivi.bupt.edu.cn/hls/cctv6.m3u8";
private static final String URL = "http://ivi.bupt.edu.cn/hls/hunanhd.m3u8";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -43,7 +43,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.init()
.autoRotate()
.setUrl(URL)
.setTitle("CCTV6")
.setTitle("湖南卫视")
.setVideoController(MagicVideoView.LIVE)
.start();
}
Expand Down Expand Up @@ -117,4 +117,8 @@ public void match(View view) {
public void original(View view) {
magicVideoView.setScreenType(MagicVideoView.SCREEN_TYPE_ORIGINAL);
}

public void defaultSize(View view) {
magicVideoView.setScreenType(MagicVideoView.SCREEN_TYPE_DEFAULT);
}
}
14 changes: 13 additions & 1 deletion app/src/main/java/com/devlin_n/magicplayer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package com.devlin_n.magicplayer;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.devlin_n.magic_player.player.BackgroundPlayService;
import com.devlin_n.magic_player.player.VideoCacheManager;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//用于将视频缓存到SDCard,如不授权将缓存到/data/data目录
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
}
}

public void skipToVodPlayer(View view) {
Expand All @@ -34,6 +44,8 @@ public void startFullScreen(View view) {
}

public void clearCache(View view) {
VideoCacheManager.clearAllCache(this);
if (VideoCacheManager.clearAllCache(this)) {
Toast.makeText(this, "清除缓存成功", Toast.LENGTH_SHORT).show();
}
}
}
19 changes: 15 additions & 4 deletions app/src/main/res/layout/activity_live_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,37 @@
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="defaultSize"
android:text="default" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="original"
android:text="original" />

<Button
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="wide"
android:text="16:9" />

<Button
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tv"
android:text="4:3" />

<Button
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="match"
android:text="match" />

Expand Down
4 changes: 2 additions & 2 deletions magic_player/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 3
versionName "1.0.2"
versionCode 4
versionName "1.0.3"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public class MagicVideoView extends FrameLayout implements MagicVideoController.

private AudioManager mAudioManager;//系统音频管理器

public static final int SCREEN_TYPE_DEFAULT = 0;
public static final int SCREEN_TYPE_16_9 = 1;
public static final int SCREEN_TYPE_4_3 = 2;
public static final int SCREEN_TYPE_MATCH_PARENT = 3;
public static final int SCREEN_TYPE_ORIGINAL = 4;
private int mCurrentScreenType;

/**
* 加速度传感器监听
Expand Down Expand Up @@ -142,7 +142,13 @@ public void onWindowFocusChanged(boolean hasFocus) {
originalWidth = getWidth();
originalHeight = getHeight();
if (mMediaController != null) {
mMediaController.setLayoutParams(new LayoutParams(originalWidth, originalHeight));
if (isFullScreen) {
//如果直接全屏播放器,需要去掉navigation bar的高度,不能直接使用获取的原始高宽
mMediaController.setLayoutParams(new LayoutParams(WindowUtil.getScreenWidth(getContext()),
WindowUtil.getScreenHeight(getContext(), false)));
} else {
mMediaController.setLayoutParams(new LayoutParams(originalWidth, originalHeight));
}
}
}
}
Expand Down Expand Up @@ -233,8 +239,7 @@ public void surfaceDestroyed(SurfaceHolder holder) {
* 设置视频比例
*/
public MagicVideoView setScreenType(int type) {
mCurrentScreenType = type;
surfaceView.setScreenType(mCurrentScreenType);
surfaceView.setScreenType(type);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import java.io.File;

public class VideoCacheManager {

private static HttpProxyCacheServer sharedProxy;

private VideoCacheManager() {
}

public static HttpProxyCacheServer getProxy(Context context) {
return sharedProxy == null ? (sharedProxy = newProxy(context)) : sharedProxy;
}

private static HttpProxyCacheServer newProxy(Context context) {
return new HttpProxyCacheServer.Builder(context)
.maxCacheSize(1024 * 1024 * 1024) // 1 Gb for cache
Expand All @@ -28,16 +28,18 @@ private static HttpProxyCacheServer newProxy(Context context) {

/**
* 删除所有缓存文件
* @return 返回缓存是否删除成功
*/
public static void clearAllCache(Context context) {
public static boolean clearAllCache(Context context) {
File cacheDirectory = StorageUtil.getIndividualCacheDirectory(context);
StorageUtil.deleteFiles(cacheDirectory);
return StorageUtil.deleteFiles(cacheDirectory);
}

/**
* 删除url对应默认缓存文件
* @return 返回缓存是否删除成功
*/
public static void clearDefaultCache(Context context, String url) {
public static boolean clearDefaultCache(Context context, String url) {
Md5FileNameGenerator md5FileNameGenerator = new Md5FileNameGenerator();
String name = md5FileNameGenerator.generate(url);
String pathTmp = StorageUtil.getIndividualCacheDirectory
Expand All @@ -46,8 +48,8 @@ public static void clearDefaultCache(Context context, String url) {
String path = StorageUtil.getIndividualCacheDirectory
(context.getApplicationContext()).getAbsolutePath()
+ File.separator + name;
StorageUtil.deleteFile(pathTmp);
StorageUtil.deleteFile(path);
return StorageUtil.deleteFile(pathTmp) &&
StorageUtil.deleteFile(path);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,40 @@ private static File getExternalCacheDir(Context context) {
/**
* delete directory
*/
public static void deleteFiles(File root) {
public static boolean deleteFiles(File root) {
File files[] = root.listFiles();
if (files != null) {
for (File f : files) {
if (!f.isDirectory() && f.exists()) { // 判断是否存在
try {
f.delete();
} catch (Exception e) {
e.printStackTrace();
if (!f.delete()) {
return false;
}
}
}
}
return true;
}

/**
* delete file
*/
public static void deleteFile(String filePath) {
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.isFile()) {
file.delete();
if (!file.delete()) {
return false;
}
} else {
String[] filePaths = file.list();
for (String path : filePaths) {
deleteFile(filePath + File.separator + path);
}
file.delete();
if (!file.delete()) {
return false;
}
}
}
return true;
}
}

0 comments on commit 7f1b1be

Please sign in to comment.