felgamedev

MediaPlayer Audio Basic Setup

Sep 7th, 2015
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package com.********.surfaceviewtest.Audio;
  2.  
  3. import android.content.Context;
  4. import android.content.res.AssetFileDescriptor;
  5. import android.content.res.AssetManager;
  6. import android.media.MediaPlayer;
  7. import android.media.SoundPool;
  8. import android.util.Log;
  9.  
  10. import java.io.IOException;
  11.  
  12.  
  13. public class Audio {
  14.  
  15.     public static Context context;
  16.     static AssetManager am;
  17.     static SoundPool sp;
  18.     public static MediaPlayer mediaPlayer;
  19.     public static boolean mediaPaused = false;
  20.  
  21.     /**
  22.      * Create a new music player from a filepath.
  23.      * MediaPlayer object returned is already prepared to be played
  24.      *
  25.      * @param file The filepath for the sound file to be streamed
  26.      * @return reference to the Audio class MediaPlayer object
  27.      */
  28.     public static MediaPlayer createMusic(String file) {
  29.         // Create new instance if necessary - prevents resource usage
  30.         if (mediaPlayer == null) {
  31.             mediaPlayer = new MediaPlayer();
  32.         }
  33.  
  34.         if (am == null) {
  35.             am = context.getAssets();
  36.         }
  37.  
  38.         try {
  39.             AssetFileDescriptor asset = am.openFd(file);
  40.             mediaPlayer.setDataSource(asset.getFileDescriptor(), asset.getStartOffset(), asset.getLength());
  41.             mediaPlayer.prepare();
  42.         } catch (IOException e) {
  43.             Log.e("AudioClass", "createMusic: Unable to open media from String. File not available");
  44.         }
  45.         return mediaPlayer;
  46.     }
  47.  
  48.     // Release and nullify the media player to free up resources
  49.     public static void unloadMusic() {
  50.         if (mediaPlayer == null) {
  51.             return;
  52.         }
  53.         if (mediaPlayer.isPlaying()) {
  54.             mediaPlayer.stop();
  55.         }
  56.         mediaPlayer.release();
  57.         mediaPlayer = null;
  58.     }
  59.  
  60.     public static void onStop(boolean isFinishing) {
  61.         // TODO Close all instanced Audio objects in Audio.onStop()
  62.         if ((mediaPlayer != null && mediaPlayer.isPlaying()) || mediaPaused) {
  63.             mediaPlayer.pause();
  64.             if (isFinishing) {
  65.                 Audio.unloadMusic();
  66.             }
  67.         }
  68.     }
  69.  
  70.     public static void onPause() {
  71.         if (mediaPlayer != null && mediaPlayer.isPlaying()) {
  72.             mediaPaused = true;
  73.             mediaPlayer.pause();
  74.         }
  75.     }
  76.  
  77.     public static void onResume() {
  78.         if (mediaPlayer != null && mediaPaused) {
  79.             mediaPlayer.start();
  80.             mediaPaused = false;
  81.         }
  82.     }
  83.  
  84.     public static void setMediaPaused(boolean pause) {
  85.         if (pause) {
  86.             onPause();
  87.         } else {
  88.             onResume();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment