在Galaxy S1上播放闹钟声音

时间:2013-08-11 15:34:32

标签: android alarm android-2.3-gingerbread

嗨,我正试图像这样发出一个惊人的声音

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();
}

在我的星系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

是问题,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要播放闹钟声音,您可以使用此功能:

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

并且不要忘记提及像这样的铃声

public class MainActivity extends Activity{
Ringtone r;