moralschool
سه شنبه 06 اسفند 1392, 22:05 عصر
برای استفاده از سیک بار seekbar برای جلو و عقب کردن فایل صوتی ، در برنامه نویسی اندروید مشکلم اینه که وقتی روی نوار زمان فایل صوتی کلیک میکنم موسیقی که در حال پخش هست جلو و عقب نمیشه!
اشکال کار من کجاست ؟
چه کدی و در کجای کد زیر باید قرار بدم تا سیک بار درست کار کنه و مثلا با کلیک در وسط سیک بار موسیقی هم از همونجا ادامه پیدا کنه …
کد XMl :
-------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text_shown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120px"
android:text=""
android:textSize="42px" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/play_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:gravity="center"
android:text="Play" />
<Button
android:id="@+id/pause_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:text="Pause" />
</RelativeLayout>
کد اکتیویتی :
package com.example.test80;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.win8);
seek_bar.setMax(player.getDuration());
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
text_shown.setText("Playing...");
player.start();
break;
case R.id.pause_button:
player.pause();
text_shown.setText("Paused...");
}
}
}
اشکال کار من کجاست ؟
چه کدی و در کجای کد زیر باید قرار بدم تا سیک بار درست کار کنه و مثلا با کلیک در وسط سیک بار موسیقی هم از همونجا ادامه پیدا کنه …
کد XMl :
-------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text_shown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120px"
android:text=""
android:textSize="42px" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/play_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:gravity="center"
android:text="Play" />
<Button
android:id="@+id/pause_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_below="@+id/seek_bar"
android:layout_marginTop="60px"
android:text="Pause" />
</RelativeLayout>
کد اکتیویتی :
package com.example.test80;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.win8);
seek_bar.setMax(player.getDuration());
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
text_shown.setText("Playing...");
player.start();
break;
case R.id.pause_button:
player.pause();
text_shown.setText("Paused...");
}
}
}