简体   繁体   English

Android MediaPlayer中的错误代码(1,-2147483648)

[英]Error code (1, -2147483648) in Android MediaPlayer

I'm trying to implement a simple alarm in android by using the MediaPlayer. 我正在尝试使用MediaPlayer在android中实现一个简单的警报。 However, every time I try to prepare() it, I get an error. 但是,每次我尝试prepare()时,都会出现错误。 Here's my code. 这是我的代码。 I'm a total beginner concerning Java and Android so perhaps I'm doing something inherently wrong. 我是有关Java和Android的初学者,所以也许我在做一些天生的错误。

private void playDiscreteAlarm()
{
    alarmSound = new MediaPlayer();
    alarmSound.setAudioStreamType(AudioManager.STREAM_RING);
    Resources res=context.getResources();
    AssetFileDescriptor fd = res.openRawResourceFd(R.raw.discrete_alarm);
    try {
        alarmSound.setDataSource(fd.getFileDescriptor());
        fd.close();
        alarmSound.setLooping(true);
        alarmSound.prepare();
        alarmSound.start();
    }
    catch (IOException e)
    {
        Log.d("error\n");
    }
}

The weird thing is that this worked once and after that stopped working. 奇怪的是,它一次又一次地停止了工作。 It works when I use MediaPlayer.create() however I need to use the ringer volume instead of the media volume , and I believe this is the way to do it. 当我使用MediaPlayer.create()时它可以工作,但是我需要使用铃声音量而不是媒体音量 ,并且我相信这是做到这一点的方法。

I fixed the problem, though I'm not sure what caused it. 我已经解决了这个问题,尽管我不确定是什么原因造成的。 I just used a different and maybe slightly simpler method. 我只是使用了一种不同的方法,也许稍微简单了些。 Here's what I did: 这是我所做的:

private void playDiscreteAlarm()
{
    String filename = "android.resource://" + context.getPackageName() + "/raw/discrete_alarm";

    alarmSound = new MediaPlayer();
    alarmSound.setAudioStreamType(AudioManager.STREAM_RING);
    try {
        alarmSound.setDataSource(context, Uri.parse(filename));
        alarmSound.setLooping(true);
        alarmSound.prepare();
        alarmSound.start();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

Thanks ! 谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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