Meghs
Meghs

Reputation: 269

How can I play sound when my activity starts

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View view = findViewById(R.id.textView1);
    view.setOnTouchListener(this);

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            loaded = true;
        }
    });
    soundID = soundPool.load(this, R.raw.dog_bark, 1);

 }
;
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;

        if (loaded) {
            soundPool.play(soundID, volume, volume, 1, 0, 1f);
        }
    }
    return false;
}
}

I tried this way, but the sound is being played on touch event. I want the sound to be played automatically when my activity starts. But it should stop after a loop is completed and start again on touch event. Please help.

Upvotes: 3

Views: 10756

Answers (6)

Olanrewaju O
Olanrewaju O

Reputation: 1

If your activity name is MainActivity

MediaPlayer play= MediaPlayer.create(MainActivity.this,R.raw.sound);
play.start();

Place this in the onCreate method

Upvotes: 0

Manjeet Singh Goyal
Manjeet Singh Goyal

Reputation: 186

public class CommonMethod {
public static MediaPlayer player;
public static void SoundPlayer(Context ctx,int raw_id){
        player = MediaPlayer.create(ctx, raw_id);
        player.setLooping(false); // Set looping
        player.setVolume(100, 100);

        //player.release();
         player.start();
    }
}

//you can use this method for stopping the player.
CommonMethod.player.stop();

Upvotes: 4

user1488506
user1488506

Reputation:

Put your onTouch() method's code into OnStart() method of your Activity, so that when activity is started, then Your sound plays.

Upvotes: 0

Dong Xiabin
Dong Xiabin

Reputation: 171

you can play the sound in a runnable and post it in onCreate method.

View view = findViewById(R.id.textView1);
view.post(new Runnable() {

            @Override
            public void run() {
                if (loaded) {
                    soundPool.play(soundID, volume, volume, 1, 0, 1f);
                }
            }
        });

Upvotes: 1

Exceptional
Exceptional

Reputation: 3004

Use this in the activity where you wanna hear the music

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();

Upvotes: 9

Lazy Ninja
Lazy Ninja

Reputation: 22527

If it because you are just loading the sound on onCreate() and play it on touch! Try soundPlay.play() in onCreate instead!

Upvotes: 1

Related Questions