Guest User

Untitled

a guest
Jan 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. if (mp != null)
  2. {
  3. if (mp.isPlaying())
  4. {
  5. mp.stop();
  6. }
  7. if (mp != null) mp.release();
  8. if (mp != null) mp = null;
  9. }
  10.  
  11. mp = MediaPlayer.create(this, R.raw.sound);
  12.  
  13. if (mp !=null)
  14. {
  15. mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
  16. {
  17. public void onPrepared(MediaPlayer mp)
  18. {
  19. if (mp != null) mp.start();
  20. }
  21. };
  22. }
  23. else
  24. {
  25. // error, what should I do here to recover from this situation?
  26. }
  27.  
  28. AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sound);
  29.  
  30. //Resets the MediaPlayer state but keeps the resources
  31. mp.reset();
  32.  
  33. //Sets the data source to the requested sound (R.raw.sound)
  34. mp.setDataSource(afd.getFileDescriptor());
  35.  
  36. //Prepare the MediaPlayer to play and then start
  37. mp.prepare();
  38. mp.start();
Add Comment
Please, Sign In to add comment