Я работаю над приложением медиаплеера. Я пытаюсь синхронизировать его с панелью поиска, чтобы при воспроизведении мультимедиа панель поиска автоматически прогрессировала. Вот мой исходный код:
public class MusicDroid extends ListActivity implements
SeekBar.OnSeekBarChangeListener, Runnable {
private int cur_time = 0;
private SeekBar seekbar;
int song_dur = 0;
GroveService gs;
public Chronometer timer;
int timer_time = 0;
@Override
public void onCreate(Bundle icicle) {
try {
super.onCreate(icicle);
setContentView(R.layout.songlist);
gs = new GroveService();
gs.updateSongList();
} catch (NullPointerException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
seekbar = (SeekBar) findViewById(R.id.SeekBar01);
timer = (Chronometer) findViewById(R.id.Chronometer01);
seekbar.setOnSeekBarChangeListener(this);
// this.bindService(new Intent(this, GroveService.class), conn,
// Context.BIND_AUTO_CREATE);
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
R.layout.song_item, gs.songs);
setListAdapter(songList);
timer.setOnChronometerTickListener(new OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer arg0) {
// TODO Auto-generated method stub
seekbar.setProgress(cur_time);
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
timer.stop();
timer.start();
gs.playSong(position, 0);
}
/*
* private ServiceConnection conn= new ServiceConnection(){
*
* @Override public void onServiceConnected(ComponentName arg0, IBinder
* arg1) { Log.v("Status:","Service Connected to player"); // TODO
* Auto-generated method stub tprog= new Thread(new Runnable(){
*
* }); tprog.setPriority(Thread.MIN_PRIORITY); tprog.start(); }
*
* @Override public void onServiceDisconnected(ComponentName arg0) { // TODO
* Auto-generated method stub
*
* } };
*/
// code for menu based media player controls
/*
* public boolean onOptionsItemSelected(MenuItem item) { switch
* (item.getItemId()) { case R.id.prev: gs.prevSong(); break; case
* R.id.play: gs.playSong(gs.songindex,cur_time); break; case R.id.pause:
* gs.mp.getDuration(); gs.mp.pause(); break; case R.id.nxt: gs.nextSong();
* break; } return true; }
*/
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.ImageButton01:
gs.prevSong();
timer.stop();
seekbar.setMax(gs.getSongDuration(gs.songindex));
Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
timer.start();
break;
case R.id.ImageButton02:
Thread t=new Thread();
t.start();
gs.playSong(gs.songindex, cur_time);
timer.start();
seekbar.setMax(gs.getSongDuration(gs.songindex));
Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
break;
case R.id.ImageButton03:
cur_time = gs.getCurrentDuration(gs.songindex);
if(gs.mp.isPlaying()){
timer_time=Integer.parseInt((String)timer.getText());
timer.stop();
timer.setBase(timer_time);
Log.v("Timer Time",String.valueOf(timer_time)); }
gs.mp.pause();
seekbar.setMax(gs.getSongDuration(gs.songindex));
Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
break;
case R.id.ImageButton04:
gs.nextSong();
timer.stop();
timer.start();
seekbar.setMax(gs.getSongDuration(gs.songindex));
Log.v("SeekBar Max", String.valueOf(seekbar.getMax()));
break;
}
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
gs.mp.seekTo(arg1);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
int currentPosition = 0;
int total = gs.mp.getDuration();
seekbar.setMax(total);
while (gs.mp != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = gs.mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekbar.setProgress(currentPosition);
}
}
}
Класс обслуживания, который выполняет воспроизведение музыки. код здесь:
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class GroveService extends Service {
static final String MEDIA_PATH = new String("/sdcard/");
List<String> songs = new ArrayList<String>();
MediaPlayer mp = new MediaPlayer();
int songindex = 0;
int duration=0;
//MusicDroid md;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
super.onCreate();
updateSongList();
//md=new MusicDroid();
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
}
}
}
public void playSong(final int position, final int cur_pos) {
try {
mp.reset();
mp.setDataSource(MEDIA_PATH + songs.get(position));
mp.prepare();
//song_dur=mp.getDuration();
mp.seekTo(cur_pos);
mp.start();
duration=position;
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
public void nextSong() {
if (++songindex >= songs.size())
songindex = 0;
else
playSong(songindex,0);
}
public void prevSong() {
songindex--;
if (songindex > 0)
playSong(songindex,0);
else {
songindex = 0;
playSong(songindex,0);
}
}
public int getSongDuration(int index)
{
duration=mp.getDuration();
Log.v("Song Duration", String.valueOf(duration));
return duration;
}
public int getCurrentDuration(int index){
int current_position;
current_position=mp.getCurrentPosition();
Log.v("Current Dur:",String.valueOf(current_position));
return current_position;
}
}