Skip to content

Commit c22b4da

Browse files
author
jiajunhui
committed
增加loop设置
1 parent 8f9fb56 commit c22b4da

File tree

15 files changed

+119
-26
lines changed

15 files changed

+119
-26
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ demo示例集成了播放控制组件**ControllerCover**、加载中组件**Load
8282
dependencies {
8383
//---------如果仅使用MediaPlayer解码,使用以下依赖。----------
8484
//该依赖仅包含MediaPlayer解码
85-
implementation 'com.kk.taurus.playerbase:playerbase:3.3.7'
85+
implementation 'com.kk.taurus.playerbase:playerbase:3.3.8'
8686
8787
8888
//---------如果使用ExoPlayer解码,使用以下依赖。---------
8989
//该依赖包含exoplayer解码和MediaPlayer解码
9090
//注意exoplayer的最小支持SDK版本为16
91-
implementation 'cn.jiajunhui:exoplayer:337_2117_013'
91+
implementation 'cn.jiajunhui:exoplayer:338_2117_014'
9292
9393
9494
//---------如果使用ijkPlayer解码,使用以下依赖。---------
9595
//该依赖包含ijkplayer解码和MediaPlayer解码
96-
implementation 'cn.jiajunhui:ijkplayer:337_088_008'
96+
implementation 'cn.jiajunhui:ijkplayer:338_088_009'
9797
//ijk官方的解码库依赖,较少格式版本且不支持HTTPS。
9898
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
9999
# Other ABIs: optional

app/src/main/java/com/kk/taurus/avplayer/ui/MusicPlayActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import android.os.Bundle;
77
import android.text.TextUtils;
88
import android.view.View;
9+
import android.widget.CompoundButton;
910
import android.widget.EditText;
1011
import android.widget.SeekBar;
1112
import android.widget.Toast;
1213

1314
import androidx.annotation.Nullable;
1415
import androidx.appcompat.app.AppCompatActivity;
16+
import androidx.appcompat.widget.SwitchCompat;
1517

1618
import com.kk.taurus.avplayer.R;
1719
import com.kk.taurus.avplayer.view.VisualizerView;
@@ -35,6 +37,8 @@ public class MusicPlayActivity extends AppCompatActivity implements OnPlayerEven
3537

3638
private SeekBar mSeekBar;
3739

40+
private SwitchCompat mSwitchCompat;
41+
3842
private byte[] waveType = new byte[]{
3943
VisualizerView.WAVE_TYPE_BROKEN_LINE,
4044
VisualizerView.WAVE_TYPE_RECTANGLE,
@@ -52,12 +56,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5256
mEtUrl = findViewById(R.id.music_url_et);
5357
mMusicWave = findViewById(R.id.visualizerView);
5458
mSeekBar = findViewById(R.id.music_seek_bar);
59+
mSwitchCompat = findViewById(R.id.music_play_switch_compat);
5560

5661
mSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
5762

5863
setVolumeControlStream(AudioManager.STREAM_MUSIC);
5964

6065
mPlayer = new AVPlayer();
66+
mPlayer.setLooping(mSwitchCompat.isChecked());
6167
mPlayer.setVolume(mVolumeLeft, mVolumeRight);
6268
mPlayer.setOnPlayerEventListener(this);
6369
mPlayer.setOnErrorEventListener(new OnErrorEventListener() {
@@ -69,6 +75,13 @@ public void onErrorEvent(int eventCode, Bundle bundle) {
6975

7076
initMusicWave();
7177
updateVisualizer();
78+
79+
mSwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
80+
@Override
81+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
82+
mPlayer.setLooping(isChecked);
83+
}
84+
});
7285
}
7386

7487
private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {

app/src/main/res/layout/activity_input_url_play.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
android:layout_width="0dp"
2121
android:layout_height="match_parent"
2222
android:hint="http://"
23-
android:text="http://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8"
23+
android:text="rtmp://202.69.69.180:443/webcast/bshdlive-mobile"
2424
android:paddingLeft="16dp"
2525
style="@style/MyEditText"
2626
android:layout_weight="1"/>

app/src/main/res/layout/activity_test_play.xml

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
56
android:orientation="vertical"
6-
android:background="#000000"
7+
android:background="#99000000"
78
android:padding="16dp">
89

910
<EditText
@@ -26,30 +27,50 @@
2627
android:textColor="#FFFFFF"
2728
android:text="PLAY"/>
2829

29-
<Button
30+
<LinearLayout
3031
android:layout_width="match_parent"
31-
android:layout_height="40dp"
32+
android:layout_height="wrap_content"
3233
android:layout_marginTop="10dp"
33-
android:background="@color/colorPrimary"
34-
android:onClick="volumeIncrease"
35-
android:textSize="16sp"
36-
android:textColor="#FFFFFF"
37-
android:text="音量加"/>
34+
android:orientation="horizontal">
35+
<Button
36+
android:layout_width="0dp"
37+
android:layout_weight="1"
38+
android:layout_height="40dp"
39+
android:background="@color/colorPrimary"
40+
android:onClick="volumeIncrease"
41+
android:textSize="16sp"
42+
android:textColor="#FFFFFF"
43+
android:text="音量加"/>
44+
<Button
45+
android:layout_width="0dp"
46+
android:layout_weight="1"
47+
android:layout_height="40dp"
48+
android:layout_marginLeft="10dp"
49+
android:background="@color/colorPrimary"
50+
android:onClick="volumeReduce"
51+
android:textSize="16sp"
52+
android:textColor="#FFFFFF"
53+
android:text="音量减"/>
54+
</LinearLayout>
3855

39-
<Button
40-
android:layout_width="match_parent"
41-
android:layout_height="40dp"
42-
android:layout_marginTop="10dp"
43-
android:background="@color/colorPrimary"
44-
android:onClick="volumeReduce"
56+
<androidx.appcompat.widget.SwitchCompat
57+
android:id="@+id/music_play_switch_compat"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
4560
android:textSize="16sp"
4661
android:textColor="#FFFFFF"
47-
android:text="音量减"/>
62+
android:text="单曲循环"
63+
app:switchPadding="10dp"
64+
app:showText="true"
65+
android:textOn=""
66+
android:textOff=""
67+
android:checked="true"
68+
android:layout_marginTop="10dp" />
4869

4970
<SeekBar
5071
android:id="@+id/music_seek_bar"
5172
android:layout_width="match_parent"
52-
android:layout_height="2dp"
73+
android:layout_height="4dp"
5374
android:layout_marginTop="10dp"
5475
android:focusable="false"
5576
android:maxHeight="4dp"

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ project.ext {
4040
exoplayersdkVersion = '2.11.7',
4141
appcompatVersion = '1.1.0',
4242

43-
playerbaseIjkVersion = '337_088_008',
44-
playerbaseExoVersion = '337_2117_013',
45-
playerbaseVersion = '3.3.7'
43+
playerbaseIjkVersion = '338_088_009',
44+
playerbaseExoVersion = '338_2117_014',
45+
playerbaseVersion = '3.3.8'
4646
]
4747

4848
}

exoplayer/src/main/java/com/kk/taurus/exoplayer/ExoMediaPlayer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ public void setSpeed(float speed) {
237237
mInternalPlayer.setPlaybackParameters(parameters);
238238
}
239239

240+
@Override
241+
public void setLooping(boolean looping) {
242+
mInternalPlayer.setRepeatMode(looping?Player.REPEAT_MODE_ALL:Player.REPEAT_MODE_OFF);
243+
}
244+
240245
@Override
241246
public boolean isPlaying() {
242247
if (mInternalPlayer == null)
@@ -421,7 +426,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
421426
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_AUDIO_RENDER_START, null);
422427
}
423428

