1
+ package com .hrupin .mp3player ;
2
+
3
+ import com .hrupin .mp3player .R ;
4
+
5
+ import android .app .Activity ;
6
+ import android .media .MediaPlayer ;
7
+ import android .os .Bundle ;
8
+ import android .os .Handler ;
9
+ import android .view .MotionEvent ;
10
+ import android .view .View ;
11
+ import android .view .View .OnClickListener ;
12
+ import android .view .View .OnTouchListener ;
13
+ import android .widget .Button ;
14
+ import android .widget .SeekBar ;
15
+
16
+ public class Mp3player extends Activity {
17
+
18
+ private Button buttonPlayStop ;
19
+ private MediaPlayer mediaPlayer ;
20
+ private SeekBar seekBar ;
21
+
22
+ private final Handler handler = new Handler ();
23
+
24
+ // Here i override onCreate method.
25
+ //
26
+ // setContentView() method set the layout that you will see then
27
+ // the application will starts
28
+ //
29
+ // initViews() method i create to init views components.
30
+ @ Override
31
+ public void onCreate (Bundle icicle ) {
32
+ super .onCreate (icicle );
33
+ setContentView (R .layout .main );
34
+ initViews ();
35
+
36
+ }
37
+
38
+ // This method set the setOnClickListener and method for it (buttonClick())
39
+ private void initViews () {
40
+ buttonPlayStop = (Button ) findViewById (R .id .ButtonPlayStop );
41
+ buttonPlayStop .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v ) {buttonClick ();}});
42
+
43
+ mediaPlayer = MediaPlayer .create (this , R .raw .testsong_20_sec );
44
+
45
+ seekBar = (SeekBar ) findViewById (R .id .SeekBar01 );
46
+ seekBar .setMax (mediaPlayer .getDuration ());
47
+ seekBar .setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v , MotionEvent event ) {
48
+ seekChange (v );
49
+ return false ; }
50
+ });
51
+
52
+ }
53
+
54
+ public void startPlayProgressUpdater () {
55
+ seekBar .setProgress (mediaPlayer .getCurrentPosition ());
56
+
57
+ if (mediaPlayer .isPlaying ()) {
58
+ Runnable notification = new Runnable () {
59
+ public void run () {
60
+ startPlayProgressUpdater ();
61
+ }
62
+ };
63
+ handler .postDelayed (notification ,1000 );
64
+ }else {
65
+ mediaPlayer .pause ();
66
+ buttonPlayStop .setText (getString (R .string .play_str ));
67
+ seekBar .setProgress (0 );
68
+ }
69
+ }
70
+
71
+ // This is event handler thumb moving event
72
+ private void seekChange (View v ){
73
+ if (mediaPlayer .isPlaying ()){
74
+ SeekBar sb = (SeekBar )v ;
75
+ mediaPlayer .seekTo (sb .getProgress ());
76
+ }
77
+ }
78
+
79
+ // This is event handler for buttonClick event
80
+ private void buttonClick (){
81
+ if (buttonPlayStop .getText () == getString (R .string .play_str )) {
82
+ buttonPlayStop .setText (getString (R .string .pause_str ));
83
+ try {
84
+ mediaPlayer .start ();
85
+ startPlayProgressUpdater ();
86
+ }catch (IllegalStateException e ) {
87
+ mediaPlayer .pause ();
88
+ }
89
+ }else {
90
+ buttonPlayStop .setText (getString (R .string .play_str ));
91
+ mediaPlayer .pause ();
92
+ }
93
+ }
94
+ }
0 commit comments