Error - Class Equalizer - Methods getBand() and getBandFreqRange(short band) - android

I don't know more what I can do to solve these problems. I am
implementing a MediaPlayer + Equalizer app. My MediaPlayer works fine,
however, I am having problem with my equalizer: some its methods don't
work.
public class MainActivity extends Activity {
private MediaPlayer mediaPlayer;
private Equalizer equalizer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/sdcard/titanium.mp3");
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
equalizer = new Equalizer(0, 0);
equalizer.setEnabled(true);
int minEQLevel = equalizer.getBandLevelRange()[0];
int maxEQLevel = equalizer.getBandLevelRange()[1];
Log.i("kkk", "Valor de minEQLevel" + minEQLevel);
Log.i("kkk", "Valor de maxEQLevel" + maxEQLevel);
short numberOfBand = equalizer.getNumberOfBands();
Log.i("kkk", "NĂºmero de bandas = "+numberOfBand);
/******From here to down the methods don't work *****/
try {
short band = equalizer.getBand(Integer.valueOf(50000));
Log.i("kkk", "getBand() = "+band);
short band = 0;
//int menor = equalizer.getBandFreqRange(band)[0];
//int maior = equalizer.getBandFreqRange(band)[1];
//Log.i("kkk", "Menor = "+menor);
//Log.i("kkk", "Maior = "+maior);
} catch (IllegalArgumentException e) {
Log.i("kkk", "Bad parameter value");
} catch (UnsupportedOperationException e) {
Log.i("kkk", "get parameter() rejected");
} catch (IllegalStateException e) {
Log.i("kkk", "get parameter() called in wrong state");
}
}
}
The follow methods work normally:
getBandLevelRange();
getNumberOfBands();
The follow methods don't work. I get the message error: Bad parameter
value.
getBand();
getBandFreqRange(); this last is commented, but when it isn't also don't work.
In the getBand() method I already tried several parameters values like: 50000, 1000000. Frequency supported at example of the library AudioFx.
Why don't these methods work??? Did I forgive to setup anything???
tks.

Related

3 audios at the same time with Android MediaPlayer

I am trying to play 3 audios width Android but not at the same time, my code is the next one:
for (Word w : wordsList){
String path = Parameters.PATH_AUDIO + "/" + w.getAudioPath();
Uri audioPath = Uri.parse(path);
MediaPlayer audio1 = MediaPlayer.create(context, audioPath);
audio1.start();
}
But all audios start to play at least at the same time. I need something like audio1.wait() or audio1.sleep or someone like this.
You might want to consider using setOnCompletionListener:
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
//do something when audio is finished
}});
Here's an example (didn't have the chance to try it though):
Decalre these in the class level:
int index = 0;
MediaPlayer audio1;
and then:
try {
audio1 = MediaPlayer.create(this, Uri.parse(yourList.get(index)));
audio1.start();
index++;
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
if (index < yourList.size()) {
audio1.reset();
audio1 = MediaPlayer.create(MainActivity.this,
Uri.parse(yourList.get(index)));
audio1.start();
index++;
}
else
{
//release it
}
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

how to disable sound in my game app?

Now i develop a game application and as all games on google play, they have a toggle button to switch on/off the sound.
Is there any technique i can use to disable all sounds in the game with one step or i have to disable every sound separately ??
I use MediaPlayer for playing sounds
private int playSound(int file) {
int duration = 1000;
sound = new MediaPlayer();
AssetFileDescriptor fd = getResources().openRawResourceFd(file);
try {
sound.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
sound.prepare();
sound.start();
sound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// Do the work after completion of audio
Toast.makeText(GameActivity.this, "good", Toast.LENGTH_SHORT).show();
mp.release();
}
});
duration = sound.getDuration();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return duration;
}
try this
media.setVolume(0,0);

Change audio being played with delay android

I am trying to run audios one after another with a time gap of 5 seconds. However I dont really see that happening. My third audio in the list is played and others are skipped. This means while debugging only music3 is being played and others are not. I would love alternate methods of doing this. Moreover I used prepare method just like that. Makes no difference while running though.
for(int j=0;j<3;j++)
{
if(j==0)
{
xnp = MediaPlayer.create(this,R.raw.ticktock);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
}
if(j==1)
{
xnp = MediaPlayer.create(this,R.raw.music2);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
}
else if (j==2)
{
xnp = MediaPlayer.create(this,R.raw.music3);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
intenter();
}
}
Your current implementation does not work because the media playback is not done on the main thread, so you are immediately playing all 3 media resources, so only the third one is heard.
I think you might want to call MediaPlayer#setOnCompletionListener on your MediaPlayer object. In the completion listener, you can then postDelayed onto your Handler to queue up the next resource to play.

Get mediaplayer service screen from service

