我构建了一个应用程序,用于随机化我的铃声"在Android上创建一个铃声随机化器"关于https://code.tutsplus.com的教程,但是当我运行应用程序时,我收到此错误:
E / MediaPlayer-JNI:QCMediaPlayer媒体播放器不存在
铃声未加载到ListView中。
这是我的代码:
mainactivity.java:
public class MainActivity extends AppCompatActivity {
private ListView list;
private ToggleButton toggle;
private List<Ringtone> ringtones;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle = (ToggleButton) findViewById(R.id.toggleButton);
list = (ListView) findViewById(R.id.list_of_ringtones);
ringtones = RingtoneHelper.fetchAvailableRingtones(this);
initToggle();
initList();
}
public void initToggle(){
final SharedPreferences sharedPreferences = getSharedPreferences("Randomizer", Context.MODE_PRIVATE);
boolean active = sharedPreferences.getBoolean("active", false);
toggle.setChecked(active);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sharedPreferences.edit().putBoolean("active", isChecked).apply();
}
});
}
public void initList(){
ArrayAdapter<Ringtone> adapter = new ArrayAdapter<Ringtone>(this, android.R.layout.simple_list_item_1){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
TextView item = (TextView) super.getView(position, convertView, parent);
String title = ringtones.get(position).getTitle(MainActivity.this);
item.setText(title);
Log.v("MainActivity", title);
return item;
}
};
list.setAdapter(adapter);
}
}
ringreceiver.java
public class RingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(callState.equals(TelephonyManager.EXTRA_STATE_RINGING)){
RingtoneHelper.changeRingtone(context);
}
}
}
}
ringtonehelper.java
public class RingtoneHelper {
public static List<Ringtone> fetchAvailableRingtones(Context context){
List<Ringtone> list = new ArrayList<>();
RingtoneManager rm = new RingtoneManager(context);
rm.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor ringtones = rm.getCursor();
int count = ringtones.getCount();
for(int i = 0; i <= count; i++){
list.add(rm.getRingtone(i));
Log.v("Ringtone Helper",String.valueOf(count));
}
return list;
}
public static void changeRingtone(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE);
if(!sharedPreferences.getBoolean("active", false)){
return;
}
RingtoneManager rm = new RingtoneManager(context);
Random random = new Random(System.currentTimeMillis());
int n = random.nextInt(rm.getCursor().getCount());
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, rm.getRingtoneUri(n));
}
}
我知道其他人已经解决了这个问题,但他们在使用MediaPlayer时遇到了错误,所以我不认为他们的答案适用于此。
你知道出了什么问题吗?我很感激任何建议。
谢谢
答案 0 :(得分:0)
所以我通过Android MediaPlayer获取歌曲uri解决了这个问题,我没有得到 E / MediaPlayer-JNI:QCMediaPlayer媒体播放器不存在错误了。解决了一个问题,但应用仍然无法正常工作。所以我的下一个想法是深入调试共享偏好。