I've been trying a few things with sounds in flutter lately . Using audioPlayer plugin.
But I'm facing a wall. I have both a background music, and small sounds effects.
The problem is that I can't play both at once. If I play the background music, then the sound effect won't play. And the opposite works too.
Any idea about how to solve that issue ?
This is an issue with the audioplayer plugin and there's an issue open for it already. The author has indicated that he's open to pull requests if you'd like to take a crack at implementing it.
Use media players. Use this code to set up the background music:
MediaPlayer backgroundMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_BACKGROUND_MUSIC));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_BACKGROUND_MUSIC with the background music path.
Then when you want your sound effect to happen, use this code:
MediaPlayer soundEffectMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_SOUND_EFFECT));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_SOUND_EFFECT with the background music path.
You can learn for about media players here.
Hope this helps!
Related
I have this:
android:onClick="onClick"
android:soundEffectsEnabled="true"
in my XML. And in my class I have:
public void onClick(View v) {
v.playSoundEffect(SoundEffectConstants.CLICK);
increment(button, key, this);
}
However the CLICK sound is still not working
According with Android developer page the method PlaySoundEffect
The sound effect will only be played if sound effects are enabled by the user, and isSoundEffectsEnabled() is true.
So in the Settings-> Sounds the sound effects must be activated.
Try
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),<yoursound> );
mp.start();
instead.
This sound will play always if audio (for media) is turned on
How do I prevent the initial black screen of a VideoView? I don't want to use a placeholder, as I therefore would have to create a previewimage for every VideoView (and I'm using several VideoViews...)
A common solution for this is to use videoView.seekto(100) once the video is prepared. But this doesn't work for me. It does set the seeker to the correct position, but the VideoView remains black until the user presses the play-button.
This is my code:
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mVideoView.seekTo(100);
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mVideoView.pause();
}
});
}
I also tried this without the onSeekCompleteListener and without seperatly starting and pausing the video, but it didn't work either..
anyone a hint for solving this issue?
EDIT
I figured out that this is only an issue for streaming videos! It's working like a charm for locally stored ones...
and it actually seems to be an official issue...
https://code.google.com/p/android/issues/detail?id=4124
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
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);
}
};
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!