Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022 – 144
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ganepola G.A.S.L IT19050386
2022 – 144
Commits
7d996612
Commit
7d996612
authored
Nov 18, 2022
by
Ravishani W.M.S
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
e0532784
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
253 additions
and
0 deletions
+253
-0
MusicTherapyS.java
MusicTherapyS.java
+253
-0
No files found.
MusicTherapyS.java
0 → 100644
View file @
7d996612
package
com.example.smartpillow
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.animation.AnimatorSet
;
import
android.animation.ObjectAnimator
;
import
android.content.Intent
;
import
android.graphics.PorterDuff
;
import
android.media.MediaPlayer
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.ImageView
;
import
android.widget.SeekBar
;
import
android.widget.TextView
;
import
java.io.File
;
import
java.time.OffsetDateTime
;
import
java.util.ArrayList
;
public
class
MusicTherapyS
extends
AppCompatActivity
{
Button
btnplay
,
btnnext
,
btnpre
,
btnff
,
btnfr
;
TextView
txtsname
,
txtsstart
,
txtsstop
;
SeekBar
seekmusic
;
ImageView
imagevView
;
String
sname
;
public
static
final
String
EXTRA_NAME
=
"song_name"
;
static
MediaPlayer
mediaPlayer
;
int
position
;
ArrayList
<
File
>
mySongs
;
Thread
updateseekbar
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_music_therapy_s
);
btnplay
=
findViewById
(
R
.
id
.
btnPlay
);
btnnext
=
findViewById
(
R
.
id
.
btnNext
);
btnpre
=
findViewById
(
R
.
id
.
btnPre
);
btnff
=
findViewById
(
R
.
id
.
btnFf
);
btnfr
=
findViewById
(
R
.
id
.
btnFr
);
txtsstart
=
findViewById
(
R
.
id
.
txtStart
);
txtsstop
=
findViewById
(
R
.
id
.
txtStop
);
txtsname
=
findViewById
(
R
.
id
.
txtSn
);
seekmusic
=
findViewById
(
R
.
id
.
seekbar
);
imagevView
=
findViewById
(
R
.
id
.
imageview
);
if
(
mediaPlayer
!=
null
)
{
mediaPlayer
.
stop
();
mediaPlayer
.
release
();
}
Intent
i
=
getIntent
();
Bundle
bundle
=
i
.
getExtras
();
mySongs
=
(
ArrayList
)
bundle
.
getParcelableArrayList
(
"songs"
);
String
songName
=
i
.
getStringExtra
(
"songname"
);
position
=
bundle
.
getInt
(
"pos"
,
0
);
txtsname
.
setSelected
(
true
);
Uri
uri
=
Uri
.
parse
(
mySongs
.
get
(
position
).
toString
());
sname
=
mySongs
.
get
(
position
).
getName
();
txtsname
.
setText
(
sname
);
mediaPlayer
=
MediaPlayer
.
create
(
getApplicationContext
(),
uri
);
mediaPlayer
.
start
();
updateseekbar
=
new
Thread
()
{
@Override
public
void
run
()
{
int
totalDuration
=
mediaPlayer
.
getDuration
();
int
currentposition
=
0
;
while
(
currentposition
<
totalDuration
)
{
try
{
sleep
(
500
);
currentposition
=
mediaPlayer
.
getCurrentPosition
();
seekmusic
.
setProgress
(
currentposition
);
}
catch
(
InterruptedException
|
IllegalStateException
e
)
{
e
.
printStackTrace
();
}
}
}
};
seekmusic
.
setMax
(
mediaPlayer
.
getDuration
());
updateseekbar
.
start
();
seekmusic
.
getProgressDrawable
().
setColorFilter
(
getResources
().
getColor
(
R
.
color
.
purple_700
),
PorterDuff
.
Mode
.
MULTIPLY
);
seekmusic
.
getThumb
().
setColorFilter
(
getResources
().
getColor
(
R
.
color
.
purple_700
),
PorterDuff
.
Mode
.
SRC_IN
);
seekmusic
.
setOnSeekBarChangeListener
(
new
SeekBar
.
OnSeekBarChangeListener
()
{
@Override
public
void
onProgressChanged
(
SeekBar
seekBar
,
int
i
,
boolean
b
)
{
}
@Override
public
void
onStartTrackingTouch
(
SeekBar
seekBar
)
{
}
@Override
public
void
onStopTrackingTouch
(
SeekBar
seekBar
)
{
mediaPlayer
.
seekTo
(
seekBar
.
getProgress
());
}
});
String
endTime
=
createTime
(
mediaPlayer
.
getDuration
());
txtsstop
.
setText
(
endTime
);
final
Handler
handler
=
new
Handler
();
final
int
delay
=
1000
;
handler
.
postDelayed
(
new
Runnable
()
{
@Override
public
void
run
()
{
String
currentTime
=
createTime
(
mediaPlayer
.
getCurrentPosition
());
txtsstart
.
setText
(
currentTime
);
handler
.
postDelayed
(
this
,
delay
);
}
},
delay
);
btnplay
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
mediaPlayer
.
isPlaying
())
{
btnplay
.
setBackgroundResource
(
R
.
drawable
.
ic_play
);
mediaPlayer
.
pause
();
}
else
{
btnplay
.
setBackgroundResource
(
R
.
drawable
.
ic_pause
);
mediaPlayer
.
start
();
}
}
});
//next listener
mediaPlayer
.
setOnCompletionListener
(
new
MediaPlayer
.
OnCompletionListener
()
{
@Override
public
void
onCompletion
(
MediaPlayer
mediaPlayer
)
{
btnnext
.
performClick
();
}
});
btnnext
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
mediaPlayer
.
isPlaying
())
{
mediaPlayer
.
stop
();
mediaPlayer
.
release
();
}
/*position = ((position+1)%mySongs.size());*/
position
=
(((
position
+
1
)==
mySongs
.
size
())?
0
:
position
+
1
);
Uri
u
=
Uri
.
parse
(
mySongs
.
get
(
position
).
toString
());
mediaPlayer
=
MediaPlayer
.
create
(
getApplicationContext
(),
u
);
sname
=
mySongs
.
get
(
position
).
getName
();
txtsname
.
setText
(
sname
);
mediaPlayer
.
start
();
btnplay
.
setBackgroundResource
(
R
.
drawable
.
ic_pause
);
startAnimation
(
imagevView
);
}
});
btnpre
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
mediaPlayer
.
isPlaying
())
{
mediaPlayer
.
stop
();
mediaPlayer
.
release
();
}
/*position = ((position-1)<0)?(mySongs.size()-1):(position-1);*/
position
=((
position
-
1
)<
0
)?
mySongs
.
size
()-
1
:(
position
-
1
);
Uri
ur
=
Uri
.
parse
(
mySongs
.
get
(
position
).
toString
());
mediaPlayer
=
MediaPlayer
.
create
(
getApplicationContext
(),
ur
);
sname
=
mySongs
.
get
(
position
).
getName
();
txtsname
.
setText
(
sname
);
mediaPlayer
.
start
();
btnplay
.
setBackgroundResource
(
R
.
drawable
.
ic_pause
);
startAnimation
(
imagevView
);
}
});
btnff
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
mediaPlayer
.
isPlaying
())
{
mediaPlayer
.
seekTo
(
mediaPlayer
.
getCurrentPosition
()+
10000
);
}
}
});
btnfr
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
mediaPlayer
.
isPlaying
())
{
mediaPlayer
.
seekTo
(
mediaPlayer
.
getCurrentPosition
()-
10000
);
}
}
});
}
public
void
startAnimation
(
View
view
)
{
ObjectAnimator
animator
=
ObjectAnimator
.
ofFloat
(
imagevView
,
"rotation"
,
0
f
,
360
f
);
animator
.
setDuration
(
1000
);
AnimatorSet
animatorSet
=
new
AnimatorSet
();
animatorSet
.
playTogether
(
animator
);
animatorSet
.
start
();
}
public
String
createTime
(
int
duration
)
{
String
time
=
""
;
int
min
=
duration
/
1000
/
60
;
int
sec
=
duration
/
1000
%
60
;
time
+=
min
+
":"
;
if
(
sec
<
10
)
{
time
+=
"0"
;
}
time
+=
sec
;
return
time
;
}
@Override
public
void
onBackPressed
()
{
mediaPlayer
.
stop
();
Intent
intent
=
new
Intent
(
MusicTherapyS
.
this
,
MusicTherapyF
.
class
);
startActivity
(
intent
);
finish
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment