-1

我正在发出警报,所以当我关闭项目时如何停止声音????因为我关闭它时声音仍在播放,如何停止它?我已经放了这个但是没有用:

@Override
protected void onStop(){
    super.onStop();
    //your code for stopping the sound
}

这是代码

public class MainActivity extends Activity {
    ImageView img;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

          requestWindowFeature(Window.FEATURE_NO_TITLE);
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
          WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

           MediaPlayer media = MediaPlayer.create(getBaseContext(), R.raw.alarm);
            media.start();

        img = (ImageView) findViewById(R.id.img);
            img.setImageResource(R.drawable.image1);

    }


    @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;
    }

}
4

2 回答 2

0

将 MediaPlayer 媒体变量设为全局变量,当 onStop 触发时,您可以将其停在那里:

private MediaPlayer media;

@Override
protected void onStop(){
    super.onStop();
    //your code for stopping the sound
    if( media != null && media.isPlaying()) {
        media.stop();
    }
}
于 2013-06-03T07:07:46.110 回答
0

我在我的项目中使用的是:

Ringtone在主要活动中声明实例

Ringtone ringtone;

开始播放声音:

ringtone = RingtoneManager.getRingtone(this, Uri.parse(ringUri));
ringtone.play();

ringUri 是uri您要播放的铃声或声音。

停止声音:

ringtone.stop();
于 2013-06-03T03:53:54.640 回答