|
|
|
Android SeekBar ไม่เลี่อนตามเวลาของเพลง ไม่แอคทีฟเวลาเริ่มกดฟังเพลง แต่เลี่อนไปตามเวลาที่ต้องการได้ปกติค่ะ |
|
|
|
|
|
|
|
Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/forest01"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ForestActivity" >
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="44dp"
android:layout_marginTop="54dp"
android:text="Peaceful" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="Geffen" />
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Payon" />
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="Ground" />
<Button
android:id="@+id/button5"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="Plateau" />
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button5"
android:layout_toRightOf="@+id/button5"
android:text="Stream" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="68dp"
android:text="Sound Volume Control"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="68dp"
android:text="Sound Volume Control"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar2"
android:layout_alignLeft="@+id/textView1"
android:text="Play" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_toRightOf="@+id/button7"
android:text="Pause" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_toRightOf="@+id/button8"
android:text="Stop" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_alignParentLeft="true" />
</RelativeLayout>
Code
package com.example.relaxinglife;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class ForestActivity extends Activity implements View.OnClickListener{
private final Button btn[] = new Button[6];
private MediaPlayer mPlayer;
private Handler handler = new Handler();
private SeekBar seekBar1;
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_forest);
// call method ValumnControls()
ValumnControls();
// Variable
btn[0] = (Button) findViewById(R.id.button1);
btn[1] = (Button) findViewById(R.id.button2);
btn[2] = (Button) findViewById(R.id.button3);
btn[3] = (Button) findViewById(R.id.button4);
btn[4] = (Button) findViewById(R.id.button5);
btn[5] = (Button) findViewById(R.id.button6);
for (int i =0; i < btn.length; i++) {
btn[i].setOnClickListener(this);
}
final Button btn7 = (Button) findViewById(R.id.button7); // Start
final Button btn8 = (Button) findViewById(R.id.button8); // Pause
final Button btn9 = (Button) findViewById(R.id.button9); // Stop
// Start
btn7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPlayer.start();
btn7.setEnabled(false);
btn8.setEnabled(true);
btn9.setEnabled(true);
}
});
// Pause
btn8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPlayer.pause();
btn7.setEnabled(true);
btn8.setEnabled(false);
btn9.setEnabled(false);
}
});
// Stop
btn9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPlayer.stop();
btn7.setEnabled(true);
btn8.setEnabled(false);
btn9.setEnabled(false);
try {
mPlayer.prepare();
mPlayer.seekTo(0);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void onClick(View v){
int resId = 0;
switch (v.getId()) {
case R.id.button1 :
resId = R.raw.peaceful_forest;
break;
case R.id.button2 :
resId = R.raw.theme_of_geffen;
break;
case R.id.button3 :
resId = R.raw.theme_of_payon;
break;
case R.id.button4 :
resId = R.raw.on_the_ground;
break;
case R.id.button5 :
resId = R.raw.plateau;
break;
case R.id.button6 :
resId = R.raw.streamside;
break;
}
playSound(resId);
}
private void playSound(int position) {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this,position);
mPlayer.start();
// seekBar1 Control media
seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
seekBar1.setMax(mPlayer.getDuration());
seekBar1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
UpdateseekChange(v);
return false;
}
});
}
private void UpdateseekChange(View v){
if(mPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
mPlayer.seekTo(sb.getProgress());
}
}
public void startPlayProgressUpdater() {
seekBar1.setProgress(mPlayer.getCurrentPosition());
if (mPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mPlayer != null){
mPlayer.release();
}
}
private void ValumnControls()
{
try
{
SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekBar2.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar2.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar arg0)
{
}
public void onStartTrackingTouch(SeekBar arg0)
{
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}}
Tag : Java, Android, JAVA
|
|
|
|
|
|
Date :
2014-11-17 10:58:10 |
By :
mewnichapa |
View :
1124 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูบทความนี้ครับ
Android SeekBar Control MediaPlayer Progress
|
|
|
|
|
Date :
2014-11-17 15:37:53 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จริงๆดูตัวอย่างจากลิ้งข้างบนเลยนะละจ้า แต่เอามามิกซ์กับโค้ดของตัวเอง เลยเกิดปัญหาไม่ยอมเลื่อนตามเพลง
|
|
|
|
|
Date :
2014-11-17 18:15:46 |
By :
mewnichapa |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|