-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppCompatActivity .java
74 lines (60 loc) · 2.13 KB
/
AppCompatActivity .java
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
package main.java.com.example.mediaapp;
// package com.example.mediaapp;
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 android.widget.Toast;
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);
// mediaplayer creat a local source
MediaPlayer mediaPlayer=MediaPlayer.create(this,R.raw.odhani);
// mediaplayer using Remote source
// try {
// mediaPlayer.setDataSource("http://penguinradio.dominican.edu/Sound%20FX%20Collection/Cat%20meow%201.mp3");
// } catch (IOException e) {
// e.printStackTrace();
// }
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) {
}
});
mediaPlayer.start();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
}
}