我是android的新手,正在学习阶段,我在有声书应用程序上工作,在这里我引用DMAudioStreamer库在我的应用程序中播放音频,它使用媒体播放器,并且我认为媒体播放器流很慢,因为seekbar无法正常工作正确,下一个上一个也不在我的应用程序中工作,我从服务器上传了音频,所以我用exoplayer替换了媒体播放器,现在Seekbar和上一个下一个正常工作,但播放和暂停停止工作,seekbar的时间也没有更新。实际上,我没有到达我所不能替代的球员。有人帮助我,我同时附加了Code Media Player和Exoplayer。
代码使用Media Player
test.mylaravelapp.example
我用exoplayer替换媒体播放器的代码。
public class AudioPlaybackListener implements PlaybackListener, AudioManager.OnAudioFocusChangeListener,
OnCompletionListener, MediaPlayer.OnErrorListener, OnPreparedListener, OnSeekCompleteListener {
private static final String TAG = Logger.makeLogTag(AudioPlaybackListener.class);
public static final float VOLUME_DUCK = 0.2f;
public static final float VOLUME_NORMAL = 1.0f;
private static final int AUDIO_NO_FOCUS_NO_DUCK = 0;
private static final int AUDIO_NO_FOCUS_CAN_DUCK = 1;
private static final int AUDIO_FOCUSED = 2;
private final Context mContext;
private final WifiManager.WifiLock mWifiLock;
private int mState;
private Callback mCallback;
private boolean mPlayOnFocusGain;
private volatile int mCurrentPosition;
private volatile String mCurrentMediaId;
private int mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
private final AudioManager mAudioManager;
private MediaPlayer mMediaPlayer;
public AudioPlaybackListener(Context context) {
this.mContext = context;
this.mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
this.mWifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "dmAudioStreaming_Lock");
this.mState = PlaybackStateCompat.STATE_NONE;
}
private final IntentFilter mAudioNoisyIntentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private final BroadcastReceiver mAudioNoisyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
Logger.d(TAG, "Headphones disconnected.");
if (isPlaying()) {
//TODO PAUSE THE PLAYING SONG
}
}
}
};
@Override
public void start() {
}
@Override
public void stop(boolean notifyListeners) {
mState = PlaybackStateCompat.STATE_STOPPED;
if (notifyListeners && mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
mCurrentPosition = getCurrentStreamPosition();
// Give up Audio focus
giveUpAudioFocus();
unregisterAudioNoisyReceiver();
// Relax all resources
relaxResources(true);
}
@Override
public void setState(int state) {
this.mState = state;
}
@Override
public int getState() {
return mState;
}
@Override
public boolean isConnected() {
return true;
}
@Override
public boolean isPlaying() {
return mPlayOnFocusGain || (mMediaPlayer != null && mMediaPlayer.isPlaying());
}
@Override
public int getCurrentStreamPosition() {
return mMediaPlayer != null ? mMediaPlayer.getCurrentPosition() : mCurrentPosition;
}
@Override
public void updateLastKnownStreamPosition() {
if (mMediaPlayer != null) {
mCurrentPosition = mMediaPlayer.getCurrentPosition();
}
}
@Override
public void play(MediaMetaData item) {
mPlayOnFocusGain = true;
tryToGetAudioFocus();
registerAudioNoisyReceiver();
String mediaId = item.getMediaId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentPosition = 0;
mCurrentMediaId = mediaId;
}
if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
configMediaPlayerState();
} else {
mState = PlaybackStateCompat.STATE_STOPPED;
relaxResources(false); // release everything except MediaPlayer
String source = item.getMediaUrl();
if (source != null) {
source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
}
try {
createMediaPlayerIfNeeded();
mState = PlaybackStateCompat.STATE_BUFFERING;
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(source);
mMediaPlayer.prepareAsync();
mWifiLock.acquire();
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
} catch (IOException ex) {
Logger.e(TAG, ex, "Exception playing song");
if (mCallback != null) {
mCallback.onError(ex.getMessage());
}
}
}
}
@Override
public void pause() {
try {
if (mState == PlaybackStateCompat.STATE_PLAYING) {
// Pause media player and cancel the 'foreground service' state.
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mCurrentPosition = mMediaPlayer.getCurrentPosition();
}
// while paused, retain the MediaPlayer but give up audio focus
relaxResources(false);
}
mState = PlaybackStateCompat.STATE_PAUSED;
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
unregisterAudioNoisyReceiver();
} catch (IllegalStateException ex) {
Logger.e(TAG, ex, "Exception pause IllegalStateException");
ex.printStackTrace();
}
}
@Override
public void seekTo(int position) {
Logger.d(TAG, "seekTo called with ", position);
if (mMediaPlayer == null) {
// If we do not have a current media player, simply update the current position
mCurrentPosition = position;
} else {
if (isPlaying()) {
mState = PlaybackStateCompat.STATE_BUFFERING;
}
registerAudioNoisyReceiver();
mMediaPlayer.seekTo(position);
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
}
}
@Override
public void setCallback(Callback callback) {
this.mCallback = callback;
}
@Override
public void setCurrentStreamPosition(int pos) {
this.mCurrentPosition = pos;
}
@Override
public void setCurrentMediaId(String mediaId) {
this.mCurrentMediaId = mediaId;
}
@Override
public String getCurrentMediaId() {
return mCurrentMediaId;
}
private void tryToGetAudioFocus() {
Logger.d(TAG, "tryToGetAudioFocus");
int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mAudioFocus = AUDIO_FOCUSED;
} else {
mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
}
}
private void giveUpAudioFocus() {
Logger.d(TAG, "giveUpAudioFocus");
if (mAudioManager.abandonAudioFocus(this) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
}
}
private void configMediaPlayerState() {
Logger.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
// If we don't have audio focus and can't duck, we have to pause,
if (mState == PlaybackStateCompat.STATE_PLAYING) {
mPlayOnFocusGain = false;
pause();
}
} else { // we have audio focus:
registerAudioNoisyReceiver();
if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
} else {
if (mMediaPlayer != null) {
mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
} // else do something for remote client.
}
// If we were playing when we lost focus, we need to resume playing.
if (mPlayOnFocusGain) {
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
Logger.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ",
mCurrentPosition);
if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
} else {
mMediaPlayer.seekTo(mCurrentPosition);
mState = PlaybackStateCompat.STATE_BUFFERING;
}
}
mPlayOnFocusGain = false;
}
}
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
}
@Override
public void onAudioFocusChange(int focusChange) {
Logger.d(TAG, "onAudioFocusChange. focusChange=", focusChange);
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// We have gained focus:
mAudioFocus = AUDIO_FOCUSED;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;
if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
mPlayOnFocusGain = true;
}
} else {
Logger.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
}
configMediaPlayerState();
}
@Override
public void onSeekComplete(MediaPlayer mp) {
Logger.d(TAG, "onSeekComplete from MediaPlayer:", mp.getCurrentPosition());
mCurrentPosition = mp.getCurrentPosition();
if (mState == PlaybackStateCompat.STATE_BUFFERING) {
registerAudioNoisyReceiver();
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
}
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
}
@Override
public void onCompletion(MediaPlayer player) {
Logger.d(TAG, "onCompletion from MediaPlayer");
// The media player finished playing the current song, so we go ahead
// and start the next.
if (mCallback != null) {
mCallback.onCompletion();
}
}
@Override
public void onPrepared(MediaPlayer player) {
Logger.d(TAG, "onPrepared from MediaPlayer");
configMediaPlayerState();
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Logger.e(TAG, "Media player error: what=" + what + ", extra=" + extra);
if (mCallback != null) {
mCallback.onError("MediaPlayer error " + what + " (" + extra + ")");
}
return true; // true indicates we handled the error
}
private void createMediaPlayerIfNeeded() {
Logger.d(TAG, "createMediaPlayerIfNeeded. needed? ", (mMediaPlayer == null));
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setWakeMode(mContext.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnSeekCompleteListener(this);
} else {
mMediaPlayer.reset();
}
}
private void relaxResources(boolean releaseMediaPlayer) {
Logger.d(TAG, "relaxResources. releaseMediaPlayer=", releaseMediaPlayer);
// stop and release the Media Player, if it's available
if (releaseMediaPlayer && mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
mCurrentMediaId = null;
mCurrentPosition = 0;
}
// we can also release the Wifi lock, if we're holding it
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}
private void registerAudioNoisyReceiver() {
try {
if (mAudioNoisyReceiver!=null) {
mContext.registerReceiver(mAudioNoisyReceiver, mAudioNoisyIntentFilter);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void unregisterAudioNoisyReceiver() {
try {
if (mAudioNoisyReceiver!=null) {
mContext.unregisterReceiver(mAudioNoisyReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}