如何让我的铃声在 Android 中播放?

发布于 2024-10-28 06:00:56 字数 2206 浏览 7 评论 0原文

我正在制作一个音板应用程序,并尝试根据单击的菜单项将声音作为铃声或通知播放。该铃声当前在铃声菜单中显示为默认铃声,但当有电话打入时不会播放。我做错了什么?下面列出了我的代码。

public boolean saveas(int ressound,String file,String typesound){
    byte[] buffer=null;  
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
    int size=0;  

    try {  
        size = fIn.available();  
        buffer = new byte[size];  
        fIn.read(buffer);  
        fIn.close();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }  

    String path="/sdcard/media/audio/ringtones/";  
    String filename=file+".ogg";  

    boolean exists = (new File(path)).exists();  
    if (!exists){new File(path).mkdirs();}  

    FileOutputStream save;  
    try {  
        save = new FileOutputStream(path+filename);  
        save.write(buffer);  
        save.flush();  
        save.close();  
    } catch (FileNotFoundException e) {  
        // TODO Auto-generated catch block  
        return false;  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }      

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

    File k = new File(path, filename);  

    ContentValues values = new ContentValues();  
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
    values.put(MediaStore.MediaColumns.TITLE, file);  
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
    values.put(MediaStore.Audio.Media.ARTIST, "douchebag");  
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
    values.put(MediaStore.Audio.Media.IS_ALARM, true);  
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

    //Insert it into the database  
    this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

    //set ringtone
    Uri ringtoneUri = Uri.parse(path+filename);
    if(typesound=="ringtone")
        RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);

    return true;  
}  

I'm making a soundboard application and am trying to have a sound play as a ringtone or notification depending on which menu item is clicked. The ringtone currently shows up as the default ringtone in the ringtone menu, but does not play when a phone call comes in. What am I doing wrong? My code is listed below.

public boolean saveas(int ressound,String file,String typesound){
    byte[] buffer=null;  
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
    int size=0;  

    try {  
        size = fIn.available();  
        buffer = new byte[size];  
        fIn.read(buffer);  
        fIn.close();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }  

    String path="/sdcard/media/audio/ringtones/";  
    String filename=file+".ogg";  

    boolean exists = (new File(path)).exists();  
    if (!exists){new File(path).mkdirs();}  

    FileOutputStream save;  
    try {  
        save = new FileOutputStream(path+filename);  
        save.write(buffer);  
        save.flush();  
        save.close();  
    } catch (FileNotFoundException e) {  
        // TODO Auto-generated catch block  
        return false;  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        return false;  
    }      

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

    File k = new File(path, filename);  

    ContentValues values = new ContentValues();  
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
    values.put(MediaStore.MediaColumns.TITLE, file);  
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
    values.put(MediaStore.Audio.Media.ARTIST, "douchebag");  
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
    values.put(MediaStore.Audio.Media.IS_ALARM, true);  
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

    //Insert it into the database  
    this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

    //set ringtone
    Uri ringtoneUri = Uri.parse(path+filename);
    if(typesound=="ringtone")
        RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);

    return true;  
}  

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-11-04 06:00:56

根据这个问题这里,你需要设置带有“外部”URI 的铃声。据我所知,你基本上已经具备了所有的部分,只需要进行微小的调整。

尝试一下这个片段是否适合您:

//Insert it into the database
Uri ringtoneUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 
//set ringtone    
if(typesound=="ringtone")
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);

According to this question here, you need to set the ringtone with the "external" URI. As far as I can tell, you basically have all the pieces and just need to make a tiny adjustment.

Try if this snippet works for you:

//Insert it into the database
Uri ringtoneUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 
//set ringtone    
if(typesound=="ringtone")
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, ringtoneUri);
短暂陪伴 2024-11-04 06:00:56
FullBatteryAlarm.prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String strRingtonePrefs = FullBatteryAlarm.prefs.getString(
            FullBatteryAlarm.res.getString(R.string.ringtone), "content://settings/system/alarm_alert");
    //Log.i("strringtone", strRingtonePrefs);
    Uri notification = Uri.parse(strRingtonePrefs);

    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    //mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    mgr.setStreamVolume(
            AudioManager.STREAM_RING, 
            mgr.getStreamMaxVolume(AudioManager.STREAM_RING),
            AudioManager.FLAG_PLAY_SOUND);
    //Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

或者您可以获得默认的通知声音。

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
FullBatteryAlarm.prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String strRingtonePrefs = FullBatteryAlarm.prefs.getString(
            FullBatteryAlarm.res.getString(R.string.ringtone), "content://settings/system/alarm_alert");
    //Log.i("strringtone", strRingtonePrefs);
    Uri notification = Uri.parse(strRingtonePrefs);

    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    //mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    mgr.setStreamVolume(
            AudioManager.STREAM_RING, 
            mgr.getStreamMaxVolume(AudioManager.STREAM_RING),
            AudioManager.FLAG_PLAY_SOUND);
    //Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

OR you can get your default notification sound.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
扬花落满肩 2024-11-04 06:00:56

嘿,我认为你需要在 android 清单中设置权限。
以下是我使用的权限。

 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

此外,我不确定 ogg 文件。我使用了 mp3 文件,它对我有用。

Hey, I think you need to set the permission in the android manifest.
Below is the permission I used.

 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

Also I'm not sure about ogg files. I used mp3 files and it worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文