1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.effectstest; 18 19 import android.content.Context; 20 import android.content.res.AssetFileDescriptor; 21 import android.os.Bundle; 22 import android.util.Log; 23 import android.view.KeyEvent; 24 import android.view.Menu; 25 import android.view.View.OnClickListener; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.Button; 29 import android.widget.TextView; 30 import android.widget.EditText; 31 import android.widget.SeekBar; 32 import android.widget.ToggleButton; 33 import android.widget.CompoundButton; 34 import android.widget.CompoundButton.OnCheckedChangeListener; 35 import android.widget.ImageButton; 36 import android.widget.ImageView; 37 import android.media.AudioManager; 38 import android.media.MediaPlayer; 39 import java.nio.ByteOrder; 40 import java.nio.ByteBuffer; 41 import java.io.IOException; 42 import java.util.HashMap; 43 import java.util.Map; 44 45 public class SimplePlayer implements OnClickListener { 46 47 private final static String TAG = "SimplePlayer"; 48 49 int mPlayPauseButtonId; 50 int mStopButtonId; 51 Context mContext; 52 ImageView mPlayPauseButton; 53 int mPlayImageResource; 54 int mPauseImageResource; 55 String mFileName; 56 int mFileResId; 57 MediaPlayer mMediaPlayer; 58 int mStreamType; 59 int mSession; 60 float mSendLevel = (float)0.5; 61 int mEffectId = 0; 62 TextView mSessionText; 63 SimplePlayer(Context context, int playPausebuttonId, ImageView playPausebutton, int stopButtonId, ImageView stopButton, TextView sessionText, String fileName, int stream, int session)64 SimplePlayer(Context context, int playPausebuttonId, ImageView playPausebutton, 65 int stopButtonId, ImageView stopButton, TextView sessionText, String fileName, int stream, int session) 66 { 67 set(context, playPausebuttonId, playPausebutton, stopButtonId, stopButton, sessionText, stream, session); 68 mFileName = fileName; 69 } 70 SimplePlayer(Context context, int playPausebuttonId, ImageView playPausebutton, int stopButtonId, ImageView stopButton, TextView sessionText, int fileResId, int stream, int session)71 SimplePlayer(Context context, int playPausebuttonId, ImageView playPausebutton, 72 int stopButtonId, ImageView stopButton, TextView sessionText, int fileResId, int stream, int session) { 73 set(context, playPausebuttonId, playPausebutton, stopButtonId, stopButton, sessionText, stream, session); 74 mFileResId = fileResId; 75 mFileName = ""; 76 } 77 set(Context context, int playPausebuttonId, ImageView playPausebutton, int stopButtonId, ImageView stopButton, TextView sessionText, int stream, int session)78 public void set(Context context, int playPausebuttonId, ImageView playPausebutton, 79 int stopButtonId, ImageView stopButton, TextView sessionText, int stream, int session) { 80 mContext = context; 81 mPlayPauseButtonId = playPausebuttonId; 82 mStopButtonId = stopButtonId; 83 mPlayPauseButton = (ImageButton) playPausebutton; 84 ImageButton stop = (ImageButton) stopButton; 85 86 mPlayPauseButton.setOnClickListener(this); 87 mPlayPauseButton.requestFocus(); 88 stop.setOnClickListener(this); 89 90 mPlayImageResource = android.R.drawable.ic_media_play; 91 mPauseImageResource = android.R.drawable.ic_media_pause; 92 mStreamType = stream; 93 mSession = session; 94 mSessionText = sessionText; 95 } 96 97 onClick(View v)98 public void onClick(View v) { 99 if (v.getId() == mPlayPauseButtonId) { 100 playOrPause(); 101 } else if (v.getId() == mStopButtonId) { 102 stop(); 103 } 104 } 105 playOrPause()106 public void playOrPause() { 107 if (mMediaPlayer == null || !mMediaPlayer.isPlaying()){ 108 if (mMediaPlayer == null) { 109 try { 110 mMediaPlayer = new MediaPlayer(); 111 if (mSession != 0) { 112 mMediaPlayer.setAudioSessionId(mSession); 113 Log.d(TAG, "mMediaPlayer.setAudioSessionId(): "+ mSession); 114 } 115 116 if (mFileName.equals("")) { 117 Log.d(TAG, "Playing from resource"); 118 AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(mFileResId); 119 mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 120 afd.close(); 121 } else { 122 Log.d(TAG, "Playing file: "+mFileName); 123 mMediaPlayer.setDataSource(mFileName); 124 } 125 mMediaPlayer.setAudioStreamType(mStreamType); 126 mMediaPlayer.prepare(); 127 mMediaPlayer.setLooping(true); 128 } catch (IOException ex) { 129 Log.e(TAG, "mMediaPlayercreate failed:", ex); 130 mMediaPlayer = null; 131 } catch (IllegalArgumentException ex) { 132 Log.e(TAG, "mMediaPlayercreate failed:", ex); 133 mMediaPlayer = null; 134 } catch (SecurityException ex) { 135 Log.e(TAG, "mMediaPlayercreate failed:", ex); 136 mMediaPlayer = null; 137 } 138 139 if (mMediaPlayer != null) { 140 mMediaPlayer.setAuxEffectSendLevel(mSendLevel); 141 mMediaPlayer.attachAuxEffect(mEffectId); 142 mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 143 public void onCompletion(MediaPlayer mp) { 144 updatePlayPauseButton(); 145 } 146 }); 147 mSessionText.setText("Session: "+Integer.toString(mMediaPlayer.getAudioSessionId())); 148 } 149 } 150 if (mMediaPlayer != null) { 151 mMediaPlayer.start(); 152 } 153 } else { 154 mMediaPlayer.pause(); 155 } 156 updatePlayPauseButton(); 157 } 158 stop()159 public void stop() { 160 if (mMediaPlayer != null) { 161 mMediaPlayer.stop(); 162 mMediaPlayer.release(); 163 mMediaPlayer = null; 164 } 165 updatePlayPauseButton(); 166 } 167 isPlaying()168 public boolean isPlaying() { 169 if (mMediaPlayer != null) { 170 return mMediaPlayer.isPlaying(); 171 } else { 172 return false; 173 } 174 } 175 updatePlayPauseButton()176 public void updatePlayPauseButton() { 177 mPlayPauseButton.setImageResource(isPlaying() ? mPauseImageResource : mPlayImageResource); 178 } 179 attachAuxEffect(int effectId)180 public void attachAuxEffect(int effectId) { 181 mEffectId = effectId; 182 if (mMediaPlayer != null) { 183 Log.d(TAG,"attach effect: "+effectId); 184 mMediaPlayer.attachAuxEffect(effectId); 185 } 186 } setAuxEffectSendLevel(float level)187 public void setAuxEffectSendLevel(float level) { 188 mSendLevel = level; 189 if (mMediaPlayer != null) { 190 mMediaPlayer.setAuxEffectSendLevel(level); 191 } 192 } 193 setContext(Context context)194 public void setContext(Context context) { 195 mContext = context; 196 } 197 } 198