gpt4 book ai didi

android - 搜索栏不在媒体播放器中滚动

转载 作者:行者123 更新时间:2023-11-30 01:30:05 25 4
gpt4 key购买 nike

我已经创建了一个简单的 android 媒体播放器,它工作正常,只是 seekbar 没有移动。当我手动移动 seekbar 时,它工作正常并且歌曲前进和后退。但它不会自动移动。下面是代码;

public class Datadetail extends AppCompatActivity implements Runnable, OnSeekBarChangeListener{

TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.data_detail);

Intent i = getIntent();

position = i.getExtras().getInt("position");
itemname = i.getStringArrayExtra("itemname");

txtname = (TextView) findViewById(R.id.textView2);
txtname.setText(itemname[position]);

txtaarti = (TextView) findViewById(R.id.textView3);
txtaarti.setText(Aarti.aartitxt[position]);

seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
seekbar.setEnabled(false);

buttonPlay = (ImageButton) findViewById(R.id.imageButton);

playsong(position);


buttonPlay.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if (mplayer.isPlaying()){
if (mplayer != null){
mplayer.pause();
buttonPlay.setImageResource(R.mipmap.ic_play);
}
}else{
if (mplayer != null){
mplayer.start();
seekbar.setMax(mplayer.getDuration());
new Thread(String.valueOf(this)).start();
buttonPlay.setImageResource(R.mipmap.ic_pause);

}
}
if (mplayer == null) {
if (mplayer.isPlaying() || mplayer.getDuration() > 0) {
mplayer.stop();
mplayer = null;
seekbar.setProgress(0);
}
}

}
});
}

public void playsong(int position) {
switch (position){
case 0:
resID = R.raw.ganesh_aarti;
break;
case 1:
resID = R.raw.durga_aarti;
break;

}
if (mplayer !=null){
mplayer.release();
}

mplayer = MediaPlayer.create(this, resID);
seekbar.setEnabled(true);
buttonPlay.setImageResource(R.mipmap.ic_play);

}

@Override
public void onBackPressed() {
if (mplayer.isPlaying()) {
mplayer.stop();
}
mplayer.release();
super.onBackPressed();
}
public void run() {
int currentPosition = mplayer.getCurrentPosition();
int total = mplayer.getDuration();

while (mplayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mplayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekbar.setProgress(currentPosition);
}
}
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
try {
if (mplayer.isPlaying() || mplayer != null) {
if (fromUser)
mplayer.seekTo(progress);
} else if (mplayer != null) {
Toast.makeText(getApplicationContext(), "Media is not running",
Toast.LENGTH_SHORT).show();
seekbar.setProgress(0);
}
} catch (Exception e) {
Log.e("seek bar", "" + e);
seekbar.setEnabled(false);

}
}

@Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub

}

最佳答案

这是答案,现在可以正常工作了;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.os.Handler;

public class Datadetail1 extends AppCompatActivity implements OnSeekBarChangeListener{

TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;
private Handler mHandler = new Handler();
private Utilities utils;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.data_detail);

Intent i = getIntent();

position = i.getExtras().getInt("position");
itemname = i.getStringArrayExtra("itemname");

txtname = (TextView) findViewById(R.id.textView2);
txtname.setText(itemname[position]);

txtaarti = (TextView) findViewById(R.id.textView3);
txtaarti.setText(Aarti.aartitxt[position]);

seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
seekbar.setEnabled(true);

songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
utils = new Utilities();
mplayer = new MediaPlayer();

buttonPlay = (ImageButton) findViewById(R.id.imageButton);

playsong(position);

buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mplayer.isPlaying()) {
if (mplayer != null) {
mplayer.pause();
buttonPlay.setImageResource(R.mipmap.ic_play);
}
} else {
if (mplayer != null) {
mplayer.start();
buttonPlay.setImageResource(R.mipmap.ic_pause);

}
}

}
});
}

public void playsong(int position) {
switch (position){
case 0:
resID = R.raw.ganesh_aarti;
break;
case 1:
resID = R.raw.durga_aarti;
break;

}
if (mplayer !=null){
mplayer.release();
}
mplayer = MediaPlayer.create(this, resID);
buttonPlay.setImageResource(R.mipmap.ic_play);
seekbar.setProgress(0);
seekbar.setMax(100);
updateProgressBar();

}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
int totalDuration = mplayer.getDuration();
int currentDuration = mplayer.getCurrentPosition();

// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
seekbar.setProgress(progress);

// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};


@Override
public void onBackPressed() {
if (mplayer.isPlaying()) {
mplayer.stop();
}
mHandler.removeCallbacks(mUpdateTimeTask);
mplayer.release();
super.onBackPressed();
}

public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);

}

@Override
public void onStopTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mplayer.getDuration();
int currentPosition = utils.progressToTimer(seekbar.getProgress(), totalDuration);

// forward or backward to certain seconds
mplayer.seekTo(currentPosition);

// update timer progress again
updateProgressBar();

}

关于android - 搜索栏不在媒体播放器中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com