我已经设置了一个复选框,现在如果取消选中,媒体播放器应该立即取消静音,当检查立即静音时,现在问题是当我选中/取消选中复选框时,声音不会立即静音但是最近我重新开始活动… 我怎么解决这个问题?

该计划的主要代码:

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;

Dialog dialog2;

TextView closeButton;

TextView closeButton2;

CheckBox checkBoxmp;

private MediaPlayer mp, mp2;

SharedPreferences mypref;

SharedPreferences.Editor editor;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_quiz);

//Dialog 1

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();

}

});

//end Dialog 1

//Dialog 2

createDialog2();

Button dialogButton2 = (Button) findViewById(R.id.dialogbtn2);

dialogButton2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

dialog2.show();

}

});

closeButton2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

dialog2.dismiss();

}

});

//end Dialog 2

SharedPreferences mypref = getPreferences(MODE_PRIVATE);

final SharedPreferences.Editor editor = mypref.edit();

checkBoxmp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

editor.putBoolean("playSounds", !isChecked);

editor.commit();

if (null != mp && null != mp2) {

if (!isChecked) {

mp.setVolume(1, 1);

mp2.setVolume(1, 1);

} else {

mp.setVolume(0, 0);

mp2.setVolume(0, 0);

}

}

}

});

final boolean playSounds = mypref.getBoolean("playSounds", false);

checkBoxmp.setChecked(!playSounds);

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 choices = new ArrayList<>();

choices.add(mButtonChoice1);

choices.add(mButtonChoice2);

choices.add(mButtonChoice3);

updateQuestion();

//Code of the mediaplayer begins:

for (final Button choice : choices) {

choice.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (choice.getText().equals(mAnswer)) {

try {

mp = new MediaPlayer();

if (playSounds) {

mp.setVolume(1, 1);

} else {

mp.setVolume(0, 0);

}

AssetFileDescriptor afd;

afd = getAssets().openFd("sample.mp3");

mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

mp.prepare();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

mp.release();

}

});

mp.start();

updateScore();

updateQuestion();

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

} else {

try {

mp2 = new MediaPlayer();

if (playSounds) {

mp2.setVolume(1, 1);

} else {

mp2.setVolume(0, 0);

}

AssetFileDescriptor afd;

afd = getAssets().openFd("wrong.mp3");

mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

mp2.prepare();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

mp.release();

}

});

mp2.start();

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);

}

}

});

}

}

//End mediaplayer main code

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);

}

private void createDialog2() {

dialog2 = new Dialog(this);

dialog2.setTitle("Settings");

dialog2.setContentView(R.layout.popup_menu1_2);

closeButton2 = (TextView) dialog2.findViewById(R.id.closeTXT2);

checkBoxmp = (CheckBox) dialog2.findViewById(R.id.ckeckBox);

}

}

java windows静音_java – 媒体播放器得到静音/取消静音太迟了相关推荐

  1. java编写的媒体播放器_BlogJava: 用Java构造自己的媒体播放器

    一.概述 首先我们来看看构造这个媒体播放器要达到什么样的目标,确定了目标也就确定了代码量和程序的复杂程度.本文的媒体播放器要达到如下目标: 媒体播放器是一个菜单驱动的简单AWT应用. 媒体播放器包含一 ...

  2. java实战——简单的媒体播放器

    这个是用jmf来做的,但是由于jmf对视频资源支持的问题所以能用的类型没有那么多,它支持的格式有下面这些. · D indicates the format can be decoded and pr ...

  3. java制作媒体播放器_用Java构造自己的媒体播放器

    一.概述 首先我们来看看构造这个媒体播放器要达到什么样的目标,确定了目标也就确定了代码量和程序的复杂程度.本文的媒体播放器要达到如下目标: 媒体播放器是一个菜单驱动的简单AWT应用. 媒体播放器包含一 ...

  4. java 媒体播放器_Java多媒体播放器(三)

    No.3 vlcj 一.简介 The vlcj project first gives you Java bindings to just about all of the native functi ...

  5. 【翻】【英汉对照】【完整官方参考】Windows媒体播放器11 SDK 播放器对象模型(三)

    用于脚本语言的播放器对象模型 ActiveX 采用拥有编程功能的对象概念.Windows 媒体播放器使用一些对象来划分控件提供的功能. 根对象为 Player 对象,其他对象都通过特定的属性与 Pla ...

  6. Moviepy输出视频MP4文件Windows媒体播放器播放无画面只有声音问题的解决办法

    一.问题 这2天用Moviepy合成了一个视频文件,用windows媒体播放器播放时只有声音没有画面,如图: 而用手机播放器却可以正常播放. 二.问题定位 文件基本信息如下: 再看视频相关信息: 可以 ...

  7. Windows Media Player 网页播放器 参数含义

    原文来源:http://www.blogjava.net/wangxinsh55/archive/2009/05/03/43535.html 常用网页播放器代码 我们在网页上看到的播放器无外乎WMP/ ...

  8. VLC媒体播放器Web插件详细说明

    原文地址:https://wiki.videolan.org/Documentation:WebPlugin/ 简介:使用视频构建网页 该VLC媒体播放器 webplugins是原生浏览器插件,类似于 ...

  9. 设计模式综合-媒体播放器的实现

    1. 媒体播放器的实现 (1)案例背景: Windows Media Player 和 RealPlayer 是两种常用的媒体播放器,它们的 API 结构和调用方法存在差别,现在你的应用程序需要支持这 ...

最新文章

  1. byte数组穿换成pcm格式_Apache Arrow:一种适合异构大数据系统的内存列存数据格式标准...
  2. Redis 概念以及底层数据结构
  3. Tomcat启动报错 Could not reserve enough space for object heap
  4. SAP 将smartforms的报表转成PDF
  5. ORA-600[4194]/[4193]解决
  6. C++ 字符串编程训练2
  7. react http请求_React组件的应用分析
  8. C语言把文件空格删去,关于文件操作,碰到空格就换行
  9. Programmer,Developer,Engineer——软件从业人员的职业规划
  10. S3上备份的json文件转存成parquet文件
  11. 2017百度之星初赛:A-1005. 今夕何夕
  12. Java小程序--多彩时钟表盘的制作
  13. 股票账户各权限开通条件总结【干活总结】
  14. Testlink 使用步骤
  15. 练习篇:完整实践——实现一个简易日记本应用
  16. (超详细、适合新手)QQ三国实现24小时挂机摆摊卡键喊话
  17. HDU 1560 DNA sequence(DNA序列)
  18. 使用conda卸载pytorch_centos下通过conda安装pytorch
  19. 排气控制系统--中英文翻译
  20. iOS-将像素绘制到屏幕上

热门文章

  1. %.nf和%m.nf的区别
  2. word、excel、ppt转pdf
  3. c++ 11标准模板(STL) std::vector (五)
  4. java绝对路径_java获得项目绝对路径
  5. MySQL 之 事务、存储过程、索引
  6. ubuntu18.04 geomagic touch包编译通过后使用
  7. mysql布尔数据类型_MySQL数据类型
  8. IDEA 打包war (备用)
  9. Linux之父:连你自己都懒得解释,那这就是一堆垃圾!
  10. android安装软件,安卓手机软件怎么安装 安卓手机软件安装方法【详解】