Commit 8f44ca58 by 范玉宾

Meditation play to be continue

parent 653de592
......@@ -58,10 +58,14 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.exoplayer:exoplayer-core:2.9.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.9.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.0'
// solve build problem
// cannot access 'androidx.lifecycle.hasdefaultviewmodelproviderfactory'
// Comment out when compiling
// implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "com.alibaba:arouter-api:$arouter_api"
// 注意此处的依赖方式:kotlin中使用和java中使用方式有不同
......
package com.yidianling.muse.activity
import android.os.Build
import android.media.MediaPlayer
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.constraintlayout.widget.ConstraintLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.google.android.exoplayer2.ExoPlayerFactory
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.setTransparentForImageView
import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.statusBarLightMode
import com.ydl.ydlcommon.utils.remind.ToastHelper
import com.yidianling.muse.R
import com.yidianling.muse.helper.MediaPlayerHelper
import com.yidianling.muse.helper.MediaPlayerManager
import com.yidianling.muse.widget.QuitMeditationDialog
import com.yidianling.muse.widget.ShareMeditationDialog
import kotlinx.android.synthetic.main.activity_play_meditation.*
import kotlin.properties.Delegates
import kotlinx.android.synthetic.main.exo_player_control_view.*
@Route(path = "/muse/play")
......@@ -27,7 +24,11 @@ class PlayMeditationActivity : BaseActivity() {
private var bgUrl =
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcuimianxinli.com%2Fupload%2F2016-07%2F16072613412794.jpg&refer=http%3A%2F%2Fcuimianxinli.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654150276&t=393628420414c371b4b00540943cf0c7"
private val path =
"http://file.kuyinyun.com/group1/M00/90/B7/rBBGdFPXJNeAM-nhABeMElAM6bY151.mp3"
private var collected = false
private var isPlaying = false
private var shareDialog: ShareMeditationDialog? = null
private var quitDialog: QuitMeditationDialog? = null
......@@ -59,17 +60,17 @@ class PlayMeditationActivity : BaseActivity() {
.into(iv_bg)
iv_close.setOnClickListener {
if (quitDialog == null){
if (quitDialog == null) {
quitDialog = QuitMeditationDialog.newInstance()
}
if (quitDialog!=null && quitDialog?.isAdded == false){
quitDialog?.setListener(object :QuitMeditationDialog.ClickListener{
if (quitDialog != null && quitDialog?.isAdded == false) {
quitDialog?.setListener(object : QuitMeditationDialog.ClickListener {
override fun quit() {
ToastHelper.show("结束练习")
finish()
}
})
quitDialog?.show(supportFragmentManager,QuitMeditationDialog.TAG)
quitDialog?.show(supportFragmentManager, QuitMeditationDialog.TAG)
}
}
......@@ -108,53 +109,65 @@ class PlayMeditationActivity : BaseActivity() {
ToastHelper.show("设置定时关闭!")
}
iv_play_or_pause.setOnClickListener {
exo_play.setOnClickListener {
if (isPlaying) {
MediaPlayerManager.getInstance(this).pause()
} else {
if (MediaPlayerManager.getInstance(this).path.isNullOrBlank()) {
MediaPlayerManager.getInstance(this).path = path
} else {
MediaPlayerManager.getInstance(this).play()
}
}
isPlaying = !isPlaying
if (isPlaying) {
exo_play.setImageResource(R.drawable.icon_pause_play)
} else {
exo_play.setImageResource(R.drawable.icon_pause_meditation)
}
}
iv_previous.setOnClickListener {
exo_prev.setOnClickListener {
ToastHelper.show("上一首!")
}
iv_next.setOnClickListener {
exo_next.setOnClickListener {
ToastHelper.show("下一首!")
}
iv_rewind.setOnClickListener {
exo_rew.setOnClickListener {
val currentProgress = seekbar_play_progress.progress
}
val progress = 0.coerceAtLeast(currentProgress - 15)
exo_ffwd.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
seekbar_play_progress.setProgress(progress, true)
} else {
seekbar_play_progress.progress = progress
}
}
iv_fast_forward.setOnClickListener {
val currentProgress = seekbar_play_progress.progress
val progress = 100.coerceAtMost(currentProgress + 15)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
seekbar_play_progress.setProgress(progress, true)
} else {
seekbar_play_progress.progress = progress
}
}
MediaPlayerManager.getInstance(this)
.setOnPreparedListener(object : MediaPlayerManager.OnMediaPlayerManagerListener {
override fun onProgress(mediaPlayer: MediaPlayer?) {
}
})
// MediaPlayerManager.getInstance(this).setOnPreparedListener {
// it.start()
// }
// MediaPlayerHelper.getInstance(this).setPath(
// "https://storage.googleapis.com/exoplayer-test-media-0/play.mp3"
// )
}
initPlayer()
}
private fun initPlayer(){
private fun initPlayer() {
val exoPlayer = ExoPlayerFactory.newSimpleInstance(this)
exoPlayer.playWhenReady = false
}
}
\ No newline at end of file
......@@ -95,42 +95,26 @@ class PlayPureMusicActivity : BaseActivity() {
ToastHelper.show("设置定时关闭!")
}
iv_play_or_pause.setOnClickListener {
}
iv_previous.setOnClickListener {
ToastHelper.show("上一首!")
}
iv_next.setOnClickListener {
ToastHelper.show("下一首!")
}
iv_rewind.setOnClickListener {
val currentProgress = seekbar_play_progress.progress
val progress = 0.coerceAtLeast(currentProgress - 15)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
seekbar_play_progress.setProgress(progress, true)
} else {
seekbar_play_progress.progress = progress
}
}
iv_fast_forward.setOnClickListener {
val currentProgress = seekbar_play_progress.progress
val progress = 100.coerceAtMost(currentProgress + 15)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
seekbar_play_progress.setProgress(progress, true)
} else {
seekbar_play_progress.progress = progress
}
}
// iv_play_or_pause.setOnClickListener {
//
// }
// iv_previous.setOnClickListener {
// ToastHelper.show("上一首!")
// }
// iv_next.setOnClickListener {
// ToastHelper.show("下一首!")
// }
// iv_rewind.setOnClickListener {
//
//
// }
//
// iv_fast_forward.setOnClickListener {
//
// }
}
......
package com.yidianling.muse.helper;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import java.io.IOException;
public class MediaPlayerManager {
private Context mContext;
private MediaPlayer mMediaPlayer;
private String mPath;
private OnMediaPlayerManagerListener mListener;
private volatile static MediaPlayerManager instance;
private MediaPlayerManager(Context context){
mContext = context;
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
public static MediaPlayerManager getInstance(Context context){
if (instance == null){
synchronized (MediaPlayerManager.class){
if (instance == null){
instance = new MediaPlayerManager(context);
}
}
}
return instance;
}
public void setPath(String path) throws IOException {
mPath = path;
if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
public String getPath(){
return mPath;
}
public void play(){
if (mMediaPlayer.isPlaying()) return;
mMediaPlayer.start();
}
public void pause(){
if (mMediaPlayer.isPlaying()) mMediaPlayer.pause();
}
public void seekTo(int position){
mMediaPlayer.seekTo(position);
}
public void setOnPreparedListener(OnMediaPlayerManagerListener listener){
mListener = listener;
}
public interface OnMediaPlayerManagerListener{
default void onPrepared(MediaPlayer mediaPlayer){};
default void onProgress(MediaPlayer mediaPlayer){};
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="28dp"
android:viewportWidth="20"
android:viewportHeight="28">
<path
android:pathData="M3,0L3,0A3,3 0,0 1,6 3L6,25A3,3 0,0 1,3 28L3,28A3,3 0,0 1,0 25L0,3A3,3 0,0 1,3 0z"
android:strokeWidth="1"
android:fillColor="#FFFFFF"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M17,0L17,0A3,3 0,0 1,20 3L20,25A3,3 0,0 1,17 28L17,28A3,3 0,0 1,14 25L14,3A3,3 0,0 1,17 0z"
android:strokeWidth="1"
android:fillColor="#FFFFFF"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
</vector>
......@@ -100,91 +100,17 @@
app:layout_constraintTop_toBottomOf="@id/tv_title"
tools:text="舒缓安神舒缓安神舒缓安神舒缓安神舒缓安神舒缓安神舒缓安神舒缓安神" />
<ImageView
android:id="@+id/iv_play_or_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:src="@drawable/icon_pause_meditation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/iv_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:src="@drawable/icon_play_meditation_previous"
app:layout_constraintBottom_toBottomOf="@id/iv_play_or_pause"
app:layout_constraintEnd_toStartOf="@id/iv_play_or_pause"
app:layout_constraintTop_toTopOf="@id/iv_play_or_pause" />
<ImageView
android:id="@+id/iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:src="@drawable/icon_play_meditation_next"
app:layout_constraintBottom_toBottomOf="@id/iv_play_or_pause"
app:layout_constraintStart_toEndOf="@id/iv_play_or_pause"
app:layout_constraintTop_toTopOf="@id/iv_play_or_pause" />
<ImageView
android:id="@+id/iv_rewind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:src="@drawable/icon_play_meditation_rewind"
app:layout_constraintBottom_toBottomOf="@id/iv_play_or_pause"
app:layout_constraintEnd_toStartOf="@id/iv_previous"
app:layout_constraintTop_toTopOf="@id/iv_play_or_pause" />
<ImageView
android:id="@+id/iv_fast_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:src="@drawable/icon_play_meditation_fast_forward"
app:layout_constraintBottom_toBottomOf="@id/iv_play_or_pause"
app:layout_constraintStart_toEndOf="@id/iv_next"
app:layout_constraintTop_toTopOf="@id/iv_play_or_pause" />
<androidx.appcompat.widget.AppCompatSeekBar
android:id="@+id/seekbar_play_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="64dp"
android:layout_marginBottom="50dp"
android:progress="50"
android:thumb="@drawable/play_meditation_thumb"
app:layout_constraintBottom_toTopOf="@id/iv_play_or_pause"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tv_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:textColor="#B3FFFFFF"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/seekbar_play_progress"
app:layout_constraintEnd_toStartOf="@id/seekbar_play_progress"
app:layout_constraintTop_toTopOf="@id/seekbar_play_progress"
tools:text="03:46" />
<TextView
android:id="@+id/tv_total_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:textColor="#B3FFFFFF"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/seekbar_play_progress"
app:layout_constraintStart_toEndOf="@id/seekbar_play_progress"
app:layout_constraintTop_toTopOf="@id/seekbar_play_progress"
tools:text="13:46" />
<!-- <com.google.android.exoplayer2.ui.PlayerView-->
<!-- android:id="@+id/player_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="180dp"-->
<!-- android:layout_marginBottom="140dp"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:controller_layout_id="@layout/exo_player_control_view" />-->
<include layout="@layout/exo_player_control_view" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guide_line"
......@@ -200,7 +126,7 @@
android:layout_marginEnd="50dp"
android:layout_marginBottom="48dp"
android:src="@drawable/icon_play_meditation_collect"
app:layout_constraintBottom_toTopOf="@id/seekbar_play_progress"
app:layout_constraintBottom_toTopOf="@id/exo_progress"
app:layout_constraintEnd_toStartOf="@id/guide_line" />
<ImageView
......@@ -210,7 +136,7 @@
android:layout_marginStart="50dp"
android:layout_marginBottom="48dp"
android:src="@drawable/icon_play_meditation_time_off"
app:layout_constraintBottom_toTopOf="@id/seekbar_play_progress"
app:layout_constraintBottom_toTopOf="@id/exo_progress"
app:layout_constraintStart_toEndOf="@id/guide_line" />
<TextView
......
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/activity_play_meditation">
<ImageView
android:id="@+id/exo_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:src="@drawable/icon_pause_meditation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/exo_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:src="@drawable/icon_play_meditation_previous"
app:layout_constraintBottom_toBottomOf="@id/exo_play"
app:layout_constraintEnd_toStartOf="@id/exo_play"
app:layout_constraintTop_toTopOf="@id/exo_play" />
<ImageView
android:id="@+id/exo_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:src="@drawable/icon_play_meditation_next"
app:layout_constraintBottom_toBottomOf="@id/exo_play"
app:layout_constraintStart_toEndOf="@id/exo_play"
app:layout_constraintTop_toTopOf="@id/exo_play" />
<ImageView
android:id="@+id/exo_rew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:src="@drawable/icon_play_meditation_rewind"
app:layout_constraintBottom_toBottomOf="@id/exo_play"
app:layout_constraintEnd_toStartOf="@id/exo_prev"
app:layout_constraintTop_toTopOf="@id/exo_play" />
<ImageView
android:id="@+id/exo_ffwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:src="@drawable/icon_play_meditation_fast_forward"
app:layout_constraintBottom_toBottomOf="@id/exo_play"
app:layout_constraintStart_toEndOf="@id/exo_next"
app:layout_constraintTop_toTopOf="@id/exo_play" />
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@+id/exo_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="64dp"
android:layout_marginBottom="50dp"
app:played_color="#000000"
app:unplayed_color="#FFFFFFFF"
app:layout_constraintBottom_toTopOf="@id/exo_play"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:textColor="#B3FFFFFF"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/exo_progress"
app:layout_constraintEnd_toStartOf="@id/exo_progress"
app:layout_constraintTop_toTopOf="@id/exo_progress"
tools:text="03:46" />
<TextView
android:id="@+id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:textColor="#B3FFFFFF"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/exo_progress"
app:layout_constraintStart_toEndOf="@id/exo_progress"
app:layout_constraintTop_toTopOf="@id/exo_progress"
tools:text="13:46" />
</merge>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment