0
我建立一個應用程序的隨機我的鈴聲繼https://code.tutsplus.com教程「在Android上創建一個鈴聲隨機數發生器」,但是當我運行的應用程序,我得到這個錯誤:的MediaPlayer,JNI:QCMediaPlayer媒體播放器不存在與RingtoneManager
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得到了錯誤,所以我不認爲他們的答案在這裏適用。
你知道什麼是錯的嗎?我將不勝感激任何建議。
謝謝