循环变量未在循环内更新

时间:2018-12-23 12:03:10

标签: android loops audio streaming android-mediaplayer

我有一个音频流应用程序,当我尝试实现该线程时,出现此错误“循环内部未更新循环变量”。我该如何更新循环。

这是调试消息

E / MediaPlayerNative:尝试在错误状态下执行seekTo:mPlayer = 0xa1577660,mCu​​rrentState = 0     错误(-38,0)     尝试以错误状态调用getDuration:mPlayer = 0xa1577660,mCu​​rrentState = 0     错误(-38,0)

这是我的代码

package com.excellentaccountant.mohammed.myapplication;

 import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Button playBt1;
SeekBar positionBar;
TextView elopsedtimeLable;
TextView remainingTimeLable;
MediaPlayer mp;
int totalTime;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    playBt1 = (Button)findViewById(R.id.playButton);


    final String url = "URL"
    final MediaPlayer mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);



    try {



        playBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (!mp.isPlaying()){
                mp.start();
                playBt1.setBackgroundResource(R.drawable.pause);
            }

            else {

                mp.pause();
                playBt1.setBackgroundResource(R.drawable.play);
            }
            }
        });


        mp.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mp.prepareAsync(); // might take long! (for buffering, etc)
    mp.start();



    positionBar = (SeekBar)findViewById(R.id.positionBar);
    positionBar.setMax(totalTime);
    positionBar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){

                    mp.seekTo(progress);
                    positionBar.setProgress(progress);

                }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            }
    );

new Thread(new Runnable() {


@Override
public void run() {

    while (mp != null){




        try {

            Message msg = new Message();
            msg.what = mp.getCurrentPosition();
            handler.sendMessage(msg);



            Thread.sleep(1000);
        }catch (InterruptedException e){}

    }
}
}).start();


}
private Handler handler = new Handler (){

    @Override
    public void handleMessage(Message msg) {
        int currentPosition = msg.what;
        positionBar.setProgress(currentPosition);

        String elopsedTime = createTimeLable (currentPosition);
        elopsedtimeLable.setText(elopsedTime);

        String remainingtime = createTimeLable(totalTime-currentPosition);
        remainingTimeLable.setText("- " + remainingtime);

    }
};


public String createTimeLable (int time){

    String timelable = "";
    int min = time /1000/60;
    int sec = time /1000 % 60;

    timelable = min + ":";
    if (sec < 10)timelable += "0";
    timelable += sec;
       return timelable;



    }



}

0 个答案:

没有答案