Android SDK设置铃声问题

发布于 2024-11-25 01:18:46 字数 2484 浏览 1 评论 0原文

我正在尝试在模拟器/手机上添加并设置默认铃声。铃声已下载并存储在应用程序文件夹/dada/dada/com.xxx/ringtones 中。铃声是ogg文件。

我使用以下代码来添加和设置铃声:

public void setRingtone() {
    aajoAsset asset = null;
    asset = mXXXX.getAssetManager().getCurrentRingtoneAsset();

    if (asset != null && asset.isSaved()/* && !asset.getName().equals(mLastAssetName)*/) {
        String filepath = asset.getDirectoryPath() + asset.getFilename();
        File ringtoneFile = new File(filepath);

        if (LOG) {
            Log.i(TAG, "Sending Intent : " + Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        }

        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + asset.getDirectoryPath() + asset.getFilename())));  

        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "1234");
        content.put(MediaStore.MediaColumns.SIZE, asset.getSize());
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
        content.put(MediaStore.Audio.Media.ARTIST, "1234");
        content.put(MediaStore.Audio.Media.DURATION, 4800);
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
        content.put(MediaStore.Audio.Media.IS_MUSIC, false);

        if (LOG) {
            Log.i(TAG, "the absolute path of the file is : " + ringtoneFile.getAbsolutePath());
        }

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(
        ringtoneFile.getAbsolutePath());
        Uri newUri = mContext.getContentResolver().insert(uri, content);

        if (LOG) {
            Log.i(TAG,"the ringtone uri is : " + newUri);
        }


        RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE, newUri);

        mLastAssetName = asset.getName();
    }

代码执行正常,铃声显示在铃声列表中,但是当我单击它来测试它或当我模拟来电时,我在 LogCat 中收到以下错误:

DEBUG/MediaPlayer(1230): Couldn't open file on client side, trying server side
ERROR/MediaPlayerService(33): Couldn't open fd for content://media/internal/audio/media/1
ERROR/MediaPlayer(1230): Unable to to create media player
ERROR/RingtoneManager(1230): Failed to open ringtone content://media/internal/audio/media/1

我有在许多论坛中搜索了解决方案,但没有找到任何解决方案。

非常欢迎任何帮助

谢谢

I am trying to add and set the default ringtone on the emulator/phone. The ringtone has been downloaded and is stored in the application folder /dada/dada/com.xxx/ringtones. the ringtones are ogg files.

I use the following code to add and set the ringtone:

public void setRingtone() {
    aajoAsset asset = null;
    asset = mXXXX.getAssetManager().getCurrentRingtoneAsset();

    if (asset != null && asset.isSaved()/* && !asset.getName().equals(mLastAssetName)*/) {
        String filepath = asset.getDirectoryPath() + asset.getFilename();
        File ringtoneFile = new File(filepath);

        if (LOG) {
            Log.i(TAG, "Sending Intent : " + Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        }

        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + asset.getDirectoryPath() + asset.getFilename())));  

        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "1234");
        content.put(MediaStore.MediaColumns.SIZE, asset.getSize());
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
        content.put(MediaStore.Audio.Media.ARTIST, "1234");
        content.put(MediaStore.Audio.Media.DURATION, 4800);
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
        content.put(MediaStore.Audio.Media.IS_MUSIC, false);

        if (LOG) {
            Log.i(TAG, "the absolute path of the file is : " + ringtoneFile.getAbsolutePath());
        }

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(
        ringtoneFile.getAbsolutePath());
        Uri newUri = mContext.getContentResolver().insert(uri, content);

        if (LOG) {
            Log.i(TAG,"the ringtone uri is : " + newUri);
        }


        RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE, newUri);

        mLastAssetName = asset.getName();
    }

The code executes fine and the ringtone shows in the ringtone list but when I click on it to test it or when I simulate a incoming call I get the following errors in LogCat:

DEBUG/MediaPlayer(1230): Couldn't open file on client side, trying server side
ERROR/MediaPlayerService(33): Couldn't open fd for content://media/internal/audio/media/1
ERROR/MediaPlayer(1230): Unable to to create media player
ERROR/RingtoneManager(1230): Failed to open ringtone content://media/internal/audio/media/1

I have searched for solutions in many forums and cannot find any.

any help is most welcome

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文