I'm trying to run beeb sound continuously until user pressed the button. I added alarm.mp3 file to raw folder. But this media file not executing. I'm getting IllegalStateException.
This is my code:
MediaPlayer mp = new MediaPlayer(); try{ mp.release(); mp = MediaPlayer.create(this,R.raw.alarm); mp.prepare(); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); }catch (IOException e) { // TODO: handle exception System.out.println("Test Exception "+e); } I added that mp3 file to asset folder and try to play it but still I'm getting same exception.
This is the code I used:
MediaPlayer mp = new MediaPlayer(); try{ mp.release(); AssetFileDescriptor afd=context.getAssets().openFd("alarm.mp3s"); mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); mp.prepare(); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); }catch (IOException e) { // TODO: handle exception System.out.println("Test Exception "+e); } Any help would be appriciated. If you need more information please let me know.
UPDATE:
try{ mp = MediaPlayer.create(context,R.raw.alarm); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); } When user click the button:
mp.stop(); if(!mp.isPlaying()){ mp.release(); } This is the exception:
10-04 12:50:06.105: I/System.out(14074): Test Exception java.lang.IllegalStateException
release()has been calledMediaPlayercannot be used. Call the method when you're completely done withMediaPlayerand its resources are no longer needed.