简体   繁体   中英

java.io.FileNotFoundException when playing ringtone

I want to play Ringtone for Incoming Call. I do like below for that,

Uri defaultRingToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            if(currentRingtonePlayer != null ){
                stopRingTonePlayer();
            }
            currentRingtonePlayer = new MediaPlayer();
            currentRingtonePlayer.setDataSource(this, defaultRingToneUri);
            currentRingtonePlayer.setLooping(true);
            currentRingtonePlayer.prepare();
            currentRingtonePlayer.start();

Using this code ringtone is played. But I am getting below exception,

2021-03-16 11:59:05.415 12096-12096/ W/MediaPlayer: Couldn't open content://0@settings/system/ringtone_cache
    java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:149)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:705)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1700)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1516)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1433)
        at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1157)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1106)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1022)
        at com.CallActivity.playRingTone(CallActivity.java:620)
        at com.CallActivity.onCreate(CallActivity.java:348)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3310)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3484)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7551)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

In my case ringtone is played. Eventhough I am getting this exception. Anybody have any idea why this happens? Thanks in Advance

How about use getActualDefaultRingtoneUri

Uri defaultRingToneUri = RingtoneManager.getActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM