I have a requirement where upon clicking the launcher icon of my app, it should read out the app name.
I know it's a kind of funny, but is that possible? any directions?
try this in the onCreate/onResume method of your activity: MediaPlayer
Just start the player
mp = new MediaPlayer();
mp.reset();
mp.setDataSource(fileNamePath);
mp.prepare();
mp.start();
Related
Unable to switch between songs
Totally new to this thing.
I have two buttons in my android app.
I'd like to play 2 songs using MediaPlayer.
When I play one button, everything is fine, but when I start the 2nd
button, the first song is still playing. How do I stop that?
You need to end the 1st song and then start the 2nd song. So for each button, do something like this. You need to check first to see if the music is playing, and if it is, end it. Then start your new song
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, Song1); //then song2
mPlayer.start();
My android application has two buttons. Each of them will play a sound of size 1.5 seconds if clicked. I have used MediaPlayer to achieve this. The problem is when i click one button and press another within less the 1.5 sec, the second sound wont play . My requirement is when i press the second button I want the first sound to be stopped and the second sound should start playing. How do i achieve this.
When you press the 2nd button and the music is still playing, you need to stop that music and then start playing the 2nd button's song.
so add this to your button
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(this, YourMusicHere);
mPlayer.start();
I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?
Please someone help me. Thanks...
Use MediaPlayer from Android. See: Android Example
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
File file = new File(mFileName);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
In my application, I am using this code and playing an audio file but when it's finished playing the player doesn't close. Is there a way to close the player after the whole audio is played ?
For example https://gist.github.com/850599.
I tried your code on my android phone, after the music was played the default player closed and return to the last activity. So Maybe it depends on the configuration of default player.
I think it's a better way to use MediaPlayer instand of default player which is more certain.
By the way, You can try kill the default player process. Get the time of the music, and start a thread to kill the process after the play time.
Runtime.getRuntime().exec("kill" +pid);
I have a buttons called play and stop
if I press play its starting playing. after I press stop its going to home page. But I need to go home page automatically once playing is done ie. Without clicking stop button. How can I do it for android?
thanks in advance
hi shekhar you can set an oncomplete listener to the player object. The code follows:
mPlayer.setOnCompletionListener(new OnCompletionListener(){#Override public void onCompletion(MediaPlayer mp) { "your code comes here" }});
here the mPlayer is the object of the MediaPlayer which is currently running..