Illegal State Problem with Media Player

All your problems with Audio, Video and Images.

Illegal State Problem with Media Player

Postby SCM » Wed Mar 24, 2010 10:31 am

hi together,

i'm implementing a game, and every time a specific event happens, i play a sound. i did it like this:

Code: Select all
private static MediaPlayer shortEatSound;
shortEatSound = MediaPlayer.create(context, R.raw.eating_short);
shortEatSound.start();


this works great in the emulator, but if im trying to do it on the phone, there is an illegal state exception:

Code: Select all
start called in state 0


then i saw in the documentation, that if you call start more than once, you have to first call reset and prepare. so i changed my code:

Code: Select all
private static MediaPlayer shortEatSound;
shortEatSound = MediaPlayer.create(context, R.raw.eating_short);
shortEatSound.start();
shortEatSound.reset();
shortEatSound.prepare();


but know i get the error:

Code: Select all
start called in state 1


any hints why this doesn't work?????
thanks a lot!
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Top

Postby szakal » Fri Apr 16, 2010 1:48 pm

try this...



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package pl.szakal.SoundsExample;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.util.HashMap;
  8.  
  9. import java.util.Vector;
  10.  
  11.  
  12.  
  13. import pl.szakal.R;
  14.  
  15. import android.content.Context;
  16.  
  17. import android.content.res.AssetFileDescriptor;
  18.  
  19. import android.media.AudioManager;
  20.  
  21. import android.media.MediaPlayer;
  22.  
  23. import android.media.MediaPlayer.OnCompletionListener;
  24.  
  25.  
  26.  
  27. public class SoundsExample {
  28.  
  29.  
  30.  
  31.   private MediaPlayer mediaPlayer;
  32.  
  33.   private final OnCompletionListener mediaPlayerListener = new MediaPlayerListener();
  34.  
  35.   private Context context = null;
  36.  
  37.  
  38.  
  39.   public SoundsExample(Context context)
  40.  
  41.   {
  42.  
  43.     this.context = context;
  44.  
  45.     init();
  46.  
  47.   }
  48.  
  49.  
  50.  
  51.   private void init() {
  52.  
  53.     if (mediaPlayer == null) {
  54.  
  55.       AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
  56.  
  57.       int streamVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
  58.  
  59.       mediaPlayer = new MediaPlayer();
  60.  
  61.       mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  62.  
  63.       mediaPlayer.setOnCompletionListener(mediaPlayerListener);
  64.  
  65.       mediaPlayer.setVolume(streamVolume, streamVolume);
  66.  
  67.     }
  68.  
  69.   }
  70.  
  71.  
  72.  
  73.   private void setSound(int id) {
  74.  
  75.     if (mediaPlayer!=null) {
  76.  
  77.       mediaPlayer.reset();
  78.  
  79.       AssetFileDescriptor file = context.getResources().openRawResourceFd(id);
  80.  
  81.       try {
  82.  
  83.         mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
  84.  
  85.         file.close();
  86.  
  87.         mediaPlayer.prepare();
  88.  
  89.       } catch (IOException e) {
  90.  
  91.         mediaPlayer = null;
  92.  
  93.       }
  94.  
  95.     }
  96.  
  97.   }
  98.  
  99.  
  100.  
  101.   public void playSound(int id) {
  102.  
  103.     if (mediaPlayer!=null) {
  104.  
  105.       setSound(id);
  106.  
  107.       mediaPlayer.start();
  108.  
  109.     }
  110.  
  111.   }
  112.  
  113.  
  114.  
  115.   private static class MediaPlayerListener implements OnCompletionListener {
  116.  
  117.     public void onCompletion(MediaPlayer mediaPlayer) {
  118.  
  119.       mediaPlayer.seekTo(0);
  120.  
  121.     }
  122.  
  123.   }
  124.  
  125. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
szakal
Freshman
Freshman
 
Posts: 3
Joined: Thu Apr 15, 2010 8:14 am
Location: 52.4164, 16.9243

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 25 guests