0

我正在使用 Ringtone 类在消息发送时播放声音。但铃声无法在摩托罗拉 Turbo Droid 等某些设备上播放。我正在使用这样的铃声类。

Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+  mContext.getPackageName() + "/" + R.raw.send_sound);
Ringtone ringtone = RingtoneManager.getRingtone(mContext, uri);
ringtone.play();
4

1 回答 1

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(); 
}

谢谢,快乐编码......

于 2019-07-17T06:06:15.780 回答