0

嗨,我正在尝试播放这样的警报

File f = Helper.getFileFromAsset(fart, context);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Ringtone ringtone = RingtoneManager.getRingtone(context, Uri.fromFile(f));
if (ringtone != null) {
    ringtone.setStreamType(AudioManager.STREAM_ALARM);
    ringtone.play();
}

在我的 Galaxy s4 上一切都很好,但是 s1 在 logcat 中给了我

08-11 17:27:31.958: W/PowerManagerService(128): Timer 0x3->0x1|0x1
08-11 17:27:32.813: E/RingtoneManager(2866): getRingtone : file:///data/data/xxx.package.xxx/cache/THEONE.wav, streamType : -1
08-11 17:27:32.876: E/PlayerDriver(83): PlayerDriver::it is a not Protected file
08-11 17:27:32.884: E/OsclDirectFileIO(83): [LargeFileSupport] OsclDirectFileIO::OpenFileOrSharedFd Error = -1
08-11 17:27:32.884: W/MediaPlayer(2866): info/warning (1, 26)
08-11 17:27:32.884: E/PlayerDriver(83): Command PLAYER_SET_DATA_SOURCE completed with an error or info -4
08-11 17:27:32.884: E/MediaPlayer(2866): error (-4, -4)
08-11 17:27:32.884: E/RingtoneManager(2866): Failed to open ringtone file:///data/data/xxx.package.xxx/cache/THEONE.wav
08-11 17:27:32.884: W/PlayerDriver(83): PVMFInfoErrorHandlingComplete

函数 Helper.getFileFromAsset 看起来像这样

public static File getFileFromAsset(String name, Context ctx) {
    File f = new File(ctx.getCacheDir() + "/" + new File(name).getName());
    if (!f.exists())
        try {
            InputStream is = ctx.getAssets().open(name);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(buffer);
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    return f;
}

我认为

PlayerDriver::it is a not Protected file

是问题,有什么想法吗?

4

1 回答 1

0

要播放警报声,您可以使用以下命令:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

并且不要忘记像这样提及Ringtone r

public class MainActivity extends Activity{
Ringtone r;
于 2013-08-11T17:43:29.743 回答