2017-08-08 69 views
1

enter image description here时间声音我编程测验有一个问题,3个选择,一个正确的答案。当我按下3按钮(选择)的右边时,它播放的声音很好,但是当我再次点击时没有任何反应。我希望每次单击右键时声音都会自我重复。我有一个名为选择的列表。在那里,我有一个IF查询,每次按下正确的选择时,它应该重复声音。我想inser部分的代码是if(choice.getText()。等于(mAnswer))Repeate每个按钮被按下

感谢您寻找下。 :)

  public class QuizActivity extends AppCompatActivity { 

private ActionBarDrawerToggle mToggle; 

private QuestionLibrary mQuestionLibrary = new QuestionLibrary(); 

private TextView mScoreView; 
private TextView mQuestionView; 
private Button mButtonChoice1; 
private Button mButtonChoice2; 
private Button mButtonChoice3; 
private String mAnswer; 
private int mScore = 0; 
private int mQuestionNumber = 0; 
Dialog dialog; 
TextView closeButton; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample); 


    createDialog(); 
    Button dialogButton = (Button) findViewById(R.id.dialogbtn); 
    dialogButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.show(); 

     } 
    }); 

    closeButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    TextView shareTextView = (TextView) findViewById(R.id.share); 
    shareTextView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(Intent.ACTION_SEND); 
      myIntent.setType("text/plain"); 
      myIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello!"); 
      myIntent.putExtra(Intent.EXTRA_TEXT, "My highscore in Quizzi is very high! I bet you can't beat me except you are cleverer than me. Download the app now! https://play.google.com/store/apps/details?id=amapps.impossiblequiz"); 
      startActivity(Intent.createChooser(myIntent, "Share with:")); 
     } 
    }); 

    mQuestionLibrary.shuffle(); 


    setSupportActionBar((Toolbar) findViewById(R.id.nav_action)); 
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close); 
    mDrawerLayout.addDrawerListener(mToggle); 
    mToggle.syncState(); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Able to see the Navigation Burger "Button" 

    ((NavigationView) findViewById(R.id.nv1)).setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      switch (menuItem.getItemId()) { 
       case R.id.nav_stats: 
        startActivity(new Intent(QuizActivity.this, Menu2.class)); 
        break; 
       case R.id.nav_about: 
        startActivity(new Intent(QuizActivity.this, Menu3.class)); 
        break; 
      } 

      return true; 
     } 
    }); 

    mScoreView = (TextView) findViewById(R.id.score_score); 
    mQuestionView = (TextView) findViewById(R.id.question); 
    mButtonChoice1 = (Button) findViewById(R.id.choice1); 
    mButtonChoice2 = (Button) findViewById(R.id.choice2); 
    mButtonChoice3 = (Button) findViewById(R.id.choice3); 

    final List<Button> choices = new ArrayList<>(); 
    choices.add(mButtonChoice1); 
    choices.add(mButtonChoice2); 
    choices.add(mButtonChoice3); 

    updateQuestion(); 








    for (final Button choice : choices) { 



     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       mp.stop(); 

      } 

     }); 

     choice.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       if (choice.getText().equals(mAnswer)) { 


        try { 
         mp.prepare(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        mp.start(); 






        updateScore(); 
        updateQuestion(); 
        Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 



       } else { 
        Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show(); 

        Intent intent = new Intent(QuizActivity.this, Menu2.class); 
        intent.putExtra("score", mScore); // pass score to Menu2 
        startActivity(intent); 
       } 
      } 
     }); 
    } 

} 

private void updateQuestion() { 
    if (mQuestionNumber < mQuestionLibrary.getLength()) { 
     mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber)); 
     mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber)); 
     mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber)); 
     mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber)); 
     mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber++); 
    } else { 
     Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(QuizActivity.this, Menu2.class); 
     intent.putExtra("score", mScore); 
     startActivity(intent); 
    } 
} 

private void updateScore() { 
    mScoreView.setText(String.valueOf(++mScore)); 

    SharedPreferences mypref = getPreferences(MODE_PRIVATE); 
    int highScore = mypref.getInt("highScore", 0); 

    if (mScore > highScore) { 
     SharedPreferences.Editor editor = mypref.edit(); 
     editor.putInt("highScore", mScore); 
     editor.apply(); 
    } 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); 
} 

private void createDialog() { 
    dialog = new Dialog(this); 
    dialog.setTitle("Tutorial"); 
    dialog.setContentView(R.layout.popup_menu1_1); 
    closeButton = (TextView) dialog.findViewById(R.id.closeTXT); 
} 

}

+1

尝试里面添加onclickListener –

+0

媒体播放器下无法解析方法错误:choice.setOnClickListener(新View.OnClickListener(){ – Alan

+0

什么方法无法解决onclickListener –

回答

1

因此,尝试做到这一点:

mp.setOnCompletionListener(new OnCompletionListener() { 

     @Override 
     public void onCompletion(MediaPlayer mp) { 
      mp.stop(); 

     } 

     }); 

choice.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (choice.getText().equals(mAnswer)) { 
        mp.prepare(); 
        mp.start(); 


        updateScore(); 
        updateQuestion(); 
        Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 



       } else { 
        Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show(); 

        Intent intent = new Intent(QuizActivity.this, Menu2.class); 
        intent.putExtra("score", mScore); // pass score to Menu2 
        startActivity(intent); 
       } 
      } 
     }); 

其他方式

choice.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        if (choice.getText().equals(mAnswer)) { 
         try { 
      mp.reset(); 
      AssetFileDescriptor afd; 
      afd = getAssets().openFd("your_sound.mp3"); 
      mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); 
      mp.prepare(); 
      mp.start(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
         updateScore(); 
         updateQuestion(); 
         Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 



        } else { 
         Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show(); 

         Intent intent = new Intent(QuizActivity.this, Menu2.class); 
         intent.putExtra("score", mScore); // pass score to Menu2 
         startActivity(intent); 
        } 
       } 
      }); 

说明:

Acordingŧ这android developers link你需要按照图。 所以,你需要prepare()start()stop()重放声音。因为您没有prepare()而且没有stop()媒体播放器没有准备好重播。

+0

我会尝试感谢 – Alan

+0

应用强行关闭时,我打一个答案,我更新了我的代码! – Alan

+0

什么是logcat的?@Alan –