public class HelloActivity extends Activity implements OnCompletionListener {
/** Called when the activity is first created. */
MediaPlayer myMediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("CITIZEN","HLO: Start");
setContentView(R.layout.hello); // Inflate the hello.xml file in res/layout
myMediaPlayer = MediaPlayer.create(this, R.raw.audiohello1);
myMediaPlayer.setOnCompletionListener(this);
myMediaPlayer.start();
Log.v("CITIZEN","HLO: Playing Media");
}
// Now have manners!
// If the activity is paused, do it gracefully
// by using onPause(), onResume()
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
Log.v("CITIZEN","HLO: OnResume() Start");
if (myMediaPlayer != null)
{
Log.v("CITIZEN","HLO: onResume() Not null");
if (myMediaPlayer.isPlaying() == false)
myMediaPlayer.start();
}
}
@Override
public void onPause()
{ // We have disappeared into the background OR finish() has been called
super.onPause();
Log.v("CITIZEN","HLO: onPause() Start");
if (myMediaPlayer != null)
{
Log.v("CITIZEN","HLO: onPause() MediaPlayer Not null");
if (myMediaPlayer.isPlaying() )
myMediaPlayer.stop();
}
Log.v("CITIZEN","HLO: onPause() End");
}
public void onCompletion(MediaPlayer mp)
{ // The audio is complete, so time to wrapup and goo to next activity
Log.v("CITIZEN","HLO: OnCompletion() Start");
if (mp != null)
{
if (myMediaPlayer.isPlaying() )
mp.stop();
mp.release();
mp = null; // I put this in as an attempt to flag that audio had been released
}
Log.v("CITIZEN","HLO: End Media Player");
// Now go to CameraWaitActivity
Intent intentNextActivity;
intentNextActivity = new Intent(HelloActivity.this, CameraWait1Activity.class);
intentNextActivity.putExtra("TYPE", strUserType);
intentNextActivity.putExtra("NEWS", strNewsType);
intentNextActivity.putExtra("CATEGORY", strCategoryType);
startActivity(intentNextActivity);
Log.v("CITIZEN","HLO: Finish");
HelloActivity.this.finish();
}
}
以下是CITIZEN文件管理器的输出
03-20 18:50:07.705:VERBOSE / CITIZEN(8608):SLCT:新手
03-20 18:50:07.745:VERBOSE / CITIZEN(8608):SLCT:产生新手连接后
03-20 18:50:07.855:VERBOSE / CITIZEN(8608):CNCT:正在连接
03-20 18:50:07.885:VERBOSE / CITIZEN(8608):CNCT:UserType = NOVICE
03-20 18:50:07.905:VERBOSE / CITIZEN(8608):CNCT:完成
$ 03-20 18:50:07.985:VERBOSE / CITIZEN(8608):HLO:开始
03-20 18:50:08.205:VERBOSE / CITIZEN(8608):HLO:播放媒体
03-20 18:50:08.205:VERBOSE / CITIZEN(8608):HLO:OnResume()开始
03-20 18:50:08.205:VERBOSE / CITIZEN(8608):HLO:onResule()不为
03-20 18:50:25.565:VERBOSE / CITIZEN(8608):HLO:OnCompletion()开始
03-20 18:50:25.595:VERBOSE / CITIZEN(8608):HLO:终端媒体播放器
03-20 18:50:25.625:VERBOSE / CITIZEN(8608):HLO:完成
03-20 18:50:25.625:VERBOSE / CITIZEN(8608):HLO:onPause()开始
03-20 18:50:25.635:VERBOSE / CITIZEN(8608):HLO:onPause()MediaPlayer不为空
我尝试做什么。如果活动变为背景,我想暂停音频,然后在再次获得前景时恢复。现在的问题是,活动在音频结束时中止而不是优雅地死亡。在我为活动调用finish()之后,似乎正在调用onPause(),即使我已经在finish()之前释放了它,我也会释放媒体播放器。如何检测播放器是否已在onPause()中释放,以便我不尝试释放已发布的内容。
OR
我是否完全以错误的方式解决这个问题并使用大锤来破解坚果?
建议/意见,一如既往地非常感谢(甚至建议我在做什么我是疯了!),
谢谢,
奥利弗
答案 0 :(得分:0)
你能否覆盖finish()方法并在其中加入清理逻辑,然后在整齐清理后调用super.finish()?
您还可以在此函数中设置布尔值“media_player_cleaned”以停止暂停方法尝试清理两次