How to play ringtone once through MediaPlayer? - android

I can't play a ringtone once even if mediaplayer looping property is set to false. It looks like this property was overriden by the ringtone URIused. So onCompletion is never fired.
Curiously, if the URI corresponds to a notification tone, looping behaves according to setLooping. It works well here.
So is there a way to play a ringtone once?

I faced this problem before and solved it by using OnSeekCompleteListener:
mMediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
//Your stuff
}});
I don't know if it's the best way to solve this, but it worked for me.
Edit: BTW, I use both listeners, oncompletionlistener AND onseekcompletelistener. Hope this helps!

Related

The play/pause buttons on MediaController doesn't refresh alone for audio player

I followed this tutorial http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787 to build an audio player.
All works fine, but my MediaController behave oddly. In fact the controller's seekbar does not update until the user interacts with it.
I tested with API 16 to 19 without success, i have the same problem.
I tested many solution on topics i found on stackoverflow without success also.
Has anyone an idea?
It's odd, but I found that you should update start()/pause() methods of MediaPlayerControl in such way:
#Override
public void pause() {
musicSrv.pausePlayer();
controller.show();
}
#Override
public void start() {
musicSrv.go();
controller.show();
}

Using MediaPlayer to play the same file multiple times with overlap

What's currently happening with my android application:
I mapped a simple image to a button and have it play a sound on click. On each click, I create a MediaPlayer object with a sound file in my raw folder, I set an OnClickListener for that MediaPlayer object which stops playing the file and releases it, and then I play the MediaPlayer object.
The code for the defined section:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ImageButton start = (ImageButton) findViewById(R.id.imageButton1);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.dvno);
play.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
mp = null;
}
});
play.start();
}
});
}
What's wrong with it:
It works fine and does not crash, but it's terrible for memory and very slow in general. I'd like for users to click the button many times in a row, as fast as they possibly can, and hear the sound play instantaneously over and over with overlap. Creating a new MediaPlayer object on each click and waiting for it to finish and be released consumes too many resources and the sound often lags behind the actual press of the button. I'd like to be able to create a single sound as MediaPlayer object which can be played while overlapping on itself.
Possible Solution:
Create a single final MediaPlayer object in the scope of onCreate rather than onClick and somehow use threading to start the MediaPlayer object on every click. I read that this might be a possible solution, but I haven't ever used threads before and I don't know if they're slow, if not slower, than my current code, so I'd like to know if there's a solution without using threads that might be simpler. Manipulating the state of a single MediaPlayer to overlap on itself seems impossible at this point, but maybe I'm wrong.
Would this crash the program because of illegal states? If not, is this going to be slower than I want it to be? And if not, can anyone suggest a fix to my code?
I suggest you use a SoundPool instead of MediaPlayer for this.
Tutorial is here

the seekTo() function doesn't work in VideoView

There is a problem in my application,I want to use the seekTo() function with VideoView like this:
videoView.seekTo(time);
videoView.start();
It works well in android 2.2 ,but doesn't work in android 2.3 or higher version...
Some body will tell me why? It troubles me for serval days.
The call to VideoView.start() should be made only after the seek has completed. The call to VideoView.seekTo() initiates a seek but unfortunately VideoView does not support OnSeekCompleteListener needed to notify the seek is actually done.
You can customize VideoView to support OnSeekCompleteListener as shown in my answer to 7990784.
Then you can register to receive onSeekComplete() by calling setOnSeekCompleteListener(). Your implementation of the listener should then call VideoView.start().
Have you tried VideoView class from Vitamio library?
Vitamio
For proper operation of the method seekTo(),the video state should be in PlaybackState.
Checkout the VideoView source here for get more information.
This solution should work.
The problem may be that the mediaplayer inside videoView has not been created.
It's easy to test, by changing the orientation of the device. That's how I tested it.
videoView.setOnPreparedListener(onPreparedListener);
private MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.seekTo(videoPosition);
}
};

How to produce default sound

I hava a set of buttons. When I click on a button it should produce sound.
Example:
Button b=new Button(this);
b.setText("Press");
b.setOnClickListener(new OnClickListener)[
public void click(View v)
{
b.setSoundEffectsEnabled(true);
});}
This doesn't work though, can anyone help me please.
What do you mean on default sound? If you want to play your own sound, you must create a MediaPlayer like this.
MediaPlayer mediaPlayer = MediaPlayer.create(this, [here is your sound in the raw file]);
and in the click method you need to implement this:
mediaPlayer.start();
or you can use soundpool too.
Hope it helps.
You can also use the build in sound notifications
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, ToneGenerator.MAX_VOLUME);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
If you want to play the default click sound when you click on the button, then setting b.setSoundEffectsEnabled(true) should work (although it doesn't need to be on the listener), but it is dependent on the device option to play audible selection. Try checking the device's sound settings if it is on.

Play a melody on click

When I click on a view, I want to play a melody. The problem is that when I click very fast, the melody starts from the beginning and I want it to play to the end and at this time screen must not react to clicks (because I don't want the melody to be played 10 times if I clicked very fast 10 times). I wrote something like this:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
mp.start();
if(mp.isPlaying()) {
img1.setClickable(false);
}
img1.setClickable(true);
But after one click the screen does not react to click anymore... Help me please with this.
//Init your MediaPlayer when Activity is created, for instance:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
//**************************//
//onClickListener goes here:
if(mp.isPlaying()){
//do nothing
}else{
mp.start();
}
//onClickListener ends here:
Read the android developers guide, it says "If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again"
Upon click event check if the media player has finished its playback. If no, then do nothing, if yes then play again.
To check if finished use this method below:
isPlaying()
You may check this:
http://developer.android.com/reference/android/media/MediaPlayer.html
Hope you find it easy. :)

Categories

Resources