Вопрос

i'm making an app that every activities has its own sound. my problem is even the user leaves the activiy (goes to another activiy or press back or home key) the sound is still playing.

here is my code...

import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;


    public class MainActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
        private SeekBar seekBar;
        private ImageButton startMedia;
        private ImageButton pauseMedia;
        private MediaPlayer mp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);             

            TextView title1 = (TextView) findViewById(R.id.title1);
            TextView tx1 = (TextView) findViewById(R.id.textView1);
            Typeface font = Typeface.createFromAsset(getAssets(), "font.ttf");
            title1.setTypeface(font);
            tx1.setTypeface(font);

            AudioControl();    



        }



        public void AudioControl(){
            seekBar = (SeekBar) findViewById(R.id.seekBarnew2);
            startMedia = (ImageButton) findViewById(R.id.playbuttonnew2);
            pauseMedia = (ImageButton) findViewById(R.id.pausebuttonnew2);
            seekBar.setOnSeekBarChangeListener(this);
            startMedia.setOnClickListener(this);
            pauseMedia.setOnClickListener(this); 
        }



        public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }

        public void onClick(View v) {
            pauseMedia.setVisibility(View.VISIBLE);
            seekBar.setVisibility(View.VISIBLE);
            if (v.equals(startMedia)) {
                if (mp != null && mp.isPlaying()) return;
                if(seekBar.getProgress() > 0) {
                    mp.start();
                    return;
                }
                mp = MediaPlayer.create(MainActivity.this, R.raw.lone);
                mp.start();                     
                seekBar.setProgress(0);
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }
            if (v.equals(pauseMedia) && mp!=null) {
                mp.pause();
                pauseMedia.setVisibility(View.INVISIBLE);
            }       

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) mp.seekTo(progress);

        }


    }
Это было полезно?

Решение

Override the onStop method which will be called whenever your activity becomes hidden or destroyed, and stop the sound from playing.

public void onStop() {
   super.onStop();
   mp.stop();
}

Другие советы

Use this function

public void onDestroy() {

            mp.stop();
            super.onDestroy();

        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top