一段时间后声音停止
Posted
技术标签:
【中文标题】一段时间后声音停止【英文标题】:Sound is stopping after a period of time 【发布时间】:2017-08-29 20:08:14 【问题描述】:每次单击按钮时都会调用媒体播放器的代码(在注释下开始://媒体播放器的代码开始)。一段时间后,当我单击按钮时,不再播放声音。
就像:我点击了 10 次,当我再次点击时它会返回声音,它停止并且不再工作了。感谢您的关注,如果有任何解决方案评论在下面! :D
Logcat:
E/AudioFlinger:没有更多可用的曲目名称
E/AudioFlinger: createTrack_l() initCheck failed -12;没有控制块?
E/AudioTrack:AudioFlinger 无法创建轨道,状态:-12
E/Audiosink:无法创建音轨
E/ExtendedNuPlayerDecoder:打开音频接收器时出错。可能是致命的!!!
**主要活动的代码'*:
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 (!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);
if(playSounds)
mp.setVolume(1,1);
mp2.setVolume(1,1);
else
mp.setVolume(0,0);
mp2.setVolume(0,0);
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();
//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);
mp.reset();
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 unused)
mp.release();
mp = null;
);
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
mp.setVolume(0, 0);
mp2.reset();
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 unused)
mp2.release();
mp2 = null;
);
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);
【问题讨论】:
您播放的样本有多长?如果它们不长,最好使用SoundPool
。
他们就像 1 秒长,soundpool 更好吗?我该如何设置 soundpool?
一般来说,是的SoundPool
更适合短音频片段。如果您搜索它们,应该有很多示例,并且我原始评论中的链接是 API 文档。
出于好奇,您播放声音的速度有多快?您是快速点击按钮还是每隔几秒点击一次?
很快,非常快
【参考方案1】:
你没有释放MediaPlayer
s。这可能是这个问题背后的原因。在不触及大部分逻辑的情况下,一种方法是:
使mp
和mp2
、private
成为QuizActivity
的成员。
public class QuizActivity extends AppCompatActivity
...
private MediaPlayer mp, mp2;
...
在需要时创建MediaPlayer
,并在播放完成时释放它。
mp = new MediaPlayer();
if (playSounds)
mp.setVolume(1, 1);
else
mp.setVolume(0, 0);
AssetFileDescriptor afd;
...
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
@Override
public void onCompletion(MediaPlayer unused)
mp.release();
mp = null;
);
mp.start();
在其他地方,您必须执行如下所示的null
检查,然后才能访问mp
和mp2
以避免NPE。
if (null != mp && null != mp2)
if (!isChecked)
mp.setVolume(1, 1);
mp2.setVolume(1, 1);
else
mp.setVolume(0, 0);
mp2.setVolume(0, 0);
其他方法是仅在创建媒体播放器后添加点击侦听器。
This 链接更清楚地说明了MediaPlayer
的发布。
完整的源代码如下所示。
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<Button> 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);
【讨论】:
不管怎样,reset
和 release
在本机端不一定是同步的。如果足够快的话,仍然可以通过释放和创建新的MediaPlayer
实例来耗尽资源。我很想知道这是否解决了这个用例的问题。希望OP会尝试一下。
先生,我在帖子中更新了下面的代码,mp=null; / mp2 = null; 行代码是灰色的,它说:分配给'mp'的值null从未使用过
@ProjectX,我已经用null check
更新了答案。另请注意创建媒体播放器后的额外setVolume
调用。
先生,我在帖子中更新了下面的代码,mp=null; /mp2 = 空;代码行是灰色的,它说:从未使用分配给“mp”的值 null + 代码是否正确,或者我需要更改什么?
我更新了我的代码。该应用程序正在崩溃(我无法启动它)。错误:*原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.media.MediaPlayer.setVolume(float, float)”以上是关于一段时间后声音停止的主要内容,如果未能解决你的问题,请参考以下文章
SoundPool 和 MediaPlayer 在一段时间后循环播放的哔声停止
Phonegap / Cordova 在后台 IOS 一段时间后停止音频