我已经实现了mediaplayer
和mediacontroller
,但无法看到播放/暂停等控制器按钮或音频的positionbar
。音乐开始播放,但GUI无法播放出现了。
代码:
package mybitchinapp.cortana.com.musica;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
public class MainActivity extends AppCompatActivity implements MediaPlayerControl {
private MediaPlayer player;
private MediaController controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = MediaPlayer.create(this, R.raw.music);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
// controller = new MediaController(MainActivity.this);
setController();
player.start();
}
private void setController () {
controller = new MediaController(this);
controller.setPrevNextListeners(new View.OnClickListener() {
@Override
public void onClick(View v) {
playNext();
}
}, new View.OnClickListener() {
@Override
public void onClick(View v) {
playPrev();
}
});
controller.setMediaPlayer(this);
controller.setAnchorView(findViewById (R.id.main)); // This is the main activity
controller.setEnabled(true);
controller.show (10000);
}
public void playPrev () {
}
public void playNext () {
}
@Override
protected void onPause() {
super.onPause();
player.pause();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
controller.show ();
return false;
}
@Override
public void start() {
player.start();
}
@Override
public void pause() {
player.pause();
}
@Override
public int getDuration() {
return 0;
}
@Override
public int getCurrentPosition() {
return 0;
}
@Override
public void seekTo(int pos) {
}
@Override
public boolean isPlaying() {
return false;
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return false;
}
@Override
public boolean canSeekBackward() {
return false;
}
@Override
public boolean canSeekForward() {
return false;
}
@Override
public int getAudioSessionId() {
return 0;
}
}
我是初学者因此可能是实施错误,所以请提供修改后的代码。
答案 0 :(得分:0)
我还没能真正实现MediaController(它仍然没有显示)。所以相反,作为权宜之计,我使用了按钮进行播放/暂停,前进/后退和前进/后退,而不是MediaController,而对于progressSlider,可以使用SeekBar。
http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/