当停止音乐时,Android媒体播放器进度条休息为“0”

时间:2015-07-10 01:21:03

标签: android progress-bar media-player

我把一首歌放入应用程序(tabhost),我有进度条,播放按钮,停止按钮,循环按钮。

我想要的功能是:

  • 当用户播放音乐时,进度条从“0”开始运行
  • 当用户按停止时,进度条停在原处,等待用户再次按播放,然后继续停止

我可能会稍后将按钮名称更改为“暂停”,但这是我现在需要的功能。

目前,有2个问题:

  1. 当按下停止时,进度条返回“0”,但音乐从中断处继续播放(音乐从一开始就没有播放)
  2. 进度条运行速度超过预期;它在音乐结束前一直运行到最后。
  3. 这是我的代码,希望有人能帮助你,谢谢你:

    import android.app.Activity;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ProgressBar;
    
    
    import java.util.logging.LogRecord;
    
    public class TabMusicActivity extends Activity {
        MediaPlayer mediaPlayer;
        private boolean playing = false;
    
        private ProgressBar progressBar;
        private Handler handler = new Handler();
        int progress = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_music);
    
            final ImageButton buttonStart = (ImageButton)findViewById(R.id.buttonStart);
    
            progressBar = (ProgressBar)findViewById(R.id.progressBar);
    
    
            buttonStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
    
                    if (!playing) {
                        playing = true;
                        buttonStart.setImageResource(R.drawable.music_stop_button);
                        Uri path = Uri.parse("android.resource://"+getPackageName()+"/"+ R.raw.bashibafo);
    
                        if (mediaPlayer == null) {
                            mediaPlayer = MediaPlayer.create(TabMusicActivity.this, path);
                        }
                        if (mediaPlayer != null) {
                            mediaPlayer.start();
                            process();
                        }
                    }
                    else {
                        playing = false;
                        buttonStart.setImageResource(R.drawable.button_play);
    
                        if(mediaPlayer.isPlaying()){
                            mediaPlayer.pause();
    
                        }
                    }
    
                }
            });
    
    
    
            ImageButton buttonLoop = (ImageButton)findViewById(R.id.buttonLoop);
            buttonLoop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer.setLooping(true);
                }
            });
        }
    
        private void process (){
            progressBar.setProgress(0);
            progress = 0;
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (progress < 100){
                        progress = doWork();
                        try{
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post( new Runnable() {
                            @Override
                            public void run() {
                                progressBar.setProgress(progress);
                            }
                        });
                    }
                }
            }).start();
        }
    
        private int doWork(){
            progress++;
            if (progress < 100){
                return progress;
            }
    
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 100;
        }
    
        @Override
        protected void onDestroy() {
            if(mediaPlayer!=null && mediaPlayer.isPlaying()){
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
            super.onDestroy();
        }
    }
    

1 个答案:

答案 0 :(得分:1)

我用这段代码播放音乐

 if (player != null)
        {
            player.seekTo(length);
            player.start();
        }
        else
        {
            player = MediaPlayer.create(context, Uri.fromFile(new File(url)));

            seek_bar.setMax(player.getDuration());

            seek_bar.setProgress(player.getCurrentPosition());

            player.setOnCompletionListener(new OnCompletionListener()
            {
                @Override
                public void onCompletion(MediaPlayer mp)
                {
                    player.reset();
                    player.release();
                    player = null;
                    seekHandler.removeCallbacks(run);
                    seek_bar.setProgress(0);
                    if (recieveOrSend.equals("send"))
                    {

                        holder.iv_audio_pause_send.setVisibility(View.INVISIBLE);
                        holder.iv_audio_play_send.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        holder.iv_audio_pause_recieve.setVisibility(View.INVISIBLE);
                        holder.iv_audio_play_recieve.setVisibility(View.VISIBLE);
                    }
                }
            });

            //          player.prepare();
            player.start();

        }
        seekUpdation();

方法 -

    Runnable run = new Runnable()
{
    @Override
    public void run()
    {

        seekUpdation();
    }
};

public void seekUpdation()
{
    seek_bar.setProgress(player.getCurrentPosition());
    seekHandler.postDelayed(run, 1000);
}

private void pausePlayer()
{

    try
    {
        if (player != null)
        {
            player.pause();
            length = player.getCurrentPosition();
            seekHandler.removeCallbacks(run);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}