Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (mp != null)
- {
- if (mp.isPlaying())
- {
- mp.stop();
- }
- if (mp != null) mp.release();
- if (mp != null) mp = null;
- }
- mp = MediaPlayer.create(this, R.raw.sound);
- if (mp !=null)
- {
- mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
- {
- public void onPrepared(MediaPlayer mp)
- {
- if (mp != null) mp.start();
- }
- };
- }
- else
- {
- // error, what should I do here to recover from this situation?
- }
- AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sound);
- //Resets the MediaPlayer state but keeps the resources
- mp.reset();
- //Sets the data source to the requested sound (R.raw.sound)
- mp.setDataSource(afd.getFileDescriptor());
- //Prepare the MediaPlayer to play and then start
- mp.prepare();
- mp.start();
Add Comment
Please, Sign In to add comment