(Android的MediaPlayer的)我怎么叫setAudioStreamType()如果MediaPlayer.create()隐式调用prepare()? [英] (Android MediaPlayer) How am I supposed to call setAudioStreamType() if MediaPlayer.create() implicitly calls prepare()?

查看:3388
本文介绍了(Android的MediaPlayer的)我怎么叫setAudioStreamType()如果MediaPlayer.create()隐式调用prepare()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,为了发挥闹钟铃声使用服务一个Android应用程序报警。目前,我能够得到的音频播放,但它可以通过调低设备的音量静音形式播放。因此,我想加入 setAudioStreamType通话(AudioManager.STREAM_ALARM); 来prevent这种

I am writing an Android alarm application that uses a Service in order to play the alarm tone. Currently, I am able to get the audio to play, but it plays in a form that can be muted by turning down the device's volume. Thus, I am trying to add a call to setAudioStreamType(AudioManager.STREAM_ALARM); to prevent this.

我对为服务我的 onStartCommand()函数如下:

I have the following for my onStartCommand() function for the service:

MediaPlayer mMP;    
@Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        try
        {
            mMP = MediaPlayer.create(this, R.raw.alarm);
            mMP.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMP.setLooping(true);
            //mMP.prepare(); commented out since prepare() is called in create
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        if (mMP != null) mMP.start();

        return START_STICKY;
    }

我的问题是,随着呼叫setAudioStreamType(),在MediaPlayer从未播放音频。如果我评论说线路输出,在音频播放。

My problem is that with the call to setAudioStreamType(), the MediaPlayer never plays the audio. If I comment that line out, the audio plays.

随着行了,我得到以下运行时错误(S):

With the line in, I get the following runtime error(s):

19 04-10:32:03.115:E / MediaPlayer的(3411):setAudioStream称为状态8

04-10 19:32:03.115: E/MediaPlayer(3411): setAudioStream called in state 8

19 04-10:32:03.115:E / MediaPlayer的(3411):错误(-38,0)

04-10 19:32:03.115: E/MediaPlayer(3411): error (-38, 0)

19 04-10:32:03.115:E / MediaPlayer的(3411):启动名为状态0

04-10 19:32:03.115: E/MediaPlayer(3411): start called in state 0

19 04-10:32:03.115:E / MediaPlayer的(3411):错误(-38,0)

04-10 19:32:03.115: E/MediaPlayer(3411): error (-38, 0)

19 04-10:32:03.115:E / MediaPlayer的(3411):错误(-38,0)

04-10 19:32:03.115: E/MediaPlayer(3411): Error (-38,0)

19 04-10:32:03.115:E / MediaPlayer的(3411):错误(-38,0)

04-10 19:32:03.115: E/MediaPlayer(3411): Error (-38,0)

一些研究(我找不到现在的链接)告诉我, setAudioStreamType()后不能被称为 prepare ()被调用,而创建()隐式调用 prepare()

Some research (I can't find the link now) told me that setAudioStreamType() can't be called after prepare() has been called, and that create() implicitly calls prepare().

在任何方面,我怎么到 setAudioStreamType()没有这样的错误?

In any regard, how am I supposed to setAudioStreamType() without such an error?

推荐答案

您既可以调用 mp.reset(),然后设置流类型,数据源,那么prepare。或者只使用默认构造函数和处理自己的初始化。

You can either call mp.reset() and then set the stream type, data source, and then prepare. Alternately just use the default constructor and handle the initialization yourself.

编辑:

Resources res = getResources();
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.alarm);

mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();

这篇关于(Android的MediaPlayer的)我怎么叫setAudioStreamType()如果MediaPlayer.create()隐式调用prepare()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