-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
94 lines (59 loc) · 2.24 KB
/
MainActivity.java
File metadata and controls
94 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.example.mediaplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button play;
private Button pause;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = findViewById(R.id.play);
// pause = findViewById(R.id.pause);
seekBar = findViewById(R.id.seekBar);
// Play music using local source
final MediaPlayer mediaPlayer = new MediaPlayer().create(this, R.raw.desikalakaar);
// Play music using remote source
// MediaPlayer mediaPlayer1 = new MediaPlayer();
// try {
// mediaPlayer1.setDataSource("https://google.com");
// } catch (IOException e) {
// e.printStackTrace();
// }
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mediaPlayer.setLooping(true);
play.setText("Play");
} else {
mediaPlayer.start();
play.setText("Pause");
}
}
});
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}