精简播放界面

This commit is contained in:
sifacaii 2022-08-29 18:38:58 +08:00
parent 43da3d5a88
commit 34237746e9
4 changed files with 99 additions and 162 deletions

View File

@ -12,11 +12,11 @@ import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import me.jessyan.autosize.internal.CustomAdapt; import me.jessyan.autosize.internal.CustomAdapt;
public class BaseActivity extends AppCompatActivity implements CustomAdapt { public class BaseActivity extends AppCompatActivity implements CustomAdapt {

View File

@ -5,10 +5,15 @@ import static android.net.sip.SipErrorCode.TIME_OUT;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import androidx.annotation.NonNull;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -29,6 +34,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import okhttp3.Callback;
import okhttp3.Headers; import okhttp3.Headers;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -45,7 +51,6 @@ public class Utils {
public static int playIndex = 0; //当前播放 public static int playIndex = 0; //当前播放
public static ArrayList<Video> playList = new ArrayList<>(); //播放列表 public static ArrayList<Video> playList = new ArrayList<>(); //播放列表
/** /**
* GET请求 * GET请求
* *
@ -158,39 +163,29 @@ public class Utils {
} }
/** /**
* 报告播放开始 * 报告播放状态
* * @param type
* @param Id
* @param PositionTicks * @param PositionTicks
*/ */
public static void ReportPlaying(String Id, long PositionTicks) { public static void ReportPlayState(ReportType type,String Id,long PositionTicks){
String url = config.getJellyfinUrl() + "/Sessions/Playing"; String url = config.getJellyfinUrl();
String json = "{\"itemId\":\"" + Id + "\",\"PositionTicks\":\"" + PositionTicks * 10000 + "\"}"; if(type == ReportType.playing){
String rsp = okhttpSend(url, json); url += "/Sessions/Playing";
//Log.d("VLC播放器", "ReportPlaying: " + Id + " : " + rsp); }else if(type == ReportType.Progress){
url += "/Sessions/Playing/Progress";
}else if(type == ReportType.stop){
url += "/Sessions/Playing/Stopped";
} }
/**
* 报告播放进度
*
* @param PositionTicks
*/
public static void ReportPlaybackProgress(String Id, long PositionTicks) {
String json = "{\"itemId\" : \"" + Id + "\",\"positionTicks\": \"" + PositionTicks * 10000 + "\"}";
String url = config.getJellyfinUrl() + "/Sessions/Playing/Progress";
String rsp = okhttpSend(url, json);
//Log.d("VLC播放器", "ReportPlaybackProgress: 返回:" + Id + ":" + rsp);
}
/**
* 播放停止
*
* @param PositionTicks
*/
public static void ReportPlaybackStop(String Id, long PositionTicks) {
String url = config.getJellyfinUrl() + "/Sessions/Playing/Stopped";
String json = "{\"itemId\":\"" + Id + "\",\"PositionTicks\":\"" + PositionTicks * 10000 + "\"}"; String json = "{\"itemId\":\"" + Id + "\",\"PositionTicks\":\"" + PositionTicks * 10000 + "\"}";
String rsp = okhttpSend(url, json); String finalUrl = url;
//Log.d("VLC播放器", "ReportPlaybackStop: " + Id + " : " + rsp); new Thread(new Runnable() {
@Override
public void run() {
String rsptxt = okhttpSend(finalUrl,json);
Log.d("Report", "run: " + rsptxt);
}
}).start();
} }
/** /**

View File

@ -3,6 +3,9 @@ package org.sifacai.vlcjellyfin;
import android.app.Activity; import android.app.Activity;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
@ -47,8 +50,6 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
private TextView countTime; private TextView countTime;
private TextView speedBtn; private TextView speedBtn;
private TextView scaleBtn; private TextView scaleBtn;
private ImageView preBtn;
private ImageView nextBtn;
private ImageView playPauseBtn; private ImageView playPauseBtn;
private ImageView stopBtn; private ImageView stopBtn;
private ImageView subTracksBtn; private ImageView subTracksBtn;
@ -56,9 +57,7 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
private ImageView playListBtn; private ImageView playListBtn;
private ImageView pauseFlag; private ImageView pauseFlag;
private SeekBar currPostion; private SeekBar currPostion;
private ProgressBar loading;
private Timer progressTime = null; //控制器进度条更新定时
private Timer reportProcessTime = null; // 报告进度定时器
private PopMenu playListMenu = null; //播放列表 private PopMenu playListMenu = null; //播放列表
private PopMenu subTrackMenu = null; //字幕菜单 private PopMenu subTrackMenu = null; //字幕菜单
@ -68,7 +67,9 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
private float speedRate[] = {0.5f, 1.0f, 1.5f, 2.0f}; //倍速播放列表 private float speedRate[] = {0.5f, 1.0f, 1.5f, 2.0f}; //倍速播放列表
private long currPlaybackTimeTrack = 0; //当前播放进度 private int updateTimeCount = 0;
private int ReportCount = 30;
private Video currItem; //当前播放项目
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -101,7 +102,7 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
Hide(); Hide();
pauseFlag.setVisibility(View.GONE); pauseFlag.setVisibility(View.GONE);
Log.d(TAG, "onEvent: Playing"); Log.d(TAG, "onEvent: Playing");
ReportPlayState(Utils.ReportType.playing, Utils.playList.get(Utils.playIndex).Id); ReportPlayState(Utils.ReportType.playing);
initMenu(); initMenu();
break; break;
case MediaPlayer.Event.Paused: //暂停 case MediaPlayer.Event.Paused: //暂停
@ -109,7 +110,7 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
break; break;
case MediaPlayer.Event.Stopped: case MediaPlayer.Event.Stopped:
Log.d(TAG, "onEvent: Stopped"); Log.d(TAG, "onEvent: Stopped");
ReportPlayState(Utils.ReportType.stop, Utils.playList.get(Utils.playIndex).Id); ReportPlayState(Utils.ReportType.stop);
playNext(); playNext();
break; break;
case MediaPlayer.Event.Opening: //媒体打开 case MediaPlayer.Event.Opening: //媒体打开
@ -117,9 +118,12 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
break; break;
case MediaPlayer.Event.Buffering: //媒体加载public float getBuffering() 获取加载视频流的进度0-100 case MediaPlayer.Event.Buffering: //媒体加载public float getBuffering() 获取加载视频流的进度0-100
int Buffering = (int) event.getBuffering(); int Buffering = (int) event.getBuffering();
setLoadingText("加载进度:%" + Buffering);
if (Buffering >= 100) { if (Buffering >= 100) {
dismissLoadingDialog(); loading.setVisibility(View.GONE);
}else{
if(loading.getVisibility() == View.GONE){
loading.setVisibility(View.VISIBLE);
}
} }
break; break;
case MediaPlayer.Event.EndReached://媒体播放结束 case MediaPlayer.Event.EndReached://媒体播放结束
@ -131,7 +135,12 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
stop(); stop();
break; break;
case MediaPlayer.Event.TimeChanged://视频时间变化 case MediaPlayer.Event.TimeChanged://视频时间变化
currPlaybackTimeTrack = event.getTimeChanged(); currItem.PositionTicks = event.getTimeChanged();
updateTimeCount +=1;
if(updateTimeCount > ReportCount){
updateTimeCount = 0;
ReportPlayState(Utils.ReportType.Progress);
}
break; break;
case MediaPlayer.Event.PositionChanged://视频总时长的百分比 case MediaPlayer.Event.PositionChanged://视频总时长的百分比
break; break;
@ -172,14 +181,11 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
countTime = findViewById(R.id.countTime); countTime = findViewById(R.id.countTime);
speedBtn = findViewById(R.id.speedBtn); speedBtn = findViewById(R.id.speedBtn);
scaleBtn = findViewById(R.id.scaleBtn); scaleBtn = findViewById(R.id.scaleBtn);
preBtn = findViewById(R.id.preBtn);
nextBtn = findViewById(R.id.nextBtn);
playPauseBtn = findViewById(R.id.playPauseBtn); playPauseBtn = findViewById(R.id.playPauseBtn);
stopBtn = findViewById(R.id.stopBtn); stopBtn = findViewById(R.id.stopBtn);
pauseFlag = findViewById(R.id.pauseFlag); pauseFlag = findViewById(R.id.pauseFlag);
currPostion = findViewById(R.id.currPostion); currPostion = findViewById(R.id.currPostion);
preBtn.setOnClickListener(this); loading = findViewById(R.id.loading);
nextBtn.setOnClickListener(this);
playPauseBtn.setOnClickListener(this); playPauseBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this); stopBtn.setOnClickListener(this);
pauseFlag.setOnClickListener(this); pauseFlag.setOnClickListener(this);
@ -189,9 +195,9 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
boolean rv = false; boolean rv = false;
int keycode = keyEvent.getKeyCode(); int keycode = keyEvent.getKeyCode();
if (keycode == KeyEvent.KEYCODE_DPAD_RIGHT) { if (keycode == KeyEvent.KEYCODE_DPAD_RIGHT) {
rv = setTimeOnSeekBar(currPlaybackTimeTrack + (long) (mediaPlayer.getLength() * 0.05)); rv = setTimeOnSeekBar(currItem.PositionTicks + (long) (mediaPlayer.getLength() * 0.05));
} else if (keycode == KeyEvent.KEYCODE_DPAD_LEFT) { } else if (keycode == KeyEvent.KEYCODE_DPAD_LEFT) {
rv = setTimeOnSeekBar(currPlaybackTimeTrack - (long) (mediaPlayer.getLength() * 0.05)); rv = setTimeOnSeekBar(currItem.PositionTicks - (long) (mediaPlayer.getLength() * 0.05));
} }
return rv; return rv;
} }
@ -276,7 +282,7 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
public void onClick(View view) { public void onClick(View view) {
playListMenu.dismiss(); playListMenu.dismiss();
if (m.id != Utils.playIndex) { if (m.id != Utils.playIndex) {
ReportPlayState(Utils.ReportType.stop, Utils.playList.get(Utils.playIndex).Id); ReportPlayState(Utils.ReportType.stop);
Utils.playIndex = m.id; Utils.playIndex = m.id;
play(); play();
} }
@ -330,6 +336,16 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
audioTracksBtn.setOnClickListener(this); audioTracksBtn.setOnClickListener(this);
} }
private Handler mhandler = new Handler(Looper.getMainLooper());
private Runnable mUpdateSeekBar = new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: 更新进度:" + currItem.PositionTicks);
setSeekBar(currItem.PositionTicks);
mhandler.postDelayed(mUpdateSeekBar,1000);
}
};
/** /**
* 显示控制器 * 显示控制器
*/ */
@ -345,39 +361,12 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
ControllerTop.setVisibility(View.VISIBLE); ControllerTop.setVisibility(View.VISIBLE);
} }
if (ControllerBottom.getVisibility() == View.GONE) { if (ControllerBottom.getVisibility() == View.GONE) {
progressTime = new Timer(); mhandler.postDelayed(mUpdateSeekBar,1000);
progressTime.schedule(new TimerTask() {
@Override
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
setSeekBar(currPlaybackTimeTrack);
}
});
}
}, 0, 1000);
ControllerBottom.setVisibility(View.VISIBLE); ControllerBottom.setVisibility(View.VISIBLE);
playPauseBtn.requestFocus(); playPauseBtn.requestFocus();
} }
} }
/**
* 设置进度条时间
*/
public void setSeekBar(Long p) {
if (ControllerBottom.getVisibility() == View.VISIBLE) {
double i = (double) p / 1000;
long duration = mediaPlayer.getLength();
if (duration > 0) {
long pos = 1000L * p / duration;
currPostion.setProgress((int) pos);
}
currTime.setText(TrickToTime(p));
countTime.setText(TrickToTime(duration));
}
}
/** /**
* 隐藏控制器 * 隐藏控制器
*/ */
@ -387,9 +376,24 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
} }
if (ControllerBottom.getVisibility() == View.VISIBLE) { if (ControllerBottom.getVisibility() == View.VISIBLE) {
ControllerBottom.setVisibility(View.GONE); ControllerBottom.setVisibility(View.GONE);
if (progressTime != null) { mhandler.removeCallbacks(mUpdateSeekBar);
progressTime.cancel(); }
progressTime = null; }
/**
* 设置进度条时间
*/
public void setSeekBar(Long p) {
if (ControllerBottom.getVisibility() == View.VISIBLE) {
if(null != mediaPlayer && mediaPlayer.getLength() > 0){
double i = (double) p / 1000;
long duration = mediaPlayer.getLength();
if (duration > 0) {
long pos = 1000L * p / duration;
currPostion.setProgress((int) pos);
}
currTime.setText(TrickToTime(p));
countTime.setText(TrickToTime(duration));
} }
} }
} }
@ -400,9 +404,9 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
public void play() { public void play() {
if (Utils.playList.size() > 0) { if (Utils.playList.size() > 0) {
if (Utils.playIndex < Utils.playList.size()) { if (Utils.playIndex < Utils.playList.size()) {
Video v = Utils.playList.get(Utils.playIndex); currItem = Utils.playList.get(Utils.playIndex);
videoTitle.setText(v.Name); videoTitle.setText(currItem.Name);
mediaPlayer.play(Uri.parse(v.Url)); mediaPlayer.play(Uri.parse(currItem.Url));
} }
} else { } else {
stop(); stop();
@ -414,36 +418,17 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
*/ */
public void playNext() { public void playNext() {
if (Utils.playIndex < (Utils.playList.size() - 1)) { if (Utils.playIndex < (Utils.playList.size() - 1)) {
ReportPlayState(Utils.ReportType.stop, Utils.playList.get(Utils.playIndex).Id); ReportPlayState(Utils.ReportType.stop);
Utils.playIndex += 1; Utils.playIndex += 1;
play(); play();
} }
} }
/**
* 上一集
*/
public void playPre() {
if (Utils.playIndex > 0) {
ReportPlayState(Utils.ReportType.stop, Utils.playList.get(Utils.playIndex).Id);
Utils.playIndex -= 1;
play();
}
}
/** /**
* 停止播放并结束Activity * 停止播放并结束Activity
*/ */
public void stop() { public void stop() {
ReportPlayState(Utils.ReportType.stop, Utils.playList.get(Utils.playIndex).Id); ReportPlayState(Utils.ReportType.stop);
if (progressTime != null) {
progressTime.cancel();
progressTime = null;
}
if (reportProcessTime != null) {
reportProcessTime.cancel();
progressTime = null;
}
mediaPlayer.stop(); mediaPlayer.stop();
mediaPlayer.release(); mediaPlayer.release();
libVLC.release(); libVLC.release();
@ -528,11 +513,7 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
@Override @Override
public void onClick(View view) { public void onClick(View view) {
int id = view.getId(); int id = view.getId();
if (id == R.id.nextBtn) { if (id == R.id.playPauseBtn) {
playNext();
} else if (id == R.id.preBtn) {
playPre();
} else if (id == R.id.playPauseBtn) {
playOrpause(); playOrpause();
} else if (id == R.id.stopBtn) { } else if (id == R.id.stopBtn) {
stop(); stop();
@ -549,33 +530,13 @@ public class VlcPlayerActivity extends BaseActivity implements MediaPlayer.Event
} }
} }
private void ReportPlayState(Utils.ReportType type, String Id) { private void ReportPlayState(Utils.ReportType type) {
if (type == Utils.ReportType.playing) { Utils.ReportPlayState(type,currItem.Id,currItem.PositionTicks);
reportProcessTime = new Timer();
reportProcessTime.schedule(new TimerTask() {
@Override
public void run() {
ReportPlayState(Utils.ReportType.Progress, Utils.playList.get(Utils.playIndex).Id);
}
}, 1000, 10000);
} else if (type == Utils.ReportType.stop) {
if (reportProcessTime != null) {
reportProcessTime.cancel();
reportProcessTime = null;
}
} }
new Thread(new Runnable() {
@Override @Override
public void run() { public void finish() {
if (type == Utils.ReportType.playing) { mhandler.removeCallbacksAndMessages(null);
Utils.ReportPlaying(Id, currPlaybackTimeTrack); super.finish();
} else if (type == Utils.ReportType.stop) {
Utils.ReportPlaybackStop(Id, currPlaybackTimeTrack);
} else if (type == Utils.ReportType.Progress) {
Utils.ReportPlaybackProgress(Id, currPlaybackTimeTrack);
}
}
}).start();
} }
} }

View File

@ -95,14 +95,6 @@
android:orientation="horizontal" android:orientation="horizontal"
android:paddingBottom="20dp"> android:paddingBottom="20dp">
<ImageView
android:id="@+id/preBtn"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:background="@drawable/shape_user_focus"
android:focusable="true"
android:src="@drawable/ic_outline_skip_previous_48" />
<ImageView <ImageView
android:id="@+id/playPauseBtn" android:id="@+id/playPauseBtn"
android:layout_width="@dimen/button_width" android:layout_width="@dimen/button_width"
@ -121,15 +113,6 @@
android:focusable="true" android:focusable="true"
android:src="@drawable/ic_outline_stop_48" /> android:src="@drawable/ic_outline_stop_48" />
<ImageView
android:id="@+id/nextBtn"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_marginLeft="@dimen/button_margin_left"
android:background="@drawable/shape_user_focus"
android:focusable="true"
android:src="@drawable/ic_outline_skip_next_48" />
<TextView <TextView
android:id="@+id/speedBtn" android:id="@+id/speedBtn"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -187,14 +170,6 @@
</LinearLayout> </LinearLayout>
</FrameLayout> </FrameLayout>
<ImageView
android:id="@+id/loading"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:src="@android:drawable/ic_input_get"
android:visibility="gone" />
<ImageView <ImageView
android:id="@+id/pauseFlag" android:id="@+id/pauseFlag"
android:layout_width="160dp" android:layout_width="160dp"
@ -202,4 +177,10 @@
android:layout_gravity="center" android:layout_gravity="center"
android:src="@drawable/ic_baseline_pause_circle_outline_48" android:src="@drawable/ic_baseline_pause_circle_outline_48"
android:visibility="gone" /> android:visibility="gone" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center" />
</FrameLayout> </FrameLayout>