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.
Related
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.
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!
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'm currently developing an Android application that plays a sound to the user at various intervals. I have working code (included below) which functions exactly as expected on my Hero (running 2.2) and on the 1.6 emulator. However, it doesn't work on my friends Xperia x8 -- no sound is played. He has a notification tone set, which plays fine when he receives a text and so on.
private void prepareAudio(){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mp = new MediaPlayer();
if(alert != null){
try {
mp.setDataSource(ctx, alert);
} catch (IllegalArgumentException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (SecurityException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (IllegalStateException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (IOException e) {
Log.i("Prepare Audio", e.getMessage());
}
}
am = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
}
private void notifyUser(){
if(alert != null){
if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(false);
try {
mp.prepare();
} catch (IllegalStateException e) {
Log.i("Notify User", e.getMessage());
} catch (IOException e) {
Log.i("Notify User", e.getMessage());
}
mp.start();
} else {
Log.i("Notify User", "Alarm volume zero");
}
} else {
Log.i("Notify User", "Uri is null");
}
long[] pattern = {0,200,300,600};
v.vibrate(pattern, -1);
}
PrepareAudio is called on initialising the class, and notifyUser every time we want to play the sound. The phone always vibrates when it should, so notifyUser is definitely being called.
The version my friend has installed uses e.printStackTrace rather than Log.i, so there's nothing being spat out in logcat. I'm going to try and get hold of his phone to update it to a version with Log.i, but in the meantime is there anything obviously wrong with the code that could cause such an intermittent problem?
Thanks,
So it turned out that updating my exception handling as listed in the original post appears to have fixed the problem.
If you're using the above code for anything, it's worth noting that sometimes e.getMessage() returns null, so rather than
Log.i("Notify User", e.getMessage());
use
Log.i("Notify User", e.getMessage() + "");
To avoid any nasty FCs when an exception is thrown.
Sorry for the incidentally unanswerable question!
Not sure, but older versions of Android had problems playing WAV files. Are these WAV files or MP3 files that you're trying to play?