MediaPlayer未声明为私有时不起作用

时间:2018-07-21 17:33:27

标签: java android android-mediaplayer

重新发布问题:由于我15天没有得到答案,而且我没有足够的赏金可以搁置。

我是android编程的初学者,正在尝试创建一个播放特定声音的应用。单击ListView项时,但是当我在onItemClick方法中声明MediaPlayer对象(如下所示)时,应用崩溃

  phrasesActivityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Word currentWord = phrases.get(position);
            MediaPlayer mediaPlayer = MediaPlayer.create(PhrasesActivity.this, currentWord.getAudioResourceId());
            mediaPlayer.start();
        }
    });

其中短语ActivityListView是ListView的名称

而当我将MediaPlayer对象声明为私有对象时,应用程序将运行(如下所示)

private MediaPlayer mMediaPlayer;
.
.
 phrasesActivityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Word currentWord = phrases.get(position);
            mMediaPlayer = MediaPlayer.create(PhrasesActivity.this, currentWord.getAudioResourceId());
            mMediaPlayer.start();
        }
    });

为什么?而且我还在下面包括了词类

package com.example.android.miwok;

public class Word {
//Default translation of the word
private String mDefaultTranslation;
//Miwok translation of the word
private String mMiwokTranslation;
//Resource id of the image to be shown on the screen.It is set to minus one so that it can be verified that whether the resource id is associated with any ImageView.
private int mImageResourceId = -1;
//Resource id of the audio file that is going to be played when the user click on the ListView item.
private int mAudioResourceId;

//Constructor to intialise the values of text in the textViewx
public Word(String defaultTranslation, String miwokTranslation, int audioResourceId) {
    mDefaultTranslation = defaultTranslation;
    mMiwokTranslation = miwokTranslation;
    mAudioResourceId = audioResourceId;
}

//Constructor to intialise the values of text and the resource for the TextView and the ImageView.
public Word(String defaultTranslation, String miwokTranslation, int imageResourceId, int audioResourceId) {
    mDefaultTranslation = defaultTranslation;
    mMiwokTranslation = miwokTranslation;
    mImageResourceId = imageResourceId;
    mImageResourceId = audioResourceId;
}

//Returns the defaultTranslation of the word
public String getDefaultTranslation() {
    return mDefaultTranslation;
}

//Returns the Miwok translation of the word.
public String getMiwokTranslation() {
    return mMiwokTranslation;
}

//Returns the image resource id of the image.
public int getImageResourceId() {
    return mImageResourceId;
}

//Checks whether the resource view has been intialised when creating a ImageView.
public boolean hasImage() {
    return mImageResourceId != -1;
}
//Returns the audio resource id of the audio.

public int getAudioResourceId() {
    return mAudioResourceId;
}

}

2 个答案:

答案 0 :(得分:1)

在您的第一个代码段中,mediaPlayer是局部变量。 onItemClick()返回后,它就会超出范围,并且有资格进行垃圾回收。由于您希望该对象的生存期更长,因此需要将其保留在其他地方,例如该代码所在的活动或片段中。

答案 1 :(得分:0)

保留列表视图phrasesActivityListView对象的方式,与声明媒体播放器对象的方式相同。这样一来,您可以玩更长久的游戏。 更多信息您还应该阅读媒体播放器的lifecycle。它将清楚表明您应该如何处理媒体播放器对象,例如释放对象,准备和开始的顺序。