1

我已经为我的应用程序实现了一个搜索栏,它是一个简单的音乐播放器。但是当我播放音乐(从 uri 接收)时,它不会在运行时自行更新。但是当我将它移到某个位置时,它会将歌曲移到那里。任何想法如何解决这个问题?

我的代码:

public class MusicPlayer extends ThemableActivity implements SeekBar.OnSeekBarChangeListener {
MediaPlayer musicPlayer;

@Override
protected void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.media_player_layout);
    Intent intent = getIntent();
    String track = intent.getDataString();
    TextView txt = (TextView)findViewById(R.id.txt);
    txt.setGravity(Gravity.CENTER);
    txt.setText(track);
    ImageView img = (ImageView)findViewById(R.id.img);
    img.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    final Uri uri = getIntent() != null ? getIntent().getData() : null;
    final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
    final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
    final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setOnSeekBarChangeListener(this);


    if (isPremium) {
        musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
        seekBar.setMax(musicPlayer.getDuration()/1000);
        musicPlayer.start();
        final ImageButton play = (ImageButton) findViewById(R.id.play);
        if(!musicPlayer.isPlaying()){

            play.setImageResource(android.R.drawable.ic_media_play);
        }
        else{
            play.setImageResource(android.R.drawable.ic_media_pause);

        }
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = musicPlayer.getCurrentPosition();
                if(musicPlayer.isPlaying()){
                    musicPlayer.pause();
                    play.setImageResource(android.R.drawable.ic_media_play);

                }
                else {
                    musicPlayer.seekTo(position);
                    musicPlayer.start();
                    play.setImageResource(android.R.drawable.ic_media_pause);

            }}
        });
        final Handler mHandler = new Handler();
        Runnable mRunnable = new Runnable() {

            @Override
            public void run() {
                if(musicPlayer!=null){
                    int mCurrentPosition = musicPlayer.getCurrentPosition() / 1000;
                    seekBar.setProgress(mCurrentPosition);
                }
                mHandler.postDelayed(this, 1000);
            }
        };

        ImageButton exit = (ImageButton) findViewById(R.id.exit);
        exit.setImageResource(android.R.drawable.btn_dialog);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (musicPlayer.isPlaying()) {
                    musicPlayer.stop();
                }
                    musicPlayer.release();
                    musicPlayer = null;
                    finish();

            }
        });


    }
    else {
        AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayer.this);
        builder.setTitle("Error");
        builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
        builder.setIcon(R.drawable.holo_dark_action_info);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                finish();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }}
public void onBackPressed () {
        if(musicPlayer.isPlaying()) {
            musicPlayer.stop();
        }
            musicPlayer = null;
        finish();
}

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
    if (musicPlayer != null && b){
        musicPlayer.seekTo(i * 1000);
    }

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
4

2 回答 2

1

在您发布的代码段中,runnable 永远不会运行。我本来希望在 Runnable 本身mHandler.post(mRunnable);之外mRunnable.run();

于 2014-10-07T13:18:05.157 回答
0

创建一个简单的线程说“progressSeekBarOnEverySecond”

progressSeekBarOnEverySecond=new Runnable() {

        @Override
        public void run() {

            if(seekBar != null) 
            {
                seekBar.setProgress(musicPlayer.getCurrentPosition());
                textviewCurrentVideoTime.setText((musicPlayer.getCurrentPosition()/1000)/60 +":"+(musicPlayer.getCurrentPosition()/1000)%60);   
            }

            if(musicPlayer.isPlaying()) 
            {
                seekBar.postDelayed(onEverySecond, 1000);
            }

        }
    };

并在您单击播放时运行线程..或您要使用的任何逻辑

textviewCurrentVideoTime 是 textview ..我在其中显示视频的当前时间 ..还请注意,一旦调用线程 ..它将在 1 秒内自动调用自身,并且 seekbar 进度到视频的当前位置

于 2014-10-07T13:25:53.337 回答