Skip to content

Commit

Permalink
增加loop设置
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajunhui committed Aug 21, 2020
1 parent 8f9fb56 commit c22b4da
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ demo示例集成了播放控制组件**ControllerCover**、加载中组件**Load
dependencies {
//---------如果仅使用MediaPlayer解码,使用以下依赖。----------
//该依赖仅包含MediaPlayer解码
implementation 'com.kk.taurus.playerbase:playerbase:3.3.7'
implementation 'com.kk.taurus.playerbase:playerbase:3.3.8'
//---------如果使用ExoPlayer解码,使用以下依赖。---------
//该依赖包含exoplayer解码和MediaPlayer解码
//注意exoplayer的最小支持SDK版本为16
implementation 'cn.jiajunhui:exoplayer:337_2117_013'
implementation 'cn.jiajunhui:exoplayer:338_2117_014'
//---------如果使用ijkPlayer解码,使用以下依赖。---------
//该依赖包含ijkplayer解码和MediaPlayer解码
implementation 'cn.jiajunhui:ijkplayer:337_088_008'
implementation 'cn.jiajunhui:ijkplayer:338_088_009'
//ijk官方的解码库依赖,较少格式版本且不支持HTTPS。
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
# Other ABIs: optional
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/kk/taurus/avplayer/ui/MusicPlayActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

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

private SeekBar mSeekBar;

private SwitchCompat mSwitchCompat;

private byte[] waveType = new byte[]{
VisualizerView.WAVE_TYPE_BROKEN_LINE,
VisualizerView.WAVE_TYPE_RECTANGLE,
Expand All @@ -52,12 +56,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mEtUrl = findViewById(R.id.music_url_et);
mMusicWave = findViewById(R.id.visualizerView);
mSeekBar = findViewById(R.id.music_seek_bar);
mSwitchCompat = findViewById(R.id.music_play_switch_compat);

mSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

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

initMusicWave();
updateVisualizer();

mSwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mPlayer.setLooping(isChecked);
}
});
}

private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_input_url_play.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="http://"
android:text="http://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8"
android:text="rtmp://202.69.69.180:443/webcast/bshdlive-mobile"
android:paddingLeft="16dp"
style="@style/MyEditText"
android:layout_weight="1"/>
Expand Down
53 changes: 37 additions & 16 deletions app/src/main/res/layout/activity_test_play.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#000000"
android:background="#99000000"
android:padding="16dp">

<EditText
Expand All @@ -26,30 +27,50 @@
android:textColor="#FFFFFF"
android:text="PLAY"/>

<Button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
android:onClick="volumeIncrease"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:text="音量加"/>
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:onClick="volumeIncrease"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:text="音量加"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="@color/colorPrimary"
android:onClick="volumeReduce"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:text="音量减"/>
</LinearLayout>

<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
android:onClick="volumeReduce"
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/music_play_switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:text="音量减"/>
android:text="单曲循环"
app:switchPadding="10dp"
app:showText="true"
android:textOn=""
android:textOff=""
android:checked="true"
android:layout_marginTop="10dp" />

<SeekBar
android:id="@+id/music_seek_bar"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_height="4dp"
android:layout_marginTop="10dp"
android:focusable="false"
android:maxHeight="4dp"
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ project.ext {
exoplayersdkVersion = '2.11.7',
appcompatVersion = '1.1.0',

playerbaseIjkVersion = '337_088_008',
playerbaseExoVersion = '337_2117_013',
playerbaseVersion = '3.3.7'
playerbaseIjkVersion = '338_088_009',
playerbaseExoVersion = '338_2117_014',
playerbaseVersion = '3.3.8'
]

}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public void setSpeed(float speed) {
mInternalPlayer.setPlaybackParameters(parameters);
}

@Override
public void setLooping(boolean looping) {
mInternalPlayer.setRepeatMode(looping?Player.REPEAT_MODE_ALL:Player.REPEAT_MODE_OFF);
}

@Override
public boolean isPlaying() {
if (mInternalPlayer == null)
Expand Down Expand Up @@ -421,7 +426,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_AUDIO_RENDER_START, null);
}

if(mStartPos > 0){
if(mStartPos > 0 && mInternalPlayer.getDuration() > 0){
mInternalPlayer.seekTo(mStartPos);
mStartPos = -1;
}
Expand Down
14 changes: 13 additions & 1 deletion ijkplayer/src/main/java/com/kk/taurus/ijkplayer/IjkPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ private void openVideo(DataSource dataSource) {
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();

//set looping indicator for IjkMediaPlayer
mMediaPlayer.setLooping(isLooping());

Bundle bundle = BundlePool.obtain();
bundle.putSerializable(EventKey.SERIALIZABLE_DATA,dataSource);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET,bundle);
Expand Down Expand Up @@ -377,6 +380,12 @@ public void setSpeed(float speed) {
}
}

