I have an Android app and I want to play different sounds. My code works fine but after playing some sounds it stops playing more sounds.
My click listener is in a ListView, So it loops for different sounds.
I think the problem is because of creating and stoping, but I don't understand how to fix it?
Can anyone please help with my code?
MediaPlayer mp;
holder.alphabetSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null && mp.isPlaying()) {
mp.stop();
}
mp = MediaPlayer.create(activity,sound[index]));
mp.start();
}
});
Logcat:
2019-10-26 20:44:28.737 1319-8893/? E/MP3Extractor: Unable to resync. Signalling end of stream.
2019-10-26 20:44:28.744 1315-1946/? E/OMXNodeInstance: setConfig(52300a9:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)
2019-10-26 20:44:28.751 1306-9450/? E/AudioFlinger: no more track names available
2019-10-26 20:44:28.751 1306-9450/? E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
2019-10-26 20:44:28.751 1320-10055/? E/AudioTrack: AudioFlinger could not create track, status: -12
2019-10-26 20:44:28.751 1320-10055/? E/AudioSink: Unable to create audio track
2019-10-26 20:44:28.752 1320-10052/? E/NuPlayer: received error(0xffffffed) from audio decoder, flushing(0), now shutting down
2019-10-26 20:44:28.752 9789-9801/ir.lariha.englishanimals E/MediaPlayer: error (1, -19)
2019-10-26 20:44:28.752 9789-9789/ir.lariha.englishanimals E/MediaPlayer: Error (1,-19)
private void startMP(final Context context){
if(mp == null){
mp = MediaPlayer.create( context, R.raw.ringtone);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopMP(context);
}
});
}
mp.start();
mp.setLooping(true);
}
private void stopMP(Context context) {
if(mp != null) {
mp.release();
mp = null;
Toast.makeText(context , "song is stopped " , Toast.LENGTH_SHORT).show();
}
}
use this methods by your own necessity.
As you think ,problem is in your creating and stopping procedure. I think if you follow that , it would be helpful.
Related
I'm building an application that among other things plays some audio files.
Here is my code for doing so:
public void reproducirAudioSelect() {
String audioPath = directorio1 + File.separator + getItemSeleccionado();
try {
// mediaplayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(audioPath);
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaplayer.setLooping(false);
mediaplayer.prepare();
mediaplayer.start();
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaplayer) {
Log.i("A", "onComplete hit");
mediaplayer.stop();
mediaplayer.release();
}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
There is no problem when I just try to play whichever song I choose.
However, the song is played again and again. I just want them to be played once, and when the song is finished, stop. (Hence the use of the setOnCompletionListener(..)).
I've tried many different solutions to this problem. The most popular (https://stackoverflow.com/a/19555480/13127574) consists of placing the listener after the .start(). But it doesn't work for me. I can't see anything wrong in my code, after debugging. Simply, onCompletition is not triggered.
Logcat if that's of any help:
2020-12-28 13:14:57.677 3662-3662/com.example.a_2_b_a19manuelgp
W/MediaPlayer: Use of stream types is deprecated for operations other
than volume control 2020-12-28 13:14:57.677
3662-3662/com.example.a_2_b_a19manuelgp W/MediaPlayer: See the
documentation of setAudioStreamType() for what to use instead with
android.media.AudioAttributes to qualify your playback use case
2020-12-28 13:15:03.057 3662-3662/com.example.a_2_b_a19manuelgp
W/2_b_a19manuelg: Accessing hidden method
Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate;
(light greylist, linking) 2020-12-28 13:15:03.553
3662-3707/com.example.a_2_b_a19manuelgp D/EGL_emulation:
eglMakeCurrent: 0xefcc3580: ver 2 0 (tinfo 0xefc31ca0) 2020-12-28
13:15:03.562 3662-3662/com.example.a_2_b_a19manuelgp V/MediaPlayer:
resetDrmState: mDrmInfo=null mDrmProvisioningThread=null
mPrepareDrmInProgress=false mActiveDrmScheme=false 2020-12-28
13:15:03.563 3662-3662/com.example.a_2_b_a19manuelgp V/MediaPlayer:
cleanDrmObj: mDrmObj=null mDrmSessionId=null
I think you want to use prepareAsync() along with setOnPreparedListener(), and set your listeners before calling prepareAsync, something like this:
String audioPath = directorio1 + File.separator + getItemSeleccionado();
try
{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(audioPath);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mediaPlayer)
{
mediaPlayer.start();
}
};
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mediaPlayer)
{
Log.i("A", "onComplete hit");
mediaPlayer.stop();
mediaPlayer.release();
}
});
mediaPlayer.setLooping (false);
mediaPlayer.prepareAsync();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
I am displaying the videos in a list. After playing the videos one after another it is displaying video can't play error in android naught only in Remaining all the versions are working fine.Please help me to resolve this issue. Below is the my code for playing the video in video view.
videoview.setVideoURI(Uri.parse(streamInfo.video_streams.get(0).url));
viewHolder.large_videoview.seekTo(starttime * 1000);
viewHolder.large_videoview.start();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e("large_videoview", "<><>onInfo");
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
//Log.e("buffering starts", "buffering starts");
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
Log.e("buffering ends", "<><>buffering ends");
return false;
}
});
}
});
getting this error in my log cat
E/MediaPlayer: invoke failed: wrong state 0, mPlayer(0x7a30b0e0)
E/MediaPlayer: Error (1,-19)
E/MediaPlayer: Error (1,-1010)
Are you sure you are doing like this?
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});
i.e wait for VideoView to be prepared & once it's prepared, you call start() on it?
I'm creating a simple soundboard to play sounds when a user clicks a button. Problem is, if the button is pressed enough ( usually around 10 times ) it will eventually stop playing and show the error E/MediaPlayer: error (1, -19)
what am I doing wrong? My code that plays the sound:
private void playSound(int soundID){
final MediaPlayer mp = MediaPlayer.create(this,soundID);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
}
You should use Soundpool. It's created exactly for playing short sound effects. And it's much simpler to use than MediaPlayer. MediaPlayer should only be used for playing regular/large music.
See here for example:
http://www.vogella.com/tutorials/AndroidMedia/article.html#tutorial-play-sounds-via-soundpool
Note: No need to use onTouch as in the example, you can just use onClick for simplicity.
Reason: "W/Choreographer: (let say) Frame time is 0.239384 ms in the future! Check that graphics HAL is generating vsync timestamps using the correct timebase."
i.e, there is a much gap between timestamps.
This problem can be removed by using setOnCompletionListener() within your OnClick() method like: `
#Overrid
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Get the {#link Word} object at the given position the user clicked on
Word word = words.get(position);
Log.v("NumbersActivity","Current word: "+word);
// Create and setup the {#link MediaPlayer} for the audio resource associated with the current word
MediaPlayer mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
// Keep timeStamp sync
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mMediaPlayer.release();
}
});
}
`
private void playSound(int soundID){
final MediaPlayer mp = MediaPlayer.create(this,soundID);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
}
});
}
So i am trying to get a mediaplayer to work, but on first install the music doesnt start playing (i think it's because of permissions needed for the visualizer). But everytime afterwards it plays just fine.
code:
mPlayer = MediaPlayer.create(this, R.raw.bik);
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Log cat:
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (1,-1010)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader V/MediaPlayer[Native]: isPlaying: 0
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
The file format is .mp4
Thanks for the help.
Edit:
What i meant by everytime after;
Everytime after i gave permissions and RESTARTED the app, the music starts playing just fine and no errors are given.
Permissions:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
You are getting two different errors. The first one
E/MediaPlayer: Error (-38,0)
means that you try to call the MediaPlayer without preparing it first.
The second error
E/MediaPlayer: Error (1,-1010)
means that the media is unsupported. You can see the error codes here and here
I fixed it by changing a part in the onResumse(),
As expected it was indeed the permission causing havoc,
i had:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
if (!mPlayer.isPlaying() && !mpCreated) {
initTunnelPlayerWorkaround();
init();
} else {
mPlayer.start();
mVisualizerView.link(mPlayer);
}
} else {
cleanUp();
mPlayer.start();
Log.i("boop","biep");
}
And finally discovered by that log it was going into the else, meaning it was calling the cleanUp(); method
And in the cleanUp() method i had:
private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(!mPlayer.isPlaying()) {
mPlayer.pause();
}
}
}
The 2nd if was causing havoc (duh) and i fixed it by changing it to:
private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(mPlayer.isPlaying()) {
mPlayer.pause();
}
}
I know this is a weird and specific answer, but maybe someone else who had the same struggle can find out why he or she was having problems.
Have a great day all and thanks for trying to help
~Waro (dh19, couldnt use waro sadly)
My code to start mediaplayer:
MediaPlayer player = MediaPlayer.create(context, Uri.fromFile(new File(context.getExternalCacheDir().getAbsolutePath()+ File.separator + "test.mp3")));//MediaPlayer.create(context, resId);
player.setAudioStreamType(playOnStream);
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
if after this I start any animation (I have different animations with different views started one after another by listening onAnimationEnd) mediaplayer stops with logcat message:
05-08 11:17:43.180: W/MediaPlayer-JNI(6571): MediaPlayer finalized without being released
Currently I have no ideas about how workaround this
Make sure to release the media player object in onPause() and prepare it again in onResume()