001.
package
com.myapp;
002.
003.
004.
import
java.io.IOException;
005.
006.
import
android.media.AudioManager;
007.
import
android.media.MediaPlayer;
008.
import
android.os.Bundle;
009.
import
android.os.Handler;
010.
import
android.app.Activity;
011.
import
android.content.Context;
012.
import
android.view.Menu;
013.
import
android.view.View;
014.
import
android.view.View.OnTouchListener;
015.
import
android.widget.Button;
016.
import
android.view.MotionEvent;
017.
import
android.widget.SeekBar;
018.
import
android.widget.SeekBar.OnSeekBarChangeListener;
019.
import
android.widget.TextView;
020.
021.
public
class
MainActivity
extends
Activity {
022.
023.
private
MediaPlayer mMedia;
024.
private
Handler handler =
new
Handler();
025.
private
SeekBar seekBar1;
026.
027.
028.
@Override
029.
public
void
onCreate(Bundle savedInstanceState) {
030.
super
.onCreate(savedInstanceState);
031.
setContentView(R.layout.activity_main);
032.
033.
034.
ValumnControls();
035.
036.
037.
if
(mMedia !=
null
){
038.
mMedia.release();
039.
}
040.
041.
final
TextView txtView = (TextView)findViewById(R.id.textView1);
042.
txtView.setText(
"Source : music.mp3"
);
043.
044.
045.
046.
047.
048.
049.
050.
051.
052.
053.
054.
055.
056.
mMedia = MediaPlayer.create(
this
, R.raw.music);
057.
058.
059.
seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
060.
seekBar1.setMax(mMedia.getDuration());
061.
seekBar1.setOnTouchListener(
new
OnTouchListener() {
062.
public
boolean
onTouch(View v, MotionEvent event) {
063.
UpdateseekChange(v);
064.
return
false
;
065.
}
066.
});
067.
068.
069.
070.
071.
final
Button btn1 = (Button) findViewById(R.id.button1);
072.
final
Button btn2 = (Button) findViewById(R.id.button2);
073.
final
Button btn3 = (Button) findViewById(R.id.button3);
074.
075.
076.
077.
btn1.setOnClickListener(
new
View.OnClickListener() {
078.
public
void
onClick(View v) {
079.
txtView.setText(
"Playing : music.mp3...."
);
080.
mMedia.start();
081.
startPlayProgressUpdater();
082.
btn1.setEnabled(
false
);
083.
btn2.setEnabled(
true
);
084.
btn3.setEnabled(
true
);
085.
}
086.
});
087.
088.
089.
btn2.setOnClickListener(
new
View.OnClickListener() {
090.
public
void
onClick(View v) {
091.
txtView.setText(
"Pause : music.mp3"
);
092.
mMedia.pause();
093.
btn1.setEnabled(
true
);
094.
btn2.setEnabled(
false
);
095.
btn3.setEnabled(
false
);
096.
}
097.
});
098.
099.
100.
btn3.setOnClickListener(
new
View.OnClickListener() {
101.
public
void
onClick(View v) {
102.
txtView.setText(
"Stop Play : music.mp3"
);
103.
mMedia.stop();
104.
btn1.setEnabled(
true
);
105.
btn2.setEnabled(
false
);
106.
btn3.setEnabled(
false
);
107.
try
{
108.
mMedia.prepare();
109.
mMedia.seekTo(
0
);
110.
}
catch
(IllegalStateException e) {
111.
112.
e.printStackTrace();
113.
}
catch
(IOException e) {
114.
115.
e.printStackTrace();
116.
}
117.
118.
}
119.
});
120.
121.
}
122.
123.
private
void
UpdateseekChange(View v){
124.
if
(mMedia.isPlaying()){
125.
SeekBar sb = (SeekBar)v;
126.
mMedia.seekTo(sb.getProgress());
127.
}
128.
}
129.
130.
public
void
startPlayProgressUpdater() {
131.
seekBar1.setProgress(mMedia.getCurrentPosition());
132.
133.
if
(mMedia.isPlaying()) {
134.
Runnable notification =
new
Runnable() {
135.
public
void
run() {
136.
startPlayProgressUpdater();
137.
}
138.
};
139.
handler.postDelayed(notification,
1000
);
140.
}
141.
}
142.
143.
144.
@Override
145.
protected
void
onDestroy() {
146.
147.
super
.onDestroy();
148.
if
(mMedia !=
null
){
149.
mMedia.release();
150.
}
151.
}
152.
153.
154.
private
void
ValumnControls()
155.
{
156.
try
157.
{
158.
SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
159.
final
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
160.
seekBar2.setMax(audioManager
161.
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
162.
seekBar2.setProgress(audioManager
163.
.getStreamVolume(AudioManager.STREAM_MUSIC));
164.
165.
166.
seekBar2.setOnSeekBarChangeListener(
new
OnSeekBarChangeListener()
167.
{
168.
public
void
onStopTrackingTouch(SeekBar arg0)
169.
{
170.
}
171.
172.
public
void
onStartTrackingTouch(SeekBar arg0)
173.
{
174.
}
175.
176.
public
void
onProgressChanged(SeekBar arg0,
int
progress,
boolean
arg2)
177.
{
178.
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
179.
progress,
0
);
180.
}
181.
});
182.
}
183.
catch
(Exception e)
184.
{
185.
e.printStackTrace();
186.
}
187.
}
188.
189.
@Override
190.
public
boolean
onCreateOptionsMenu(Menu menu) {
191.
getMenuInflater().inflate(R.menu.activity_main, menu);
192.
return
true
;
193.
}
194.
195.
}