gpt4 book ai didi

android - 如何停止以前在 android 中播放的铃声

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:49 26 4
gpt4 key购买 nike

我正在尝试处理通知铃声。现在,我正在点击按钮播放铃声。每当我在第一次尝试后单击按钮时,我都需要停止之前播放的铃声并开始播放新的铃声。但是我做不到。

这是我的代码:

RingtoneManager  manager = new RingtoneManager(this);
Ringtone ringtone = manager.getRingtone(getApplicationContext(), uri);
manager.setStopPreviousRingtone(true);
ringtone.play();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (ringtone.isPlaying())
ringtone.stop();
}
}, 30000);

但这行不通。

这是我的完整代码:

RingtoneManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pickMediaButton = (Button) findViewById(R.id.pickMediaButton);
manager = new RingtoneManager(this);
manager.setStopPreviousRingtone(true);
pickMediaButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
startActivityForResult(intent, 5);

}
});

}

@SuppressWarnings("static-access")
@Override
protected void onActivityResult(final int requestCode,final int resultCode, final Intent intent) {

String chosenRingtone = null;

if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

if (uri != null)
{
chosenRingtone = uri.toString();
Ringtone ringtone = manager.getRingtone(getApplicationContext(), uri);

ringtone.play();
/* final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (ringtone.isPlaying())
ringtone.stop();
}
}, 30000);*/
}
else
{
chosenRingtone = null;
}
}

Toast.makeText(MainActivity.this, chosenRingtone+" ", Toast.LENGTH_LONG).show();
}

最佳答案

没有理由在您的用例中使用 RingtoneManager 实例。

只需通过 RingtoneManager.getRingtone(Context, Uri) 获取铃声并将其设置为成员。在获取新铃声之前,请检查您之前是否启动了铃声并在它正在播放时停止。

private Ringtone ringtone;

public void method(...) {
if (ringtone != null) ringtone.stop();
ringtone = RingtoneManager.getRingtone(this, uri);
ringtone.play();
}

关于android - 如何停止以前在 android 中播放的铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25640964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com