如何使用循环播放媒体文件有一些延迟?

时间:2013-03-20 06:33:43

标签: android multithreading media-player

我正在尝试使用循环播放原始媒体文件。我编写了一个同步方法来播放媒体文件。这适用于模拟器,但不适用于真实设备。

我为问题创建了一个简单的bean,需要播放声音。

public class Question {
private String mQuestionString;
private int mSoundFile;
private int mAnswer;
private int mQuestionNo;

public Question() {

    mQuestionNo = 0;
    mQuestionString = "";
    mSoundFile = 0;
    mAnswer = 0;
}

public Question(int mQuestionNo, String mQuestionString, int mSoundFile,
        int mAnswer) {
    super();
    this.mQuestionNo = mQuestionNo;
    this.mQuestionString = mQuestionString;
    this.mSoundFile = mSoundFile;
    this.mAnswer = mAnswer;
}

public String getQuestionString() {
    return mQuestionString;
}

public void setQuestionString(String mQuestionString) {
    this.mQuestionString = mQuestionString;
}

public int getSoundFile() {
    return mSoundFile;
}

public void setSoundFile(int mSoundFile) {
    this.mSoundFile = mSoundFile;
}

public int getAnswer() {
    return mAnswer;
}

public void setAnswer(int mAnswer) {
    this.mAnswer = mAnswer;
}

public int getQuestionNo() {
    return mQuestionNo;
}

public void setQuestionNo(int mQuestionNo) {
    this.mQuestionNo = mQuestionNo;
}

@Override
public String toString() {
    return "Question " + mQuestionNo + ". " + mQuestionString;
}

}

这是我在活动中的代码

public class LQActivity extends Activity{

    private Vector<Question> mQuestions = null;
private TextView tvQuestion = null;
private Button btnSubmit = null;
private Button btnNext = null;
    ...

  private void prepareQuestions() {
    // TODO Auto-generated method stub
    mQuestions = new Vector<Question>();
    mQuestions.add(new Question(1, "Count the number of the Tabala played",
            R.raw.q1, 5));
    mQuestions.add(new Question(2,
            "Count the number of the persons visited temple", R.raw.q2, 3));
    mQuestions
            .add(new Question(3, "Count the number of frogs", R.raw.q3, 6));
    mQuestions.add(new Question(4,
            "Count the number of the cuckoo on the tree", R.raw.q4, 8));
    mQuestions.add(new Question(5, "Count the number of the flute played",
            R.raw.q5, 7));

}

private synchronized void playQuestion(Question question) {


    mMediaPlayer = MediaPlayer.create(this, question.getSoundFile());

    mMediaPlayer.start();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
        }
    }
            mMediaPlayer.release();
        }

}

问题是mMediaPlyer.create()返回null

我没有得到确切的问题。感谢您的帮助。提前谢谢......

1 个答案:

答案 0 :(得分:0)

mMediaPlyer.create() is returning null

原因是由于损坏的mp3文件或使用不受支持的文件,请检查链接android支持media format