Problem with Seekbar
-
I'm trying to make an audio book. I was able to reproduce from the track record, but I can't put SeekBar. I also need to get the name of the lost track.
package simolapps.myapplication;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;public class MainActivity extends Activity {
// variable declaration private ListView mainList; private MediaPlayer mp; private SeekBar mSeekBar; TextView tb_title; int pos = 0; private final Handler handler = new Handler(); private final String[] listContent = { "0000", "0001", }; private final int[] resID = { R.raw.a0001, R.raw.a0000, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); // Initializing variables } private void initViews() { mp = new MediaPlayer(); mainList = (ListView) findViewById(R.id.listView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContent); mainList.setAdapter(adapter); mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { playSong(position); } }); mSeekBar = (SeekBar) findViewById(R.id.mSeekBar); mSeekBar.setMax(mp.getDuration()); mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser && mp.isPlaying()) { mp.seekTo(seekBar.getProgress()); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } private void seekChange(View v){ if(mp.isPlaying()){ SeekBar sb = (SeekBar)v; mp.seekTo(sb.getProgress()); } } public void play (View view) { if (mp.isPlaying()) { mp.pause(); } else { mp.start(); } } public void stop (View view) { mp.stop(); } public void playSong(int songIndex) {
// Play song
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getApplicationContext(), resID[songIndex]);// create'smp.start(); // starting mediaplayer startPlayProgressUpdater(); } public void startPlayProgressUpdater() { mSeekBar.setProgress(mp.getCurrentPosition()); if (mp.isPlaying()) { Runnable notification = new Runnable() { public void run() { startPlayProgressUpdater(); } }; handler.postDelayed(notification,1000); }else{ mp.pause(); mSeekBar.setProgress(0); } } @Override public void onDestroy() { super.onDestroy(); mp.release(); }
}
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"><LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" android:id="@+id/button" android:onClick="play" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="STOP" android:id="@+id/button2" android:onClick="stop" /> </LinearLayout> <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mSeekBar" android:indeterminate="false" android:max="1000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/tb_title" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > </ListView>
</LinearLayout>
-
Ooh.
SeekBar
is your own.Listener
to change progress. I hate to use colts withonTouch
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser && mp.isPlaying()) { mp.seekTo(seekBar.getProgress()); } }
@Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { }
});