Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Problem? You must specify audioStreamType before .prepare()! So this is working.</p> <pre><code>MediaPlayer player = new MediaPlayer(); AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.alarm); player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); player.setAudioStreamType(AudioManager.STREAM_ALARM); player.setLooping(true); player.prepare(); player.start(); </code></pre> <p>But if you remove</p> <blockquote> <p>player.setAudioStreamType(AudioManager.STREAM_ALARM);</p> </blockquote> <p>it won't work. If you use <strong>MediaPlayer.create(context, resid)</strong> - the static method itself will make prepare before you use</p> <blockquote> <p>player.setAudioStreamType(AudioManager.STREAM_ALARM);</p> </blockquote> <p>because body of the method is (see <strong>mp.prepare()</strong> before return statement)</p> <pre><code>public static MediaPlayer create(Context context, int resid) { try { AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid); if (afd == null) return null; MediaPlayer mp = new MediaPlayer(); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); return mp; } catch (IOException ex) { Log.d(TAG, "create failed:", ex); // fall through } catch (IllegalArgumentException ex) { Log.d(TAG, "create failed:", ex); // fall through } catch (SecurityException ex) { Log.d(TAG, "create failed:", ex); // fall through } return null; } </code></pre> <p>So use the snippet at the beginning or use own static method</p> <pre><code>public static MediaPlayer create(Context context, int resid, boolean looping) { try { AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid); MediaPlayer mp = new MediaPlayer(); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.setAudioStreamType(AudioManager.STREAM_ALARM); if (looping) mp.setLooping(true); mp.prepare(); return mp; } catch (IOException ex) { Log.d("MediaPlayer", "create failed:", ex); // fall through } catch (IllegalArgumentException ex) { Log.d("MediaPlayer", "create failed:", ex); // fall through } catch (SecurityException ex) { Log.d("MediaPlayer", "create failed:", ex); // fall through } return null; } </code></pre> <p>and use it in your activity as (third argument is to loop the sound or not)</p> <pre><code>MediaPlayer player = MainActivity.create(this, R.raw.alarm, true); player.start(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload