mp.stop() 问题
Posted
技术标签:
【中文标题】mp.stop() 问题【英文标题】:mp.stop() problem 【发布时间】:2011-05-20 03:57:00 【问题描述】:我是 android 新手,实际上这是我的第二个程序。我从昨晚开始编程,我试图找到几个小时的解决方案!!!我在这里阅读了所有内容,我用谷歌搜索了我的问题,但我仍然无法解决它!! 我的问题很常见,因为我知道当我单击另一个按钮时我无法停止音频播放。我尝试了 mp.release,stop,everything 但是当我按下停止按钮或返回时一直崩溃!!我做错了什么??我有22 个按钮,我播放 21 首歌曲(顶部的一个按钮用于停止)。 另外我忘了提到我在第二次点击停止播放时尝试制作按钮但我做不到这就是我放停止按钮的原因。
这是我的代码
package org.kidsongs;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
public class menu extends Activity
public MediaPlayer mp=null;
Button playerButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
);
final Button button2 = (Button) findViewById(R.id.Button02);
button2.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song2);
mp.start();
);
final Button button3 = (Button) findViewById(R.id.Button03);
button3.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song3);
mp.start();
);
final Button button4 = (Button) findViewById(R.id.Button04);
button4.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song4);
mp.start();
);
final Button button5 = (Button) findViewById(R.id.Button05);
button5.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song5);
mp.start();
);
final Button button6 = (Button) findViewById(R.id.Button06);
button6.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song6);
mp.start();
);
final Button button7 = (Button) findViewById(R.id.Button07);
button7.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song7);
mp.start();
);
final Button button8 = (Button) findViewById(R.id.Button08);
button8.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song8);
mp.start();
);
final Button button9 = (Button) findViewById(R.id.Button09);
button9.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song9);
mp.start();
);
final Button button10 = (Button) findViewById(R.id.Button10);
button10.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song10);
mp.start();
);
final Button button11 = (Button) findViewById(R.id.Button11);
button11.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song11);
mp.start();
);
final Button button12 = (Button) findViewById(R.id.Button12);
button12.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song12);
mp.start();
);
final Button button13 = (Button) findViewById(R.id.Button13);
button13.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song13);
mp.start();
);
final Button button14 = (Button) findViewById(R.id.Button14);
button14.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song14);
mp.start();
);
final Button button15 = (Button) findViewById(R.id.Button15);
button15.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song15);
mp.start();
);
final Button button16 = (Button) findViewById(R.id.Button16);
button16.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song16);
mp.start();
);
final Button button17 = (Button) findViewById(R.id.Button17);
button17.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song17);
mp.start();
);
final Button button18 = (Button) findViewById(R.id.Button18);
button18.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song18);
mp.start();
);
final Button button19 = (Button) findViewById(R.id.Button19);
button19.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song19);
mp.start();
);
final Button button20 = (Button) findViewById(R.id.Button20);
button20.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song20);
mp.start();
);
final Button button21 = (Button) findViewById(R.id.Button21);
button21.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.start();
);
final Button stopbutton = (Button) findViewById(R.id.Button22);
stopbutton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
mp.stop();
mp.release();
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.info, menu);
return true;
@Override
protected void onPause()
super.onPause();
if(mp.isPlaying())
mp.stop();
@Override
public void onDestroy()
super.onDestroy();
mp.release();
【问题讨论】:
发布您的堆栈跟踪信息,否则您将无法从这里的任何人那里得到一个像样的答案。 【参考方案1】:我相信
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.start();
应该是
public void onClick(View v)
mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.start();
【讨论】:
【参考方案2】:您发布的代码是错误的,您在每个点击处理程序中创建本地 MediaPlayer 对象。改为使用声明为活动成员的对象,然后,当您调用 stop 方法时,将正确建立引用。
button.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
);
应该是:
button.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
);
【讨论】:
活动类的 MediaPlayer 实例。您将 MediaPlayer 对象声明为该类的成员: public class menu extends Activity public MediaPlayer mp=null;以上是关于mp.stop() 问题的主要内容,如果未能解决你的问题,请参考以下文章