0

我有一个搜索栏,需要从服务中更新。但是光标根本不动。这是代码(您可以看到很多布局,因为我只放了您需要的代码,但这些布局中还有其他视图对象):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="-80dp"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <SeekBar
                android:id="@+id/seekBarTime"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

爪哇

public class MediaPlayerService extends Service implements Runnable {

    private MediaPlayer mediaPlayer = null;
    private SeekBar seekBarTime;
    private Handler handler;
    private Runnable runnable;

    @Override
    public void onCreate() {
        //SET SEEKBAR
        LayoutInflater inflater = ( LayoutInflater ) getSystemService( LAYOUT_INFLATER_SERVICE );
        View layout = inflater.inflate( R.layout.play_song , null);
        seekBarTime = (SeekBar) layout.findViewById(R.id.seekBarTime);
        seekBarTime.setOnSeekBarChangeListener(this);

        handler = new Handler();
        runnable = this;
        handler.post(runnable);
    }

    // Seekbar
    public void run() {     
        if(mediaPlayer.isPlaying()) {


            int currentPosition = 0;
            int total = mediaPlayer.getDuration();
            seekBarTime.setMax(total);
            currentPosition = mediaPlayer.getCurrentPosition();

            Log.w("disgustingapps", currentPosition + " / " + total);

            seekBarTime.setProgress(currentPosition);

        }

        handler.postDelayed(runnable, 1000);
    }
}

logcat 向我保证 currentPosition 和 total 不为空。然而什么也没发生。你能帮助我吗?提前致谢

4

1 回答 1

0

它应该工作。我测试了你的run()方法,在我这边,SeekBar 更新得很好。
您在哪里创建和启动MediaPlayer?

run() 方法正在检查媒体播放器是否正在播放:

if(mediaPlayer.isPlaying()) {
...
}

但我没看到mediaPlayer().start()

于 2013-01-05T20:04:28.650 回答