424-
if(mStartPos > 0){
429+
if(mStartPos > 0 && mInternalPlayer.getDuration() > 0){
425430
mInternalPlayer.seekTo(mStartPos);
426431
mStartPos = -1;
427432
}

ijkplayer/src/main/java/com/kk/taurus/ijkplayer/IjkPlayer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ private void openVideo(DataSource dataSource) {
171171
mMediaPlayer.setScreenOnWhilePlaying(true);
172172
mMediaPlayer.prepareAsync();
173173

174+
//set looping indicator for IjkMediaPlayer
175+
mMediaPlayer.setLooping(isLooping());
176+
174177
Bundle bundle = BundlePool.obtain();
175178
bundle.putSerializable(EventKey.SERIALIZABLE_DATA,dataSource);
176179
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET,bundle);
@@ -377,6 +380,12 @@ public void setSpeed(float speed) {
377380
}
378381
}
379382

383+
@Override
384+
public void setLooping(boolean looping) {
385+
super.setLooping(looping);
386+
mMediaPlayer.setLooping(looping);
387+
}
388+
380389
@Override
381390
public int getAudioSessionId() {
382391
if(available()){
@@ -411,7 +420,7 @@ public void onPrepared(IMediaPlayer mp) {
411420
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED,bundle);
412421

413422
int seekToPosition = startSeekPos; // mSeekWhenPrepared may be changed after seekTo() call
414-
if (seekToPosition != 0) {
423+
if (seekToPosition > 0 && mp.getDuration() > 0) {
415424
mMediaPlayer.seekTo(seekToPosition);
416425
startSeekPos = 0;
417426
}
@@ -452,6 +461,9 @@ public void onCompletion(IMediaPlayer mp) {
452461
updateStatus(STATE_PLAYBACK_COMPLETE);
453462
mTargetState = STATE_PLAYBACK_COMPLETE;
454463
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE,null);
464+
if(!isLooping()){
465+
stop();
466+
}
455467
}
456468
};
457469

playerbase/src/main/java/com/kk/taurus/playerbase/AVPlayer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ public void setSpeed(float speed) {
481481
mInternalPlayer.setSpeed(speed);
482482
}
483483

484+
@Override
485+
public void setLooping(boolean looping) {
486+
if(isPlayerAvailable())
487+
mInternalPlayer.setLooping(looping);
488+
}
489+
484490
@Override
485491
public boolean isPlaying() {
486492
if(isPlayerAvailable())

playerbase/src/main/java/com/kk/taurus/playerbase/assist/AssistPlay.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public interface AssistPlay {
4848

4949
void setVolume(float left, float right);
5050
void setSpeed(float speed);
51+
void setLooping(boolean looping);
5152

5253
void setReceiverGroup(IReceiverGroup receiverGroup);
5354

playerbase/src/main/java/com/kk/taurus/playerbase/assist/RelationAssist.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ public void setSpeed(float speed) {
337337
mPlayer.setSpeed(speed);
338338
}
339339

340+
@Override
341+
public void setLooping(boolean looping) {
342+
mPlayer.setLooping(looping);
343+
}
344+
340345
/**
341346
* Associate the playback view to the specified container
342347
* @param userContainer

playerbase/src/main/java/com/kk/taurus/playerbase/player/BaseInternalPlayer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ public abstract class BaseInternalPlayer implements IPlayer {
4141

4242
private int mBufferPercentage;
4343

44+
private boolean mLooping;
45+
46+
public boolean isLooping() {
47+
return mLooping;
48+
}
49+
4450
@Override
4551
public void option(int code, Bundle bundle) {
4652
//not handle
4753
}
4854

55+
@Override
56+
public void setLooping(boolean looping) {
57+
this.mLooping = looping;
58+
}
59+
4960
@Override
5061
public final void setOnBufferingListener(OnBufferingListener onBufferingListener) {
5162
this.mOnBufferingListener = onBufferingListener;

playerbase/src/main/java/com/kk/taurus/playerbase/player/IPlayer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public interface IPlayer {
5353
void setSurface(Surface surface);
5454
void setVolume(float left, float right);
5555
void setSpeed(float speed);
56+
void setLooping(boolean looping);
5657

5758
void setOnPlayerEventListener(OnPlayerEventListener onPlayerEventListener);
5859
void setOnErrorEventListener(OnErrorEventListener onErrorEventListener);

playerbase/src/main/java/com/kk/taurus/playerbase/player/SysMediaPlayer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public void setDataSource(DataSource dataSource) {
120120
mMediaPlayer.setScreenOnWhilePlaying(true);
121121
mMediaPlayer.prepareAsync();
122122

123+
//set looping indicator for MediaPlayer
124+
mMediaPlayer.setLooping(isLooping());
125+
123126
Bundle bundle = BundlePool.obtain();
124127
bundle.putSerializable(EventKey.SERIALIZABLE_DATA,dataSource);
125128
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET,bundle);
@@ -195,6 +198,12 @@ public void setSpeed(float speed) {
195198
}
196199
}
197200

201+
@Override
202+
public void setLooping(boolean looping) {
203+
super.setLooping(looping);
204+
mMediaPlayer.setLooping(looping);
205+
}
206+
198207
@Override
199208
public boolean isPlaying() {
200209
if(available() && getState()!= STATE_ERROR){
@@ -395,7 +404,7 @@ public void onPrepared(MediaPlayer mp) {
395404
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED,bundle);
396405

397406
int seekToPosition = startSeekPos; // mSeekWhenPrepared may be changed after seekTo() call
398-
if (seekToPosition != 0) {
407+
if (seekToPosition > 0 && mp.getDuration() > 0) {
399408
//seek to start position
400409
mMediaPlayer.seekTo(seekToPosition);
401410
startSeekPos = 0;
@@ -462,6 +471,9 @@ public void onCompletion(MediaPlayer mp) {
462471
updateStatus(STATE_PLAYBACK_COMPLETE);
463472
mTargetState = STATE_PLAYBACK_COMPLETE;
464473
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE,null);
474+
if(!isLooping()){
475+
stop();
476+
}
465477
}
466478
};
467479

playerbase/src/main/java/com/kk/taurus/playerbase/widget/BaseVideoView.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ public void setSpeed(float speed){
315315
mPlayer.setSpeed(speed);
316316
}
317317

318+
@Override
319+
public void setLooping(boolean looping) {
320+
mPlayer.setLooping(looping);
321+
}
322+
318323
/**
319324
* if you want to clear frame and recreate render, call this method.
320325
*/

playerbase/src/main/java/com/kk/taurus/playerbase/widget/IVideoView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface IVideoView {
3434

3535
void setVolume(float left, float right);
3636
void setSpeed(float speed);
37+
void setLooping(boolean looping);
3738

3839
IRender getRender();
3940

0 commit comments

Comments
 (0)