I have a media player service that plays a music in background, and this service is called from an activity. Now when i ex. exit from that activity, and get back to it again, i want to see what is the status, what song is played etc.
Here is the code from my service:
public class MediaPlayerService extends Service implements
OnCompletionListener, OnClickListener {
MediaPlayer mediaPlayer;
String url;
int mediaPos, mediaMax, position;
boolean isShufle;
WeakReference<SeekBar> seekbar;
WeakReference<TextView> reciter, songTitle, songPos;
private WeakReference<ImageView> play, forward, backward;
ArrayList<Songs> songs;
Handler handler;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// mediaPlayer = MediaPlayer.create(this,url);// raw/s.mp3
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
private void play(final int position) throws IllegalArgumentException,
SecurityException, IllegalStateException, IOException {
/*
* dialog = ProgressDialog.show(AlbumDetails.this, "",
* getString(com.darkovski.quran.R.string.buffering), true);
* dialog.setCancelable(true); dialog.show();
*/
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(songs.get(position).getLink());
// prepare track
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
play.get().setImageResource(R.drawable.pause);
// play.setImageResource(R.drawable.pause);
// this is new
mediaPos = mp.getCurrentPosition();
mediaMax = mp.getDuration();
reciter.get().setText(songs.get(position).getRecitorName());
songTitle.get().setText(songs.get(position).getTitle());
songPos.get().setText((position + 1) + " of 114");
seekbar.get().setMax(mediaMax);
seekbar.get().setProgress(mediaPos);
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 100);
mp.start();
// dialog.dismiss();
}
});
// when truck finishes
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// if(shuffle)
try {
if (isShufle) {
play(new Random().nextInt(songs.size()));
} else {
if (position == songs.size())
play(0);
else
play(position + 1);
}
AlbumDetails.position += 1;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
url = intent.getStringExtra("url");
handler = new Handler();
position = intent.getIntExtra("position", -1);
forward = new WeakReference<ImageView>(AlbumDetails.forward);
backward = new WeakReference<ImageView>(AlbumDetails.backward);
play = new WeakReference<ImageView>(AlbumDetails.play);
play.get().setOnClickListener(this);
forward.get().setOnClickListener(this);
backward.get().setOnClickListener(this);
seekbar = new WeakReference<SeekBar>(AlbumDetails.seekbar);
reciter = new WeakReference<TextView>(AlbumDetails.reciter);
songTitle = new WeakReference<TextView>(AlbumDetails.songTitle);
songPos = new WeakReference<TextView>(AlbumDetails.songPos);
songs = AlbumDetails.songs;
seekbar.get().setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser && mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(progress);
}
}
});
/*
* try { mediaPlayer.setDataSource(url); mediaPlayer.prepare();
* mediaPlayer.setOnCompletionListener(this); } catch
* (IllegalArgumentException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } catch (SecurityException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } catch
* (IllegalStateException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); } if (!mediaPlayer.isPlaying()) {
* mediaPlayer.start(); }
*/
try {
play(position);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if (mediaPlayer.isPlaying()) {
int mediaPos_new = mediaPlayer.getCurrentPosition();
int mediaMax_new = mediaPlayer.getDuration();
seekbar.get().setMax(mediaMax_new);
seekbar.get().setProgress(mediaPos_new);
handler.postDelayed(this, 100);
}
}
};
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playeer_back:
try {
if (mediaPlayer != null)
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
if (position - 1 < 0) {
play(songs.size());
position = songs.size();
} else
play(position - 1);
} catch (IllegalArgumentException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IllegalStateException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
break;
case R.id.playeer_forward:
try {
if (mediaPlayer != null)
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
play(position + 1);
position += 1;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.playeer_play:
if (mediaPlayer == null) {
try {
play(position);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
play.get().setImageResource(R.drawable.pause);
} else if (mediaPlayer != null) {
mediaPlayer.start();
play.get().setImageResource(R.drawable.play_play);
}
// playFunction();
break;
}
}
}
And my activity code - onClick:
case R.id.playeer_play:
playbackServiceIntent
.putExtra("url", songs.get(position).getLink());
playbackServiceIntent
.putExtra("position", position);
startService(playbackServiceIntent);
break;
There are different methods to realize it.
Using LocalBroadcastManager
Using an application to track the status of your service, i.e Song name, length, ...
Using SharedPreferences
IMHO, I think using a LocalBroadcastManager is more cleaner.
- Send a broadcast message to the service
- When the service receives the message, let it broadcast its status.

TextureView onSurfaceTextureAvailable never called

I try to play a video for a splash Activity when my Android app starts and I have the problem that onSurfaceTextureAvailable interface method never get called.
Here is the code:
public class HomeActivity extends Activity implements TextureView.SurfaceTextureListener,
MediaPlayer.OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
private MediaPlayer videoMediaPlayer;
private TextureView videoPreview;
private Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videoPreview = new TextureView(this);
videoPreview.setSurfaceTextureListener(this);
extras = getIntent().getExtras();
setContentView(videoPreview);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
Log.d("HOME", "onSurfaceTextureAvailable");
try {
videoMediaPlayer= new MediaPlayer();
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.intro_video);
videoMediaPlayer.setDataSource(getApplicationContext(), video);
videoMediaPlayer.setSurface(s);
videoMediaPlayer.prepare();
videoMediaPlayer.setOnBufferingUpdateListener(this);
videoMediaPlayer.setOnCompletionListener(this);
videoMediaPlayer.setOnPreparedListener(this);
videoMediaPlayer.setOnVideoSizeChangedListener(this);
videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
videoMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Other stuff
The code is from stackoverflow similar question which it seems that he got it working like this.
Any advice?
to onSurfaceTextureAvailable() be called, hardware acceleration must be enabled. you can do this in AndroidManifast.xml file
<activity
android:name="com.example.HomeActivity"
android:hardwareAccelerated="true" >
</activity>

Categories

Resources