@Override
public void setLooping(boolean looping) {
super.setLooping(looping);
mMediaPlayer.setLooping(looping);
}

@Override
public int getAudioSessionId() {
if(available()){
Expand Down Expand Up @@ -411,7 +420,7 @@ public void onPrepared(IMediaPlayer mp) {
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED,bundle);

int seekToPosition = startSeekPos; // mSeekWhenPrepared may be changed after seekTo() call
if (seekToPosition != 0) {
if (seekToPosition > 0 && mp.getDuration() > 0) {
mMediaPlayer.seekTo(seekToPosition);
startSeekPos = 0;
}
Expand Down Expand Up @@ -452,6 +461,9 @@ public void onCompletion(IMediaPlayer mp) {
updateStatus(STATE_PLAYBACK_COMPLETE);
mTargetState = STATE_PLAYBACK_COMPLETE;
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE,null);
if(!isLooping()){
stop();
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ public void setSpeed(float speed) {
mInternalPlayer.setSpeed(speed);
}

@Override
public void setLooping(boolean looping) {
if(isPlayerAvailable())
mInternalPlayer.setLooping(looping);
}

@Override
public boolean isPlaying() {
if(isPlayerAvailable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface AssistPlay {

void setVolume(float left, float right);
void setSpeed(float speed);
void setLooping(boolean looping);

void setReceiverGroup(IReceiverGroup receiverGroup);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ public void setSpeed(float speed) {
mPlayer.setSpeed(speed);
}

@Override
public void setLooping(boolean looping) {
mPlayer.setLooping(looping);
}

/**
* Associate the playback view to the specified container
* @param userContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ public abstract class BaseInternalPlayer implements IPlayer {

private int mBufferPercentage;

private boolean mLooping;

public boolean isLooping() {
return mLooping;
}

@Override
public void option(int code, Bundle bundle) {
//not handle
}

@Override
public void setLooping(boolean looping) {
this.mLooping = looping;
}

@Override
public final void setOnBufferingListener(OnBufferingListener onBufferingListener) {
this.mOnBufferingListener = onBufferingListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public interface IPlayer {
void setSurface(Surface surface);
void setVolume(float left, float right);
void setSpeed(float speed);
void setLooping(boolean looping);

void setOnPlayerEventListener(OnPlayerEventListener onPlayerEventListener);
void setOnErrorEventListener(OnErrorEventListener onErrorEventListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public void setDataSource(DataSource dataSource) {
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();

//set looping indicator for MediaPlayer
mMediaPlayer.setLooping(isLooping());

Bundle bundle = BundlePool.obtain();
bundle.putSerializable(EventKey.SERIALIZABLE_DATA,dataSource);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET,bundle);
Expand Down Expand Up @@ -195,6 +198,12 @@ public void setSpeed(float speed) {
}
}

@Override
public void setLooping(boolean looping) {
super.setLooping(looping);
mMediaPlayer.setLooping(looping);
}

@Override
public boolean isPlaying() {
if(available() && getState()!= STATE_ERROR){
Expand Down Expand Up @@ -395,7 +404,7 @@ public void onPrepared(MediaPlayer mp) {
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED,bundle);

int seekToPosition = startSeekPos; // mSeekWhenPrepared may be changed after seekTo() call
if (seekToPosition != 0) {
if (seekToPosition > 0 && mp.getDuration() > 0) {
//seek to start position
mMediaPlayer.seekTo(seekToPosition);
startSeekPos = 0;
Expand Down Expand Up @@ -462,6 +471,9 @@ public void onCompletion(MediaPlayer mp) {
updateStatus(STATE_PLAYBACK_COMPLETE);
mTargetState = STATE_PLAYBACK_COMPLETE;
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE,null);
if(!isLooping()){
stop();
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ public void setSpeed(float speed){
mPlayer.setSpeed(speed);
}

@Override
public void setLooping(boolean looping) {
mPlayer.setLooping(looping);
}

/**
* if you want to clear frame and recreate render, call this method.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface IVideoView {

void setVolume(float left, float right);
void setSpeed(float speed);
void setLooping(boolean looping);

IRender getRender();

Expand Down

0 comments on commit c22b4da

Please sign in to comment.