我们使用Google sample project进行以下更改。与我们在项目中使用的相同,在三星旗舰设备和Google设备中,媒体播放器错误侦听器出现错误(-38,0)。 (在注释8-8.1和像素2xl-9.0中进行了测试)
在这里,onError
侦听器首先调用,然后调用onPrepared
侦听器。
预期的解决方案:播放音频没有任何错误。
package com.example.android.mediaplayersample;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Exposes the functionality of the {@link MediaPlayer} and implements the {@link PlayerAdapter}
* so that {@link MainActivity} can control music playback.
*/
public final class MediaPlayerHolder implements PlayerAdapter {
public static final int PLAYBACK_POSITION_REFRESH_INTERVAL_MS = 1000;
private final Context mContext;
private MediaPlayer mMediaPlayer;
private int mResourceId;
private PlaybackInfoListener mPlaybackInfoListener;
private ScheduledExecutorService mExecutor;
private Runnable mSeekbarPositionUpdateTask;
public MediaPlayerHolder(Context context) {
mContext = context.getApplicationContext();
}
/**
* Once the {@link MediaPlayer} is released, it can't be used again, and another one has to be
* created. In the onStop() method of the {@link MainActivity} the {@link MediaPlayer} is
* released. Then in the onStart() of the {@link MainActivity} a new {@link MediaPlayer}
* object has to be created. That's why this method is private, and called by load(int) and
* not the constructor.
*/
private void initializeMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
logToUI("OnPrepared()");
mediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopUpdatingCallbackWithPosition(true);
logToUI("MediaPlayer playback completed");
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.COMPLETED);
mPlaybackInfoListener.onPlaybackCompleted();
}
}
});
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
logToUI("OnError()" + i + " " + i1);
return true;
}
});
logToUI("mMediaPlayer = new MediaPlayer()");
}
}
public void setPlaybackInfoListener(PlaybackInfoListener listener) {
mPlaybackInfoListener = listener;
}
// Implements PlaybackControl.
@Override
public void loadMedia(int resourceId) {
mResourceId = resourceId;
mMediaPlayer = new MediaPlayer();
try {
logToUI("load() {1. setDataSource}");
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource("http://radio-electron.ru:8000/96");
} catch (Exception e) {
logToUI(e.toString());
}
initializeMediaPlayer();
try {
logToUI("load() {2. prepare}");
mMediaPlayer.prepareAsync();
} catch (Exception e) {
logToUI(e.toString());
}
initializeProgressCallback();
logToUI("initializeProgressCallback()");
}
@Override
public void release() {
if (mMediaPlayer != null) {
logToUI("release() and mMediaPlayer = null");
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public boolean isPlaying() {
if (mMediaPlayer != null) {
return mMediaPlayer.isPlaying();
}
return false;
}
@Override
public void play() {
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
logToUI(String.format("playbackStart() %s",
mContext.getResources().getResourceEntryName(mResourceId)));
mMediaPlayer.start();
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.PLAYING);
}
startUpdatingCallbackWithPosition();
}
}
@Override
public void reset() {
if (mMediaPlayer != null) {
logToUI("playbackReset()");
mMediaPlayer.reset();
loadMedia(mResourceId);
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.RESET);
}
stopUpdatingCallbackWithPosition(true);
}
}
@Override
public void pause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.PAUSED);
}
logToUI("playbackPause()");
}
}
@Override
public void seekTo(int position) {
if (mMediaPlayer != null) {
logToUI(String.format("seekTo() %d ms", position));
mMediaPlayer.seekTo(position);
}
}
/**
* Syncs the mMediaPlayer position with mPlaybackProgressCallback via recurring task.
*/
private void startUpdatingCallbackWithPosition() {
if (mExecutor == null) {
mExecutor = Executors.newSingleThreadScheduledExecutor();
}
if (mSeekbarPositionUpdateTask == null) {
mSeekbarPositionUpdateTask = new Runnable() {
@Override
public void run() {
updateProgressCallbackTask();
}
};
}
mExecutor.scheduleAtFixedRate(
mSeekbarPositionUpdateTask,
0,
PLAYBACK_POSITION_REFRESH_INTERVAL_MS,
TimeUnit.MILLISECONDS
);
}
// Reports media playback position to mPlaybackProgressCallback.
private void stopUpdatingCallbackWithPosition(boolean resetUIPlaybackPosition) {
if (mExecutor != null) {
mExecutor.shutdownNow();
mExecutor = null;
mSeekbarPositionUpdateTask = null;
if (resetUIPlaybackPosition && mPlaybackInfoListener != null) {
mPlaybackInfoListener.onPositionChanged(0);
}
}
}
private void updateProgressCallbackTask() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
int currentPosition = mMediaPlayer.getCurrentPosition();
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onPositionChanged(currentPosition);
}
}
}
@Override
public void initializeProgressCallback() {
final int duration = mMediaPlayer.getDuration();
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onDurationChanged(duration);
mPlaybackInfoListener.onPositionChanged(0);
logToUI(String.format("firing setPlaybackDuration(%d sec)",
TimeUnit.MILLISECONDS.toSeconds(duration)));
logToUI("firing setPlaybackPosition(0)");
}
}
private void logToUI(String message) {
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onLogUpdated(message);
}
}
}
答案 0 :(得分:0)
我们在tsconfig.json
中使用getDuration()
并创建了问题,因此现在我们在onBufferingUpdate()
中初始化onBufferingUpdate()
监听器,并在尝试播放下一个监听器时将其删除音频。