我正在努力做一点申请...我在应用程序中使用了不同的声音..我很困惑,我应该使用方法release()
释放资源。
你能看看我的代码,让我知道我是否朝着正确的方向前进:
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SpellToNumbers extends Activity implements OnClickListener {
Button btn_next;
Button btn_restart;
Button btn_previous;
TextView tvSTN, tvSTNN;
int a = 0;
RelativeLayout stnXML;
MediaPlayer vone,vtwo,vthree,vfour,vfive,vsix,vseven,veight,vnine,vten;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spell_to_numbers);
MediaPlayer onStart = MediaPlayer.create(SpellToNumbers.this, R.raw.one110 );
onStart.start();
tvSTN = (TextView) findViewById(R.id.tvSTN);
tvSTNN = (TextView) findViewById(R.id.tvSTNN);
stnXML = (RelativeLayout) findViewById(R.id.stnXML);
btn_next = (Button) findViewById(R.id.btnNextSTN);
btn_next.setOnClickListener(this);
btn_restart = (Button) findViewById(R.id.btnRestartSTN);
btn_restart.setOnClickListener(this);
btn_previous = (Button) findViewById(R.id.btnPreviousSTN);
btn_previous.setOnClickListener(this);
vone = MediaPlayer.create(this,R.raw.one110);
vtwo = MediaPlayer.create(this,R.raw.two210);
vthree = MediaPlayer.create(this,R.raw.three310);
vfour = MediaPlayer.create(this,R.raw.four410);
vfive = MediaPlayer.create(this,R.raw.five510);
vsix = MediaPlayer.create(this,R.raw.six610);
vseven = MediaPlayer.create(this,R.raw.seven710);
veight = MediaPlayer.create(this,R.raw.eight810);
vnine = MediaPlayer.create(this,R.raw.nine910);
vten = MediaPlayer.create(this,R.raw.ten1010);
}//onCreate ends
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}//onCreateOptionMenu ends
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnRestartSTN:
restart();
break;
case R.id.btnNextSTN:
next();
break;
case R.id.btnPreviousSTN:
previous();
break;
}//switch ends
}//onClick ends
private void restart() {
a = 0;
setPage();
}
private void previous() {
if (a > 0) {
a--;
}
setPage();
}
private void next() {
if (a < 9) {
a++;
}
setPage();
}
private void setPage() {
if (a == 0) {
tvSTNN.setText("1");
tvSTN.setText("ONE");
tvSTNN.setTextColor(Color.parseColor("#F2416B"));
tvSTN.setTextColor(Color.parseColor("#F2416B"));
stnXML.setBackgroundColor(Color.parseColor("#3FD161"));
vone.start();
}
if (a == 1)
{
tvSTNN.setText("2");
tvSTNN.setTextColor(Color.parseColor("#F0EFDA"));
tvSTN.setText("TWO");
tvSTN.setTextColor(Color.parseColor("#F0EFDA"));
stnXML.setBackgroundColor(Color.parseColor("#0A0EF5"));
vtwo.start();
}
else if (a == 2)
{
tvSTNN.setText("3");
tvSTNN.setTextColor(Color.parseColor("#F5F507"));
tvSTN.setText("THREE");
tvSTN.setTextColor(Color.parseColor("#F5F507"));
stnXML.setBackgroundColor(Color.parseColor("#D15BE3"));
vthree.start();
}
else if (a == 3)
{
tvSTNN.setText("4");
tvSTNN.setTextColor(Color.parseColor("#0AF5DD"));
tvSTN.setText("FOUR");
tvSTN.setTextColor(Color.parseColor("#0AF5DD"));
stnXML.setBackgroundColor(Color.parseColor("#F50A0A"));
vfour.start();
}
else if (a == 4)
{
tvSTNN.setText("5");
tvSTNN.setTextColor(Color.parseColor("#171117"));
tvSTN.setText("FIVE");
tvSTN.setTextColor(Color.parseColor("#171117"));
stnXML.setBackgroundColor(Color.parseColor("#0AF5DD"));
vfive.start();
}
else if (a == 5)
{
tvSTNN.setText("6");
tvSTNN.setTextColor(Color.parseColor("#072BF5"));
tvSTN.setText("SIX");
tvSTN.setTextColor(Color.parseColor("#072BF5"));
stnXML.setBackgroundColor(Color.parseColor("#0AF525"));
vsix.start();
}
else if (a == 6)
{
tvSTNN.setText("7");
tvSTNN.setTextColor(Color.parseColor("#8F0AF5"));
tvSTN.setText("SEVEN");
tvSTN.setTextColor(Color.parseColor("#8F0AF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5F10A"));
vseven.start();
}
else if (a == 7)
{
tvSTNN.setText("8");
tvSTNN.setTextColor(Color.parseColor("#37F507"));
tvSTN.setText("EIGHT");
tvSTN.setTextColor(Color.parseColor("#37F507"));
stnXML.setBackgroundColor(Color.parseColor("#9C2497"));
veight.start();
}
else if (a == 8)
{
tvSTNN.setText("9");
tvSTNN.setTextColor(Color.parseColor("#F50707"));
tvSTN.setText("NINE");
tvSTN.setTextColor(Color.parseColor("#F50707"));
stnXML.setBackgroundColor(Color.parseColor("#BAF50A"));
vnine.start();
}
else if (a == 9)
{
tvSTNN.setText("10");
tvSTNN.setTextColor(Color.parseColor("#0ACAF5"));
tvSTN.setText("TEN");
tvSTN.setTextColor(Color.parseColor("#0ACAF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5700A"));
vten.start();
tvSTN.setClickable(false);
}
}//setPage Method ends
}//end of class
答案 0 :(得分:0)
我很困惑,我应该使用方法release();
如果您只使用MediaPlayer
播放一首歌曲,那么您应该将其放在onPause()
@Override
public void onPause()
{
onStart.release();
super.onPause();
}
如果另一个Activity
进入前台或Activity
被销毁,这将释放资源。如果您在Activity
的不同位置使用它,那么您希望在每次完成之前释放资源,然后再次调用start()
。
答案 1 :(得分:0)
on onResume()方法在创建成功时启动媒体播放器
onPause()方法暂停MediaPlayer。如果活动即将被杀死,请停止活动 MediaPlayer然后释放它的所有资源。
@Override
protected void onResume() {
super.onResume();
if (mediaPlayer != null) {
mediaPlayer.start();
}
}
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.pause();
if (isFinishing()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
答案 2 :(得分:0)
检查完代码后,我认为最好使用 soundpool而不是Mediaplayer 。
当你的应用程序要进行backgound(onPause())和onResume()时,你可以从mediaplayer释放你的资源,你可以将资源重新分配给mediaplayer。
@Override
public void onPause()
{
onStart.release();
super.onPause();
}
@Override
public void onResume()
{
vone = MediaPlayer.create(this,R.raw.one110);
vtwo = MediaPlayer.create(this,R.raw.two210);
vthree = MediaPlayer.create(this,R.raw.three310);
vfour = MediaPlayer.create(this,R.raw.four410);
vfive = MediaPlayer.create(this,R.raw.five510);
vsix = MediaPlayer.create(this,R.raw.six610);
vseven = MediaPlayer.create(this,R.raw.seven710);
veight = MediaPlayer.create(this,R.raw.eight810);
vnine = MediaPlayer.create(this,R.raw.nine910);
vten = MediaPlayer.create(this,R.raw.ten1010);
super.onResume();
}