gpt4 book ai didi

java - 为什么通过 MediaController (Android) 播放声音时 SeekBar 不移动?

转载 作者:行者123 更新时间:2023-12-02 04:22:02 25 4
gpt4 key购买 nike

我正在尝试使用 Android 应用程序中的媒体 Controller 播放音频文件。播放音频时,搜索栏不会移动。我查看了媒体 Controller 功能。但我找不到更新搜索栏的功能。请指教。

public class Show_subject_Activity extends Activity
implements MediaController.MediaPlayerControl, MediaPlayer.OnBufferingUpdateListener {
private RatingBar box_litner;
private MediaController mController;
private MediaPlayer mPlayer;
private Cursor cur;
int bufferPercent = 0;
DB_Nabege_helper db_nabege = new DB_Nabege_helper(this);
private String voicefileName;

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

box_litner = (RatingBar) findViewById(R.id.ratingBar1);
mController = (MediaController) findViewById(R.id.mediaController_voice);
mController.setAnchorView(null);
db_nabege.open();
cur = db_nabege.getSubject(Intent_values.id_subject);
voicefileName = getVoiceFileName();
db_nabege.close();
}

@Override
public void onResume() {
super.onResume();
mPlayer = new MediaPlayer();
// Set the audio data source
try {
mPlayer.setDataSource(this, getUriVoice());
mPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
// Set an image for the album cover
// coverImage.setImageResource(R.drawable.icon);

mController.setMediaPlayer(this);
mController.setEnabled(true);
}

private Uri getUriVoice() {
File voiceFile = null;
try {
voiceFile = new File(Environment.getExternalStorageDirectory().getPath() + "/nabege" + File.separator
+ "audio" + File.separator + voicefileName);
} catch (Exception e) {
Log.d("log", e.toString());
}
Uri voiceUri = Uri.fromFile(voiceFile);
return voiceUri;
}

@Override
public void onPause() {
super.onPause();
mPlayer.release();
mPlayer = null;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
mController.show();
return super.onTouchEvent(event);
}

// MediaPlayerControl Methods
@Override
public int getBufferPercentage() {
return bufferPercent;
}

@Override
public int getCurrentPosition() {
return mPlayer.getCurrentPosition();
}

@Override
public int getDuration() {
return mPlayer.getDuration();
}

@Override
public boolean isPlaying() {
return mPlayer.isPlaying();
}

@Override
public void pause() {
mPlayer.pause();
}

@Override
public void seekTo(int pos) {
mPlayer.seekTo(pos);
}

@Override
public void start() {
mPlayer.start();
}

// BufferUpdateListener Methods
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
bufferPercent = percent;
}

public boolean canPause() {
return true;
}

public boolean canSeekBackward() {
return true;
}

public boolean canSeekForward() {
return true;
}

@Override
public int getAudioSessionId() {
return 0;
}
}

最佳答案

您可以自己更新搜索栏,例如:

final Handler mHandler = new Handler();
final Runnable mUpdateSeekbar = new Runnable() {
public void run() {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
mSeekBar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(this, 1000);
}
};

onResume中发布可运行程序,在onPause中发布mHandler.removeCallbacks(mUpdateSeekbar)

关于java - 为什么通过 MediaController (Android) 播放声音时 SeekBar 不移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665277/

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