我录制了音频,并通过媒体播放器播放。录音,播放工作正常。我在播放录制的声音时使用了搜索栏。我使用以下编码来播放存储在SD卡中的内容。它工作正常.. mFileName是sdcard中的音频文件。 在播放时,搜索栏不会移动。任何建议......
private void startPlaying()
{
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
mSeekBar.setMax(mPlayer.getDuration());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
我用过
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
if(fromUser){
mPlayer.seekTo(progress);
mSeekBar.setProgress(progress);
}
}
});
Thread currentThread = new Thread((Runnable) this);
currentThread.start();
public void run() {
// TODO Auto-generated method stub
try {
while(mPlayer != null){
int currentPosition = mPlayer.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
}
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Handler threadHandler = new Handler(){
public void handleMessage(Message msg){
mSeekBar.setProgress(msg.what);
}
};
仅当用户移动时,进度条才会移动。它会自动移动。
答案 0 :(得分:3)
搜索栏不会神奇地更新自己以显示MediaPlayer的当前位置。这是一个链接,显示如何使用轮询线程来检查MediaPlayer的位置,并相应地更新搜索栏:
http://united-coders.com/nico-heid/an-android-seekbar-for-your-mediaplayer