I've got a little problem with MediaPlayer.
Problem appears on Android 4.0.3 - 4.0.4.
Whats happening is that my onPrepare() method is not fired at all on those two versions.
Tested on 4.3 and 4.4 and no problem at all.
I have no idea what might be the reason for it to not work. So any ideas, thoughts etc are welcome.
PLAY CODE:
try {
if (!player.isPlaying()) {
player.reset();
player.setOnPreparedListener(this);
player.setOnCompletionListener(mFragment);
player.setDataSource(String.valueOf(tmpUri));
player.prepareAsync();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
start() is in on prepare method.
Logcat error:
ERROR/MediaPlayer(10830): Error (1,-2147483648)
ERROR/MediaPlayer(10830): mOnErrorListener is null. Failed to send MEDIA_ERROR message.
Ive found the answer to my problem, so I'm going to share my info.
Anyway, the problem was that on Android 4.0.4 and 4.0.3 when reading mp3 files from
device memory setDataSource() did not read the length of file right.
Which made MediaPlayer go psycho and call Error (1,-2147483648).
Android was giving all files length = 0x7ffffffffffffffL.
Why? I don't know ;)
Fix:
File file = new File(uri);
long length = file.getTotalSpace()
FileInputStream is = new FileInputStream(file);
player.setDataSource(is.getFD(), 0l, length);
player.prepareAsync();
That's it!
Hope it will help someone with similar problem, peace!
Related
I'm using ffmpeg in my Android application and sometimes I'm getting out of memory error, I'm calling the ffmpeg inside a HandlerThread, is it ok to catch out of memory error and exit the thread while the main thread keeps on running?
I read a lot of this being not a good practice, the thing is that I really need that because I have to edit the DB when there is any kind of error
fc = new FfmpegController(context, fileTmp);
try {
fc.processVideo(clip_in, clip_out, false,
new ShellUtils.ShellCallback() {
#Override
public void shellOut(String shellLine) {
}
#Override
public void processComplete(int exitValue) {
//Update the DB
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (InterruptedException e) {
} catch (Exception e) {
}catch (OutOfMemoryError e) {
//update the DB
}
No something is going wrong if you are getting OutOfMemory errors. I would look into buffering your audio, as likely you are running the whole clip through ffmpeg at once, which is going to use up alot of memory.
Also, keep in mind that lots of us doing Audio in Android end up using the NDK primarily because of issues like you are experiencing. Audio has to be really high performance, and using the NDK allows you to write more low level memory efficient audio handling.
Android's AudioTrack has a write method that allows you to push an Audio buffer to it. A warning that this is not entry level and requires some knowledge of AudioBuffer's as well as requires you to read buffers in, send them to ffmpeg and then pass to AudioTrack. Not easy to do, and unfortunately more advanced audio on Android is not easy.
I'm developing a SIP client app using Android 2.3+ API, I can make a outgoing call and receive a incoming call successfully by using SipDemo sample codes. but I can't hear any thing before peer answers the call.
What steps will reproduce the problem?
register to sip provider (mSipManager.open())
place a call (mSipManager.makeAudioCall)
silence (onRingBack method trigger in SipAudioCallListener)
other side answer (onCallEstablished method trigger in SipAudioCallListener)
both sides hear each other with good quality
I expect for ring-back tone, in step: 3 instead silence.
Can i do something in onRingBack for hear sip ring-back tone instead of silence.
Try this in your onReceive()
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Thats supposed to start the ringtone. Don't for get to put mediaPlayer.stop(); somewhere in your code. onCallEstablished is where i put mine
I'm trying to play an rtsp stream using MediaPlayer in android and the application seems to always become stuck on MediaPlayer.prepare();
The url is valid as I tested it using VLC on my desktop.
Any ideas why the application is not preparing the stream.
class InitializeService extends Thread {
#Override
public void run() {
try {
player.prepare();
Log.d("Play", "Player prepared");
} catch (IOException e) {
e.printStackTrace();
fallback();
} catch (IllegalStateException e) {
e.printStackTrace();
fallback();
}
}
}
The log statement is never reached.
Update 1:
Sorry I forgot to mention that the stream will always be in 3gp format. Here is a url rtsp://r2---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQnTXOVs7Kwo8xMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Your stream might not be of a format supported by Android.
Check http://developer.android.com/guide/appendix/media-formats.html to see if Android supports it.
Turns out it was android l that wasn't able to play the streams.
Why doesn't the MediaPlayer show the video as soon as it is available. What I mean is on the IPhone when a video is played the video shows up right away. Even when returning from pause. But on the Android the screen stays black for a couple of milliseconds to a second depending on the device used and how many processes are running in the background.
I'm asking this because i want to use one of the beginning frames from my video play as a type of screenshot and currently I'm using a handler to wait 1 second before pausing the video.
Can someone tell me a quick way to make the video show up as soon as it is started or even prepared instead of my workaround?
EDIT:
Here is how I prepare my video player so It should be prepared right.
private void initVideo()
{
Log.i("VideoPlayer", "Initialize Video File" + videoFileName);
AssetFileDescriptor afd;
try {
if(videoFileName != null);
{
afd = getAssets().openFd(videoFileName);
vidplayer = new MediaPlayer();
vidplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
vidplayer.setDisplay(holder);
vidplayer.prepare();
vidplayer.setOnCompletionListener(this);
vidplayer.setOnPreparedListener(this);
//Log.i("INITVIDEO", Integer.toString(videoPausedAt));
vidplayer.seekTo(videoPausedAt);
//Log.i("VideoPlayer", "video Prepared");
videoDuration = vidplayer.getDuration()/1000;
isVideoReady = true;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e)
{
//Log.i("InitPlayer", e.getClass().toString());
e.printStackTrace();
}
}
For the background, you can get a thumbnail of the video:
private Bitmap getThumbnail(String path){
try{
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}catch(Exception e){
return null;
}
}
When the video starts, you'll need to set the background back to null or you won't be able to see the video.
As for it not playing right away, it should play as soon as start() is called if you prepared it correctly, but it could be delayed if it has to load data let's say from a stream over the internet.
I have found that it is the phones fault.(mostly) Video's will show up automatically unless phone is bogged down with apps and thus loading of the video takes longer (noticed after having a voip service running).
I have an app that will play a tone, it did work perfectly until I installed it on Gingerbread 2.3.4 based android phone. The problem is I don't see any exception thrown, and no sound is generated, exactly same code does play sound in pre gingerbread phone. Here is the code that plays the sound.
MediaPlayer mp = new MediaPlayer();
//mp.release();
try {
String audioFilePath = "content://media/internal/audio/media/20";
mp.setDataSource(audioFilePath);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have comments placed before and after the code to play sound, all comments are printed as if sound is played, but there is no sound.
Thanks for any help.
never mind, seems like somehow the volume on alerts was set to 0, I actually went to actual folder, selected a file, played it manually and then turned the volume up while being played and all was well afterwards.