我正在尝试从绑定服务更新我的搜索栏,该服务的方法可以从活动中访问,但是如果我调用该方法来更新我的搜索栏,则什么也不会发生,甚至媒体播放器也会停止播放,但是如果我删除该更新seekbar方法mediaplayer开始播放没有任何问题。 我真的被困住了,不胜感激。
这是我的seekbarchange方法的代码:
fun upprogress() {
val handler = Handler()
var mediaPos = musicSrv!!.posn
var mediaMax = musicSrv!!.dur
seekbr!!.setMax(mediaMax.toFloat()) // Set the Maximum range of the
seekbr!!.setProgress(mediaPos.toFloat())// set current progress to song's
val moveSeekBarThread = object : Runnable {
override fun run() {
if (isPlaying) {
val mediaPos_new = musicSrv!!.posn
val mediaMax_new = musicSrv!!.dur
seekbr!!.setMax(mediaMax_new.toFloat())
seekbr!!.setProgress(mediaPos_new.toFloat())
handler.postDelayed(this, 100) //Looping the thread after 0.1 second
}
}
}
handler.postDelayed(moveSeekBarThread, 100)
seekbr!!.setOnSeekChangeListener(object : OnSeekChangeListener {
override fun onSeeking(seekParams: SeekParams) {
handler.removeCallbacksAndMessages(null)
}
override fun onStartTrackingTouch(seekBar: IndicatorSeekBar) {
handler.removeCallbacksAndMessages(null)
}
override fun onStopTrackingTouch(seekBar: IndicatorSeekBar) {
if (isPlaying) {
musicSrv!!.seek(seekBar.getProgress())
handler.postDelayed(moveSeekBarThread, 100)
}
}
})
}
这是我的服务等级:
public class MusicService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
//media player
private MediaPlayer player;
//song list
private ArrayList<Song> songs;
//current position
private int songPosn;
//binder
private final IBinder musicBind = new MusicBinder();
//title of current song
private String songTitle="";
//notification id
private static final int NOTIFY_ID=1;
//shuffle flag and random
private boolean shuffle=false;
private Random rand;
public void onCreate(){
//create the service
super.onCreate();
//initialize position
songPosn=0;
//random
rand=new Random();
//create player
player = new MediaPlayer();
//initialize
initMusicPlayer();
}
public void initMusicPlayer(){
//set player properties
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//set listeners
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
//pass song list
public void setList(ArrayList<Song> theSongs){
songs=theSongs;
}
//binder
public class MusicBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
public boolean isPlaying() {
return player.isPlaying();
}
//activity will bind to service
@Override
public IBinder onBind(Intent intent) {
return musicBind;
}
//release resources when unbind
@Override
public boolean onUnbind(Intent intent){
player.stop();
player.release();
return false;
}
//play a song
public void playSong(){
//play
player.reset();
//get song
Song playSong = songs.get(songPosn);
//get title
songTitle=playSong.getTitle();
//get id
long currSong = playSong.getID();
//set uri
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
//set the data source
try{
player.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
player.prepareAsync();
}
//set the song
public void setSong(int songIndex){
songPosn=songIndex;
}
@Override
public void onCompletion(MediaPlayer mp) {
//check if playback has reached the end of a track
if(player.getCurrentPosition()>0){
mp.reset();
playNext();
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("MUSIC PLAYER", "Playback Error");
mp.reset();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
mp.start();
}
//playback methods
public int getPosn(){
return player.getCurrentPosition();
}
public int getDur(){
return player.getDuration();
}
public boolean isPng(){
return player.isPlaying();
}
public void pausePlayer(){
player.pause();
}
public void seek(int posn){
player.seekTo(posn);
}
public void go(){
player.start();
}
//skip to previous track
public void playPrev(){
songPosn--;
if(songPosn<0) songPosn=songs.size()-1;
playSong();
}
//skip to next
public void playNext(){
if(shuffle){
int newSong = songPosn;
while(newSong==songPosn){
newSong=rand.nextInt(songs.size());
}
songPosn=newSong;
}
else{
songPosn++;
if(songPosn>=songs.size()) songPosn=0;
}
playSong();
}
@Override
public void onDestroy() {
stopForeground(true);
}
//toggle shuffle
public void setShuffle(){
if(shuffle) shuffle=false;
else shuffle=true;
}
}