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)
Related
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.
i faced some problems when try to view video using VideoView. i always got message Can't play this video after that appears failed to connect
This is my url
rtsp://136.243.10.198:1935/xxx_vod/xx_1455171793.mp4?key=b1fbf3e6e6edcc2bb6c75f879edf8bf0
i got error on Log :
E/MediaPlayer: setDataSource: IOException! uri=rtsp://136.243.10.198:1935/xxx_vod/xx_1455171793.mp4?key=b1fbf3e6e6edcc2bb6c75f879edf8bf0
and
E/MediaPlayer: error (262, -1010)
My Code :
path = getActivity().getIntent().getStringExtra("path");
videoView.setMediaController(new MediaController(getActivity()));
videoView.setVideoPath(path);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
I am writing an app that streams videos from the network using MediaPlayer and SurfaceView.
All is working fine most of the time but I've noticed that after some repeated app restarts and debugging I am not able to stream anymore files.
I close and restart the app, rebuild and run, closing and uninstalling it from the device.. nothing helps.
When the issue happens I am getting this error from the MediaPlayer (Unknown error):
E/MediaPlayer﹕ error (1, -2147483648)
The only thing that let me return to normal behaviour so that everything is working and the streaming is good is restarting the device. No code change and no other steps, just restart and re-run the app (not ever re-installing it..)
Creating the MediaPlayer object as follow:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setDataSource(getContext(), mVideoUrl);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(mRunLopping);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.prepareAsync();
Maybe something with global device resources? caching? any clue will help.
This is how I do it, each time the MediaPlayer is called...not sure if it's the correct way but I had the same thing happening and this helped. Looks like your code might be stacking MediaPlayer instances on top of each other.
public static MediaPlayer mPlayer = null;
void createMediaPlayerIfNeeded() {
if (mPlayer == null) {
mPlayer = new MediaPlayer();
mPlayer.setOnPreparedListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
} else
mPlayer.reset();
}
}
Stopping the MediaPLayer:
if (mPlayer != null) {
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
I have the source code of an app that plays .mp4 and I have to make it works with HLS.
The video and the sound are played but I have 2 problems ONLY WITH .m3u8 :
1) When seekTo() is activated (commentaries deleted) the sound is disabled and when I want to quit the player, it makes a long time to do and it makes crash the app.
2) setLooping doesn't work and return Error (-38,0), Attempt to perform seekTo in wrong
state: mPlayer=0x1e0380, mCurrentState=0
Note: those problems are just for .M3U8
Below code of the player :
private void playVideo() {
doCleanUp();
try {
// Create a new media player and set the listeners
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(mFilePath);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnPreparedListener(this);
//mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//mMediaPlayer.seekTo(mResumePosition);
//mMediaPlayer.setLooping(true);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage(), e);
showErrorMessage(mErrorPlayingVideo);
// Toast.makeText(this, "Impossible de jouer la vidéo",
// 5000).show();
}
}
Logs when setLooping is activated under setDataSource() :
error (-38, 0)
prepareAsync called in wrong state 0
prepareAsync_l return error =-38
error: null
java.lang.IllegalStateException
at android.media.MediaPlayer.prepare(Native Method)
at fr.niji.broadpeak.activity.BroadpeakDemoPlayer.playVideo(BroadpeakDemoPlayer.java:409)
at fr.niji.broadpeak.activity.BroadpeakDemoPlayer.onRequestFinished(BroadpeakDemoPlayer.java:585)
at fr.niji.lib.dataproxy.service.DataManager.handleResult(DataManager.java:262)
at fr.niji.lib.dataproxy.service.DataManager.onRequestFinished(DataManager.java:292)
at fr.niji.lib.dataproxy.service.ServiceHelper.handleResult(ServiceHelper.java:297)
at fr.niji.lib.dataproxy.service.ServiceHelper$EvalReceiver.onReceiveResult(ServiceHelper.java:119)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4126)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
and below the code :
Attempt to perform seekTo in wrong state: mPlayer=0x1e0380,
mCurrentState=0
error (-38, 0)
Error (-38,0)
Error (-38,0)
Attempt to perform seekTo in wrong state: mPlayer=0x1e0380, mCurrentState=0
Error (-38,0)
Error (-38,0)
Attempt to perform seekTo in wrong state: mPlayer=0x1e0380, mCurrentState=0
Ok I found the problem. When you use seekTo() with a .m3u8 file and the value between the parentheses is set to 0, it doesn't work so I added a condition into playBackVideo() method :
if (mResumePosition > 0) mMediaPlayer.seekTo(mResumePosition);
So mResumePosition will never be set to 0 and play at the beginning of the file. Also, i resolved the setLooping() problem, in
public void onCompletion(final MediaPlayer mediaPlayer)
I added mMediaPlayer.release(); playVideo();
and the looping works very well !
You need to check if it is the LIVE case. If Yes, Looping and seeking might not available.
I am writing an Android alarm application that uses a Service in order to play the alarm tone. Currently, I am able to get the audio to play, but it plays in a form that can be muted by turning down the device's volume. Thus, I am trying to add a call to setAudioStreamType(AudioManager.STREAM_ALARM); to prevent this.
I have the following for my onStartCommand() function for the service:
MediaPlayer mMP;
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try
{
mMP = MediaPlayer.create(this, R.raw.alarm);
mMP.setAudioStreamType(AudioManager.STREAM_ALARM);
mMP.setLooping(true);
//mMP.prepare(); commented out since prepare() is called in create
}
catch (Exception e)
{
e.printStackTrace();
}
if (mMP != null) mMP.start();
return START_STICKY;
}
My problem is that with the call to setAudioStreamType(), the MediaPlayer never plays the audio. If I comment that line out, the audio plays.
With the line in, I get the following runtime error(s):
04-10 19:32:03.115: E/MediaPlayer(3411): setAudioStream called in state 8
04-10 19:32:03.115: E/MediaPlayer(3411): error (-38, 0)
04-10 19:32:03.115: E/MediaPlayer(3411): start called in state 0
04-10 19:32:03.115: E/MediaPlayer(3411): error (-38, 0)
04-10 19:32:03.115: E/MediaPlayer(3411): Error (-38,0)
04-10 19:32:03.115: E/MediaPlayer(3411): Error (-38,0)
Some research (I can't find the link now) told me that setAudioStreamType() can't be called after prepare() has been called, and that create() implicitly calls prepare().
In any regard, how am I supposed to setAudioStreamType() without such an error?
You can either call mp.reset() and then set the stream type, data source, and then prepare. Alternately just use the default constructor and handle the initialization yourself.
EDIT:
Resources res = getResources();
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.alarm);
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
Accepted answer was throwing an IllegalStateException. This is working
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(
this,
getCustomToneUri()
);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}