我正在使用Ringtone类在消息发送时播放声音。但是铃声无法在某些设备上播放声音,例如Motorola Turbo Droid。我正在使用像这样的铃声类。
Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ mContext.getPackageName() + "/" + R.raw.send_sound);
Ringtone ringtone = RingtoneManager.getRingtone(mContext, uri);
ringtone.play();
答案 0 :(得分:0)
我正在使用媒体播放器从原始文件夹中播放mp3文件,并且效果很好。
public class DummyActivity extends AppCompatActivity {
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
playSound()
}
void playSound() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.reset();
}
mPlayer = MediaPlayer.create(DummyActivity.this, R.raw.bubble);
mPlayer.start();
}
或者您可以像这样铃声类
try {
Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");
// The line below will set it as a default ring tone replace
// RingtoneManager.TYPE_RINGTONE with RingtoneManager.TYPE_NOTIFICATION
// to set it as a notification tone
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
path);//Optional
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), path);
r.play();
}
catch (Exception e) {
e.printStackTrace();
}
谢谢,祝您编码愉快。