views:

1843

answers:

3

I have been looking everywhere how to play a ringtone/alarm sound in android.

I press a button and I want to play a ringtone/alarm sound. I could not find a easy, straightforward sample. Yes, I already looked at Alarm clock source code... but it is not straightforward and I cannot compile it.

I cannot make this work:

 Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
 mMediaPlayer = new MediaPlayer();
 mMediaPlayer.setDataSource(this, alert);
 final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
 if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            player.setAudioStreamType(AudioManager.STREAM_ALARM);
            player.setLooping(true);
            player.prepare();
            player.start();
  }

I get this error:

04-11 17:15:27.638: ERROR/MediaPlayerService(30): Couldn't open fd for content://settings/system/ringtone

So.. please if somebody knows how to play a default ringtone/alarm let me know.

I prefer not to upload any file. Just play a default ringtone.

+1  A: 

Your example is basically what I'm using. It never works on the emulator, however, because the emulator doesn't have any ringtones by default, and content://settings/system/ringtone doesn't resolve to anything playable. It works fine on my actual phone.

synic
A: 

You can push a MP3 file in your /sdcard folder using DDMS, restart the emulator, then open the Media application, browse to your MP3 file, long press on it and select "Use as phone ringtone".

Error is gone!

Edit: same trouble with notification sounds (e.g. for SMS) solved using Ringdroid application

OcuS
A: 

Copying an audio file to the sd card of the emulator and selecting it via media player as the default ringtone does indeed solve the problem.

Quelltextfabrik