尝试使用AudioManager类。但该应用程序正在给出运行时错误:尝试调用虚拟方法' int android.media.AudioManager.requestAudioFocus(android.media.AudioManager $ OnAudioFocusChangeListener,int,int)'在空对象引用上
public class NumbersActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private AudioManager audioManager;
private AudioManager.OnAudioFocusChangeListener audioFocusChangeListener=new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if(focusChange==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT||focusChange==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
{
//now we need to call pause because listener detected that some else app wants audiofocus for short time
mediaPlayer.pause();
//rewinding our audio back to 0 i.e start so when we get audio focus back it resumes from start
mediaPlayer.seekTo(0);
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
mediaPlayer.start();
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// The AUDIOFOCUS_LOSS case means we've lost audio focus completely
//so now Stop playback and clean up resources
releasePlayer();
}
}
};
//this is a listener triggering when audio is completed playing in our app
private MediaPlayer.OnCompletionListener songCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releasePlayer();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_name);
final ArrayList<Word> numbers = new ArrayList<Word>();
numbers.add(..CODE TO ADD DATA HERE..);
//created custom arrya adapter
WordAdapter itemsAdapter = new WordAdapter(this, numbers);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
releasePlayer();
Word word = numbers.get(i);
// now we are going to gain focus on audio
int getAudioFocus=audioManager.requestAudioFocus(audioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if(getAudioFocus==AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
mediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceID());
mediaPlayer.start();
//called when audio is finished playing
mediaPlayer.setOnCompletionListener(songCompletionListener);
}
}
});
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
//all the methods that are called are below here
//to check if mediaplayer is null so that we can play new audio and it not then make it null
private void releasePlayer() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
// Regardless of whether or not we were granted audio focus, abandon it. This also
// unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
audioManager.abandonAudioFocus(audioFocusChangeListener);
}
}
}
这里我找不到null对象引用。我是Android的初学者,所以请帮助我理解这个问题。我已尽力理解并发现错误,但我无法做到。