import
java.io.BufferedReader;
import
java.io.
File
;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
com.z.media.MediaFileDown;
import
com.z.media.MediaFileDown.OnCallCompleteListener;
import
com.z.media.MediaFileDown.OnDownloadingListener;
import
com.z.media.MediaPlayer;
import
com.z.media.listener.OnBufferingUpdateListener;
import
com.z.media.listener.OnCompleteListener;
import
com.z.media.listener.OnErrorListener;
import
com.z.media.listener.OnLoadListener;
import
com.z.media.listener.OnParparedListener;
import
com.z.media.listener.OnTimeInfoListener;
import
com.z.media.listener.OnValumeDBListener;
import
com.z.media.listener.TimeInfoBean;
import
com.z.media.mutee.ScaleType;
import
com.z.utils.PermissionUtil;
import
com.z.utils.T;
import
com.z.view.VideoView;
import
android.app.Activity;
import
android.media.MediaCodec;
import
android.os.Bundle;
import
android.os.Environment;
import
android.text.TextUtils;
import
android.util.Log;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.SeekBar;
import
android.widget.TextView;
import
android.widget.Toast;
/
*
*
*
*
@ClassName: MainActivity.java
*
@author zcs
*
@version V1.
0
*
@Date
2019
年
1
月
16
日 上午
9
:
47
:
17
*
@Description: 网络播放包括 {rtmp,http}
*
/
/
ffmpeg :
file
,ftp,http,https,httpproxy,hls,mmsh,mmst,pipe,rtmp,rtmps,rtmpt,rtmpts,rtp,sctp,srtp,tcp,udp
*
*
/
public
class
MainActivity extends Activity {
private MediaPlayer mediaPlayer;
private MediaFileDown fileDown;
String TAG
=
"MainActivity"
;
TextView tvTime;
int
posttion;
SeekBar seekBar;
SeekBar seekBar_volume;
boolean isTouch;
private boolean isSeekBarDown
=
false;
private EditText inputView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputView
=
(EditText) findViewById(R.
id
.et_input);
boolean check
=
PermissionUtil.check(this, PermissionUtil.READ_EXTERNAL_STORAGE);
if
(check) {
T.showShort(this,
"有读写权限"
);
}
else
{
T.showShort(this,
"没有读写权限"
);
}
String url
=
Environment.getExternalStorageDirectory()
+
File
.separator
+
"1234.mp4"
;
inputView.setText(url);
seekBar
=
(SeekBar) findViewById(R.
id
.seekBar);
seekBar_volume
=
(SeekBar) findViewById(R.
id
.seekBar_volume);
seekBar.setMax(
100
);
seekBar.setProgress(
0
);
seekBar_volume.setMax(
100
);
seekBar_volume.setProgress(
50
);
tvTime
=
(TextView) findViewById(R.
id
.tv_time);
VideoView videoView
=
(VideoView) findViewById(R.
id
.gl_surface);
mediaPlayer
=
new MediaPlayer();
mediaPlayer.setEnableHardware(false);
mediaPlayer.setWindowMode(ScaleType.STRETCH);
/
/
设置最大视频可缓冲时间(秒,<
=
0
不做最大缓冲
mediaPlayer.setNetMaxBuffer(
60
);
/
/
设置最小视频可缓冲时间(秒,不到达此时间,将不播放,<
=
0
不做最小缓冲
mediaPlayer.setNetMinBuffer(
5
);
/
/
mediaPlayer.setOnCheckDecodeListener(new OnCheckDecodeListener() {
/
/
/
/
StringBuffer sb
=
new StringBuffer();
/
/
@Override
/
/
public void onCheckDecode(
int
type
, String name) {
/
/
if
(
type
=
=
1
) {
/
/
/
/
sb.append(
"VIDEO"
+
":"
+
name);
/
/
Log.e(TAG,
"VIDEO "
+
name);
/
/
}
else
if
(
type
=
=
2
) {
/
/
/
/
sb.append(
"Audio"
+
":"
+
name);
/
/
Log.e(TAG,
"Audio "
+
name);
/
/
}
else
{
/
/
/
/
sb.append(
"Other"
+
":"
+
name);
/
/
Log.e(TAG,
"Other "
+
name);
/
/
}
/
/
/
/
Log.e(TAG,sb.toString());
/
/
}
/
/
});
mediaPlayer.setVideoView(videoView);
mediaPlayer.setOnPreparedListener(new OnParparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e(TAG,
"准备好了,可以播放"
);
if
(mp.isPause()) {
Log.e(TAG,
"pause 暂停"
);
}
else
{
mp.start();
}
int
width
=
mp.getWidth();
int
height
=
mp.getHeight();
/
/
Log.e(TAG,
"width:"
+
width
+
" height:"
+
height);
Log.e(TAG,
"width:"
+
width
+
" height:"
+
height);
Log.e(TAG,
" 硬解码: "
+
mediaPlayer.isHardware());
int
fps
=
mp.getFps();
Log.e(TAG,
"fps: "
+
fps);
if
(mediaPlayer.getDuration()>
0
) {
Log.e(TAG,
"播放位置:"
+
mediaPlayer.getCurrentPosition());
long
curr
=
mediaPlayer.getCurrentPosition()
/
1000
;
long
total
=
mediaPlayer.getDuration()
/
1000
;
Log.e(TAG,
"播放位置:"
+
curr);
Log.e(TAG,
"时长:"
+
total);
tvTime.setText(getTime(curr)
+
" / "
+
getTime(total));
/
/
Log.e(TAG,
"当前播放位置: "
+
curr);
/
/
Log.e(TAG,
"总时长: "
+
total);
if
(total>
0
) {
int
p
=
(
int
) (curr
*
100
/
total);
/
/
Log.e(TAG,
"seek "
+
p);
seekBar.setProgress(p);
}
}
}
});
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp,
int
percent,
long
second) {
long
duration
=
mediaPlayer.getDuration();
/
/
second
=
second
/
1000
;
/
/
Log.e(TAG,
"视频时长(秒) : "
+
duration
/
1000
);
Log.w(TAG,
"缓冲时长(秒) : "
+
second
/
1000
+
"秒 "
+
percent
+
" %"
);
if
(duration>
0
) {
long
currentPosition
=
mediaPlayer.getCurrentPosition();
int
pragress
=
(
int
) (currentPosition
+
second);
/
/
Log.e(TAG,
"缓冲位置(毫秒) : "
+
pragress);
/
/
Log.e(TAG,
"缓冲位置 : "
+
pragress);
float
scale
=
pragress
/
(
float
)duration;
if
(scale<
=
0
)
return
;
/
/
Log.e(TAG,
"进度条缓冲占比(秒) : "
+
scale);
int
progre
=
(
int
) (seekBar.getMax()
*
scale);
/
/
Log.e(TAG,
"设置进度条缓冲位置% : "
+
progre);
/
/
Log.e(TAG,
"进度条最大长度 : "
+
seekBar.getMax());
/
/
Log.e(TAG,
"播放进度条: "
+
seekBar.getSecondaryProgress());
Log.w(TAG,
"缓冲进度条: "
+
progre);
seekBar.setSecondaryProgress(progre);
/
/
seekBar.setSecondaryProgress(
80
);
/
/
pragress
/
(
float
)duration;
}
/
/
Log.e(TAG,
"缓冲进度: % "
+
percent);
/
/
Log.e(TAG,
"缓冲总时长(秒): "
+
second);
/
/
Log.e(TAG,
"总时长(秒): "
+
duration);
/
/
Log.e(TAG,
"percent: "
+
percent);
/
/
Log.e(TAG,
"second : "
+
second);
}
});
mediaPlayer.setOnLoadListener(new OnLoadListener(){
@Override
public void onLoad(boolean load) {
if
(load){
/
/
正在加载
Log.w(TAG,
"OnLoadListener 加载中"
);
}
else
{
/
/
加载完成
Log.w(TAG,
"OnLoadListener 播放中"
);
}
Log.w(TAG,
"OnLoadListener pause "
+
mediaPlayer.isPause());
}
});
mediaPlayer.setOnTimedTextListener(new OnTimeInfoListener() {
@Override
public void onTimeInfo(TimeInfoBean timeInfoBean) {
/
/
Log.e(TAG,timeInfoBean.toString());
/
/
Log.e(TAG,
"onTimeInfo "
+
timeInfoBean.getCurrentTime());
/
/
Log.w(TAG,
"------- onTimeInfo --------"
);
long
curr
=
timeInfoBean.getCurrentTime()
/
1000
;
long
total
=
timeInfoBean.getTotalTime()
/
1000
;
tvTime.setText(getTime(curr)
+
"/"
+
getTime(total));
/
/
Log.w(TAG,
"当前播放位置(秒: "
+
curr);
/
/
Log.w(TAG,
"总时长: "
+
total);
/
/
Log.w(TAG,
"getCurrentPosition: "
+
mediaPlayer.getCurrentPosition());
/
/
Log.e(TAG,
"剩余时间: "
+
(total
-
curr));
if
(!isTouch && total>
0
) {
if
(isSeekBarDown)
return
;
int
p
=
(
int
) (curr
*
100
/
total);
/
/
Log.e(TAG,
"seek "
+
p);
seekBar.setProgress(p);
}
/
/
handler.sendMessage(msg);
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp,
int
code, String extra) {
if
(fileDown
=
=
null || (!fileDown.isLoading()&&!fileDown.isPause())) {
Log.e(TAG,
"-----onError----- "
+
code);
}
Log.e(TAG,
"----------"
);
Log.e(TAG,
"error code:"
+
code);
Log.e(TAG,extra);
Log.e(TAG,
"----------"
);
if
(code >
=
1500
) {
Toast.makeText(MainActivity.this,
"播放文件出错"
, Toast.LENGTH_SHORT).show();
}
else
{
/
/
Log.e(TAG,
"is File "
);
}
/
/
if
(code
=
=
1001
) {
/
/
String source
=
mediaPlayer.getSource();
/
/
Log.e(TAG,
"is File "
+
new
File
(source).isFile());
/
/
}
return
false;
}
});
mediaPlayer.setOnCompletionListener(new OnCompleteListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e(TAG,
"播放结束"
);
Toast.makeText(getApplicationContext(),
"播放结束"
, Toast.LENGTH_LONG).show();
}
});
mediaPlayer.setOnValumeDBListener(new OnValumeDBListener() {
@Override
public void onDbValue(
int
db) {
/
/
Log.e(TAG,
"分贝值:"
+
db);
}
});
/
/
录音的监听
/
/
mediaPlayer.setOnRecordTimeListener(new OnRecordTimeListener(){
/
/
@Override
/
/
public void onRecordTime(
int
recordTime) {
/
/
/
/
Log.e(TAG,
"录制时间 "
+
recordTime);
/
/
}
/
/
});
/
/
mediaPlayer.setOnPcmInfoListener(new OnPcmInfoListener() {
/
/
@Override
/
/
public void onPcmInfo(byte[]
buffer
,
int
bufsize) {
/
/
Log.e(TAG,bufsize
+
"");
/
/
/
/
}
/
/
@Override
/
/
public void onPcmRate(
int
samplerate,
int
bit,
int
channels) {
/
/
采样率
/
/
Log.e(TAG,
"采样率 "
+
samplerate);
/
/
}
/
/
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar,
int
i, boolean b) {
isSeekBarDown
=
b;
if
(b) {
/
/
mediaPlayer.pause();
int
progress
=
i
*
100
/
seekBar.getMax();
progress
=
(
int
) (mediaPlayer.getDuration()
*
progress
/
100
);
tvTime.setText(getTime(progress
/
1000
));
}
/
/
if
(b&&mediaPlayer.getDuration()>
0
) {
/
/
int
progress
=
i
*
100
/
seekBar.getMax();
/
/
progress
=
(
int
) (mediaPlayer.getDuration()
*
progress
/
100
);
/
/
/
/
tvTime.setText(getTime(progress
/
1000
));
/
/
}
/
/
posttion
=
(
int
) (i
/
100.f
*
mediaPlayer.getDuration());
System.out.printf(""
+
posttion);
/
/
if
(mediaPlayer.getDuration()>
0
&&b) {
/
/
手动触发
/
/
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
/
/
滑动时
isTouch
=
true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isSeekBarDown
=
false;
int
progress
=
seekBar.getProgress()
*
100
/
seekBar.getMax();
progress
=
(
int
) (mediaPlayer.getDuration()
*
progress
/
100
);
Log.e(TAG,
"快进(毫秒)--> "
+
progress
/
1000
);
if
(mediaPlayer.getDuration()>
0
) {
/
/
if
(posttion<mediaPlayer.getCurrentPosition()) {
/
/
seekBar.setSecondaryProgress(
0
);
/
/
}
/
/
Log.e(TAG,
"seekBar 获取播放位置 "
+
mediaPlayer.getCurrentPosition());
Log.e(TAG,
"视频总时长 "
+
mediaPlayer.getDuration()
/
1000
);
mediaPlayer.seek(progress
/
1000
);
}
isTouch
=
false;
}
});
seekBar_volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar,
int
i, boolean b) {
mediaPlayer.setVolume(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
/
/
/
/
滑动时
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
/
/
mediaPlayer.seek(posttion);
/
/
isTouch
=
false;
}
});
}
/
*
*
*
开始解析按钮
*
@param view
*
/
public void begin(View view) {
/
/
if
(mediaPlayer.isPlaying()) {
/
/
Log.e(TAG,
"正在播放中... "
+
mediaPlayer.getCurrentPosition());
/
/
return
;
/
/
}
if
(mediaPlayer.isPause()) {
mediaPlayer.resume();
return
;
}
seekBar.setSecondaryProgress(
0
);
seekBar.setProgress(
0
);
mediaPlayer.setWindowMode(ScaleType.FIT_ADAPT);
/
/
显示方式,默认即可
String CPU_ABI
=
android.os.Build.CPU_ABI;
Toast.makeText(this, CPU_ABI, Toast.LENGTH_SHORT).show();
Log.e(TAG,CPU_ABI);
String url
=
inputView.getText().toString();
if
(TextUtils.isEmpty(url)) {
T.showShort(this,
"没有播放地址"
);
return
;
}
Log.e(TAG,
"start:"
+
url);
mediaPlayer.setSource(url.trim());
/
/
设置要播放的链接
mediaPlayer.prepare();
/
/
mediaPlayer.setSaveVideoFile(Environment.getExternalStorageDirectory()
+
File
.separator
+
System.currentTimeMillis()
+
".mp4"
);
}
private String getUrl() {
String url
=
"http://mpge.5nd.com/2015/2015-11-26/69708/1.mp3"
;
return
url;
}
public void
next
(View view) {
/
/
mediaPlayer.setWindowMode(ScaleType.CENTRE);
seekBar.setProgress(
0
);
MediaCodec mediaCodec
=
mediaPlayer.getMediaCodec();
System.out.println(mediaCodec);
/
/
下一个
String url
=
Environment.getExternalStorageDirectory()
+
File
.separator
+
".mov"
;
/
/
mediaPlayer.playNext(url);
}
/
*
*
*
软
/
硬解码切换
*
@param view
*
/
public void switch_decode(View view) {
/
/
boolean hardware
=
mediaPlayer.isHardware();
mediaPlayer.setEnableHardware(!mediaPlayer.isEnadblHardware());
Log.e(TAG,
"setEnableHardware : "
+
mediaPlayer.isEnadblHardware());
}
/
*
*
*
暂停
*
@param view
*
/
public void pause(View view) {
mediaPlayer.pause();
}
/
*
*
*
播放
*
@param view
*
/
public void resume(View view) {
mediaPlayer.resume();
}
/
*
*
*
*
@param time 秒
*
@
return
*
/
private String getTime(
long
time) {
/
/
String string
=
"";
/
/
System.out.printf(String.
format
(
"00"
,
1
));
if
(time >
60
*
60
) {
/
/
小时
return
time
/
3600
+
":"
+
(time
%
3600
/
60
)
+
":"
+
(time
%
3600
%
60
);
}
else
if
(time >
60
) {
/
/
分钟
return
time
/
60
+
":"
+
time
%
60
;
}
else
if
(time<
0
){
return
"长度未知"
;
}
else
{
return
"00:"
+
time;
}
}
public void stop(View view) {
mediaPlayer.stop();
}
public void seek(View view) {
int
l
=
(
int
) (mediaPlayer.getDuration()
/
1000
-
10
);
l
=
(
int
) (mediaPlayer.getCurrentPosition()
/
1000
+
10
);
Log.e(TAG,
"MainActivity seek "
+
l
+
"");
mediaPlayer.seekTo(l);
/
/
mediaPlayer.seek(
3
*
60
+
35
);
/
/
2
分钟
}
@Override
protected void onPause() {
super
.onPause();
Log.e(TAG,
"onPause"
);
mediaPlayer.pause();
/
/
暂停下载
if
(fileDown!
=
null) {
/
/
fileDown.pause();
}
}
@Override
protected void onDestroy() {
mediaPlayer.stop();
/
/
new android.media.MediaPlayer().release();
mediaPlayer.release();
super
.onDestroy();
}
public void center(View view) {
/
/
立体声
/
/
media.setMute(MuteEnum.MUTE_CENTER);
}
public void left(View view) {
/
/
左声道
/
/
media.setMute(MuteEnum.MUTE_LEFT);
}
public void right(View view) {
/
/
右声道
/
/
wlPlayer.setMute(MuteEnum.MUTE_RIGHT);
}
public void pitch(View view) {
mediaPlayer.setSpeed(
1f
);
mediaPlayer.setPicth(
1.5f
);
}
public void speed(View view) {
mediaPlayer.setSpeed(
1.5f
);
mediaPlayer.setPicth(
1f
);
}
public void speed_pitch(View view) {
mediaPlayer.setSpeed(
1.5f
);
mediaPlayer.setPicth(
1.5f
);
}
public void normal_vol(View view) {
mediaPlayer.setSpeed(
1f
);
mediaPlayer.setPicth(
1f
);
}
public void start_record(View view) {
/
/
开如录音
/
/
File
file
=
new
File
(Environment.getExternalStorageDirectory()
+
File
.separator
+
"test_player.aac"
);
/
/
wlPlayer.startRecord(
file
);
}
public void pause_record(View view) {
/
/
暂停录音
/
/
wlPlayer.pauseRecord();
}
public void goon_record(View view) {
/
/
继续录音
/
/
wlPlayer.resumeRcord();
}
public void stop_record(View view) {
/
/
停止录音
/
/
wlPlayer.stopRecord();
}
public void startLu(View view) {
boolean check
=
PermissionUtil.check(this, PermissionUtil.READ_EXTERNAL_STORAGE);
if
(!check) {
T.showShort(this,
"没有读写权限"
);
PermissionUtil.requestPermission(this, PermissionUtil.READ_EXTERNAL_STORAGE);
return
;
}
/
/
录制视频
mediaPlayer.setSaveVideoFile(Environment.getExternalStorageDirectory()
+
File
.separator
+
"_"
+
System.currentTimeMillis()
+
".mp4"
);
}
public void stopLu(View view) {
/
/
停止录制视频
mediaPlayer.stopSaveVideo();
}
private void updateDownTime() {
if
(fileDown!
=
null) {
int
duration
=
fileDown.getDuration();
int
currPos
=
fileDown.getCurrPos();
String time
=
getTime(duration
/
1000
);
String time2
=
getTime(currPos
/
1000
);
tvTime.setText(time2
+
" / "
+
time);
}
}
public void startDown(View view) {
boolean check
=
PermissionUtil.check(this, PermissionUtil.READ_EXTERNAL_STORAGE);
if
(!check) {
T.showShort(this,
"没有读写权限"
);
PermissionUtil.requestPermission(this, PermissionUtil.READ_EXTERNAL_STORAGE);
return
;
}
if
(fileDown
=
=
null) {
fileDown
=
new MediaFileDown();
fileDown.setDownListener(null, new OnCallCompleteListener() {
@Override
public void onCallComplete(boolean isFull) {
/
/
下载完成
if
(isFull) {
Log.e(TAG,
"完整下载"
);
}
else
{
Log.e(TAG,
"非完整下载"
);
}
T.showShort(getApplication(),
"下载完成"
);
}
}, new OnDownloadingListener() {
@Override
public void onDownloadingListener(
int
curr,
int
duration) {
if
(duration
=
=
0
) {
if
(curr!
=
0
&& curr
%
100
=
=
0
) {
runOnUiThread(new Thread() {
@Override
public void run() {
updateDownTime();
}
});
}
return
;
}
int
leng
=
curr
*
100
/
duration;
Log.e(TAG,
"下载进度-->"
+
leng
+
"% curr:"
+
curr
+
" dur:"
+
duration);
seekBar.setProgress(leng);
if
(leng!
=
0
&& leng
%
2
=
=
0
) {
runOnUiThread(new Thread() {
@Override
public void run() {
updateDownTime();
}
});
}
}
}, new MediaFileDown.OnErrorListener() {
@Override
public void onError(
int
code) {
/
/
TODO Auto
-
generated method stub
T.showShort(getApplication(),
"下载出错"
);
runOnUiThread(new Thread() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"下载出错"
, Toast.LENGTH_SHORT).show();
}
});
}
});
}
String url
=
"";
Log.e(TAG,
file
);
boolean loading
=
fileDown.isLoading();
String string
=
inputView.getText().toString();
if
(!TextUtils.isEmpty(string)) {
url
=
string;
}
Log.e(TAG,
"下载:"
+
url);
fileDown.setDownload(url.trim(),
file
);
fileDown.start();
}
public void stopDown(View view) {
/
/
停止录制视频
if
(fileDown!
=
null) {
Log.e(TAG,
"stop"
);
fileDown.stop();
}
}
public void pauseDown(View view) {
/
/
暂停下载
if
(fileDown!
=
null) {
Log.e(TAG,
"pause"
);
fileDown.pause();
}
}
}