使用ExoPlayer时声音播放有问题。即,通过快速切换"停止 - 启动"异常发生
java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.android.exoplayer2.source.MediaSource$Listener.onSourceInfoRefreshed(com.google.android.exoplayer2.Timeline, java.lang.Object)' on a null object reference.
这是我的代码。我究竟做错了什么?对不起我的英语不好。我非常感谢大家的帮助!
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
startPosition = getArguments().getInt("startPosition");
Log.i("igor", "start position= " + startPosition);
sounds = (ArrayList<SoundModel>) getArguments().getSerializable("sounds");
utils = new Utils();
View view = inflater.inflate(R.layout.play_layout, container, false);
initializePlayer();
prepareExoPlayer(startPosition);
initViews(view);
settingsToolbar(view);
return view;
}
private void settingsToolbar(View view) {
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private void initViews(View view) {
mPickerTags = (ColectionTags) view.findViewById(R.id.colection_tags);
imNext = (ImageView) view.findViewById(R.id.imNext);
imPlay = (ImageView) view.findViewById(R.id.imPlay);
imPrev = (ImageView) view.findViewById(R.id.imPrev);
soundSeekBar = (SeekBar) view.findViewById(R.id.soundProgressBar);
songCurrentDurationLabel = (TextView) view.findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) view.findViewById(R.id.songTotalDurationLabel);
tvCountSounds = (TextView) view.findViewById(R.id.tvCountSounds);
settingsViews();
}
private void settingsViews() {
soundSeekBar.setOnSeekBarChangeListener(this);
soundSeekBar.setProgress(0);
soundSeekBar.setMax(100);
soundSeekBar.setClickable(true);
soundSeekBar.setFocusable(true);
soundSeekBar.setEnabled(true);
imNext.setOnClickListener(this);
imPlay.setOnClickListener(this);
imPrev.setOnClickListener(this);
tvCountSounds.setText((startPosition+1)+"/"+sounds.size());
tagsSet();
}
private void tagsSet() {
mPickerTags.clearItems();
mPickerTags.setItems(generateItems());
mPickerTags.drawItemView();
mPickerTags.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onClick(Item item, int position) {
showFragment(getActivity(), SearchListFragment.newInstance(item.text));
}
});
}
@Override
public void onStart() {
super.onStart();
}
private void initializePlayer() {
userAgent = Util.getUserAgent(getActivity(), "ExoPlayerDemo");
mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(getActivity(), trackSelector, loadControl);
//////
bwMeter = new DefaultBandwidthMeter();
dataSourceFactory = new DefaultDataSourceFactory(getActivity(),
Util.getUserAgent(getActivity(), "yourApplicationName"), bwMeter);
extractorsFactory = new DefaultExtractorsFactory();
}
private void prepareExoPlayer(int position) {
uri = Uri.parse(sounds.get(position).getSoundLink());
MediaSource mediaSource = new ExtractorMediaSource(uri,
dataSourceFactory, extractorsFactory, mainHandler, null);
try {
mediaSource.maybeThrowSourceInfoRefreshError();
} catch (IOException e) {
e.printStackTrace();
Log.i("igor","exept= "+e.getMessage());
}
player.prepare(mediaSource);
player.addListener(new ExoPlayer.EventListener() {
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch (playbackState){
case ExoPlayer.STATE_ENDED:
// onNextPlaySound();
break;
case ExoPlayer.STATE_READY:
Log.i("igor","is read");
break;
case ExoPlayer.STATE_IDLE:
break;
case ExoPlayer.STATE_BUFFERING:
break;
}
}
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
//Toast.makeText(getActivity(),"Play error",Toast.LENGTH_SHORT).show();
}
@Override
public void onPositionDiscontinuity() {
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
player.setPlayWhenReady(false);
player.release();
}
@Override
public void onLoadError(IOException error) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imPlay:
if(isPlaying==false) {
prepareExoPlayer(startPosition);
player.setPlayWhenReady(true);
player.seekTo(currentPos);
soundSeekBar.setProgress(currentProgress);
isPlaying=player.getPlayWhenReady();
imPlay.setImageDrawable(getResources().getDrawable(R.drawable.pause));
updateProgressBar(currentProgress);
}
else {
currentProgress= soundSeekBar.getProgress();
currentPos = player.getCurrentPosition();
player.setPlayWhenReady(false);
isPlaying=player.getPlayWhenReady();
imPlay.setImageDrawable(getResources().getDrawable(R.drawable.play));
}
break;
case R.id.imNext:
Log.i("igor","Ready player= "+player.getPlayWhenReady());
onNextPlaySound();
break;
case R.id.imPrev:
Log.i("igor","Ready player= "+player.getPlayWhenReady());
onPrevPlaySound();
break;
}
}
private void onPrevPlaySound() {
if (startPosition > 0) {
startPosition--;
prepareExoPlayer(startPosition);
tagsSet();
tvCountSounds.setText((startPosition+1)+"/"+sounds.size());
}
player.setPlayWhenReady(true);
}
private void onNextPlaySound() {
if (startPosition < sounds.size()-1) {
startPosition++;
prepareExoPlayer(startPosition);
tagsSet();
tvCountSounds.setText((startPosition+1)+"/"+sounds.size());
}
player.setPlayWhenReady(true);
}
public void updateProgressBar(int currentProgress) {
mHandler.postDelayed(mUpdateTimeTask, 10);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long currentDuration=currentProgress;
if(null!=player) {
currentDuration = player.getCurrentPosition();
if(player.getDuration()>0) {
totalDuration = player.getDuration();
}
//player.getDuration()>0?totalDuration=player.getDuration():;
}
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
soundSeekBar.setProgress(progress);
mHandler.postDelayed(this, 10);
}
};
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int currentPosition = utils.progressToTimer(seekBar.getProgress(), (int) totalDuration);
//forward or backward to certain seconds
player.seekTo(currentPosition);
/// update timer progress again
updateProgressBar(currentPosition);
}
private List<Item> generateItems() {
SoundModel currentSound = sounds.get(startPosition);
List<Item> items = new ArrayList<>();
for(int i = 0; i<currentSound.getTags().size(); i++){
items.add(new Item(currentSound.getId().toString(),currentSound.getTags().get(i)));
}
return items;
}
答案 0 :(得分:1)
为什么每次要暂停/取消暂停播放时都会重新创建MediaSource
?如果您想要连续播放声音列表,请使用ConcatenatingMediaSource
。此外,每次单击播放按钮,您都会通过addListener
向播放器添加新的听众,我认为这可能不是必需的。
我认为抛出异常是因为您在每次点击时都使用新的媒体资源,如果您想要多个声音效果,请尝试使用ConcatenatingMediaSource
,否则您应该不会每次都重新创建它。