0

I use this code to try:

AudioManager audioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE); 
        audioManager.setMode(AudioManager.MODE_IN_CALL);
        audioManager.setSpeakerphoneOn(true);

And then:

Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse("url.mp3"));
                        r.play();

But my app doesn' reproduce any sound.

How can i solve my problem?

user3253955
  • 433
  • 1
  • 8
  • 17
  • You sure your URI is correctly parsed? According to http://developer.android.com/reference/android/net/Uri.html then this class does not do much checking and is happy to throw garbage back to its user. I am very sceptical about the "url.mp3" string, where is this file stored? – ojs Mar 11 '14 at 21:12
  • The url is correct. I wrote "url.mp3" to be simple. – user3253955 Mar 11 '14 at 21:24

1 Answers1

1

According to the OP, the best answer can be found using the MediaPlayer (a link with an example is given in the comments section of this answer).

-- Previous Edits --

Haven't tested this so forgive me if it is buggy, but I think it may work better by setting the default value for the ringtone and then calling that default value. I haven't had a chance to test the code, but it should look something like...

To route the audio to your earpiece:

private AudioManager audioManager;  
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  
audioManager.setMode(AudioManager.MODE_IN_CALL); 
audioManager.setSpeakerphoneOn(false);

Then check out AudioTracks, it might be the way to go with what you want to do as Ringtone's have default actions based on Android's native processing; it should be something like

InputStream in =getResources().openRawResource("user_mp3");      
AudioTrack audio = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);

byte[] sound = null;
sound = new byte[in.available()]; 
sound =convertStreamToByteArray(in);
in.close();
audio.write(sound, 0, sound.length());
audio.play();

But be sure to set your mode back to normal with your AudioManager when you are done. I think this should work. There is also the deprecated AudioManager.ROUTE_EARPIECE call, you might want to check and see how they have replaced it.

Again, didn't have time to test this, just typed it up on the fly. Let me know if you find an error.

My original "ringtone" style output:

Uri soundPath = Uri.parse("uri_link_for_mp3");
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), 
        RingtoneManager.TYPE_RINGTONE, soundPath);

Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ringtone);
r.play();

Had to type this up kind of quick, might have missed something just not thinking of it. There is a good link for this though: Setting Ringtone in Android

Community
  • 1
  • 1
zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • You mean you don't want it played by the speaker or it isn't playing with my code? – zgc7009 Mar 11 '14 at 22:06
  • With your solution, the sound is played normally and not through the speaker that is used during a call.. – user3253955 Mar 12 '14 at 00:09
  • Oh so you want it to play through the earphone not the ringtone speaker? – zgc7009 Mar 12 '14 at 13:21
  • I want simulate a call..i want reproduce sound, making it appear to come from a call.. – user3253955 Mar 12 '14 at 14:01
  • Make sure you set your speakerphone to false in your AudioManager (you don't want it coming through the speaker phone) – zgc7009 Mar 12 '14 at 14:15
  • An integer to determine the allotted buffer space, set it to like 100000. – zgc7009 Mar 12 '14 at 15:57
  • Ok, and how can i set my sound? – user3253955 Mar 12 '14 at 16:25
  • @user3253955 check my updated post, let me know if it works. may have missed something. If it doesn't work with AudioTrack, you might want to check MediaPlayer. I don't know if it is handled the same though so linking it through the earpiece may be more difficult. Here is a simple implementation I found on SO: http://stackoverflow.com/questions/4162230/how-do-i-play-an-mp3-in-the-res-raw-folder-of-my-android-app – zgc7009 Mar 12 '14 at 16:42
  • Has sound ever played? If you have never been able to get sound to play at all with any of the code I have given you then there is something wrong with your sound file or your resource linking to it – zgc7009 Mar 12 '14 at 16:45
  • So, to make sure I am clear, if you implement AudioTrack without the AudioManager, it plays, but as soon as you use AudioManager your sound goes silent? – zgc7009 Mar 12 '14 at 17:07
  • @user325955 Glad to hear it :) – zgc7009 Mar 12 '14 at 17:08