How to implement release function in soundpool? - android

hy,,im newbie for android,i have error
AudioFlinger could not create track, status: -12 when running my application. i read for use release() method for fix it, but i don't know how to implement on my code, please help me ?? this is my code,please fix it :)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.talempong);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.doo, 1);
ImageButton Button = (ImageButton)findViewById(R.id.imagebutton1);
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound();
}
});
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID2 = spool2.load(this, R.raw.re, 1);
ImageButton Button2 = (ImageButton)findViewById(R.id.imagebutton2);
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound2();
}
});

Is your sound greater that 1MB? SoudPool doesn't plays sounds that are greater that that.
If it isn't you can use the release after playing the sound.
// your code:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.doo, 1);
//after this you have to:
spool.setOnLoadCompleteListener(new OnLoadCompleteListener(){
#Override
public void onLoadComplete(SoundPool sound,int sampleId,int status){
spool.play(teste, 20,20, 1, 0, 1);
new Thread(new Runnable(){
#Override
public void run(){
try {
Thread.sleep(2000);
spool.release();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}).start();
}
});
// in the Thread.sleep put the value in millisecs of the time of your music this is a sample os 2 seconds

Related

MediaPlayer How to start playing from the fifth second of the video?

I want to skip the video's title to play,But I don't know how to do it.If you know, please tell me, thank you.
MediaPlayer myMediaPlayer = new MediaPlayer();
myMediaPlayer.setDataSource(this, Uri.parse(uri));
holder = surfaceView.getHolder();
holder.addCallback(new MyCallBack());
myMediaPlayer.prepare();
mTimer = new Timer();
mTimerTask = new TimerTask() {
#Override
public void run() {
if (isChanging == true) {
return;
}
play_bar.setProgress(myMediaPlayer.getCurrentPosition());
}
};
mTimer.schedule(mTimerTask, 0, 100);
totale_time.setText(getTime(myMediaPlayer.getDuration() / 1000));
myMediaPlayer.start();
play_bar.setMax(myMediaPlayer.getDuration());
myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
PlayVideoScreen.this.finish();
}
});
Please use mediyaPlayer.seekTo(milliseconds).
Use MediaPlayer#seekTo(int msec) to seek to specified position you wanna set

can't start the MediaPlayer again after stop method of MediaPlayeris called

In this application I have 2 buttons one is for start the media player and another one is for stop the media player ,But in my case stat and stop is working fine .
I can't start the media player once the stop button is clicked and also I got
start called in state 0, mPlayer(0x9f5d8300)
error (-38, 0)
I tried some solutions from stackoverflow but nothing is helped .
Coding :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
can anyone help me to fix it .
If I read the doc they say you need to call prepare() method to reset your player.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
player.prepare();
}
});
}
you need to do
player.release();
after stopping media player

Multiple audio tracks in the same activity

I'm creating an audio playback application. I've currently just got two tracks added but eventually I need to add many more... I've currently created two different mediaplayers to run both the tracks, however as the number of tracks will increase using this procedure will lead to problems and lack of performance!
Is there some way I can code this better and more generically?
Here's the java code
public class ChantsFragment extends Fragment {
TextView text1, text2
ImageButton play1, play2, pause1, pause2, repeatoff1, repeatoff2, repeaton1, repeaton2, stop1, stop2;
MediaPlayer mediaPlayer1, mediaPlayer2;
Toast on, off;
public ChantsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chants, container, false);
play1 = (ImageButton) view.findViewById(R.id.play1);
play2 = (ImageButton) view.findViewById(R.id.play2);
pause1 = (ImageButton) view.findViewById(R.id.pause1);
pause2 = (ImageButton) view.findViewById(R.id.pause2);
repeatoff1 = (ImageButton) view.findViewById(R.id.repeatoff1);
repeatoff2 = (ImageButton) view.findViewById(R.id.repeatoff2);
repeaton1 = (ImageButton) view.findViewById(R.id.repeaton1);
repeaton2 = (ImageButton) view.findViewById(R.id.repeaton2);
stop1 = (ImageButton) view.findViewById(R.id.stop1);
stop2 = (ImageButton) view.findViewById(R.id.stop2);
mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);
on = Toast.makeText(getActivity(), "The chant will repeat itself", Toast.LENGTH_LONG);
off = Toast.makeText(getActivity(), "The chant will no longer repeat itself", Toast.LENGTH_LONG);
//Track 1
text1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
text1.setClickable(false);
}
if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
mediaPlayer2.stop();
mediaPlayer2.release();
mediaPlayer2 = null;
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
if (mediaPlayer1 == null) {
mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
} else {
text1.setClickable(true);
mediaPlayer1.start();
play1.setVisibility(View.GONE);
pause1.setVisibility(View.VISIBLE);
repeatoff1.setVisibility(View.VISIBLE);
stop1.setVisibility(View.VISIBLE);
}
mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
});
}
});
play1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play1.setVisibility(View.GONE);
pause1.setVisibility(View.VISIBLE);
mediaPlayer1.start();
}
});
pause1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play1.setVisibility(View.VISIBLE);
pause1.setVisibility(View.GONE);
mediaPlayer1.pause();
}
});
repeatoff1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.setLooping(true);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.VISIBLE);
on.show();
off.cancel();
}
});
repeaton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.setLooping(false);
repeaton1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.VISIBLE);
off.show();
on.cancel();
}
});
stop1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.stop();
mediaPlayer1 = null;
text1.setClickable(true);
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
});
//Track 2
text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
text2.setClickable(false);
}
if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
mediaPlayer1.stop();
mediaPlayer1.release();
mediaPlayer1 = null;
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
if (mediaPlayer2 == null) {
mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);
} else {
text2.setClickable(true);
mediaPlayer2.start();
play2.setVisibility(View.GONE);
pause2.setVisibility(View.VISIBLE);
repeatoff2.setVisibility(View.VISIBLE);
stop2.setVisibility(View.VISIBLE);
}
mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
});
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play2.setVisibility(View.GONE);
pause2.setVisibility(View.VISIBLE);
mediaPlayer2.start();
}
});
pause2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play2.setVisibility(View.VISIBLE);
pause2.setVisibility(View.GONE);
mediaPlayer2.pause();
}
});
repeatoff2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.setLooping(true);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.VISIBLE);
on.show();
off.cancel();
}
});
repeaton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.setLooping(false);
repeaton2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.VISIBLE);
off.show();
on.cancel();
}
});
stop2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.stop();
mediaPlayer2 = null;
text2.setClickable(true);
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
});
return view;
}
Edit
I've currently zeroed down on adding around 10 tracks to the application. I want playback to stop on the current track (if playing) the moment another track is selected and using media player to do the same would mean checking separately if 9 media players are playing! (Unless of course there's another way to do this which I've completely missed)
Well, for playing multiple audio files at once, SoundPool seems to be your best choice mainly because its much more lightweight and you can control the number of streams. You can create a new SoundPool instance as:
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100); // 10 = maxStreams, 100 = quality
You can then start loading different sounds into it as:
int soundId1 = soundPool.load(context, R.raw.audio1, 1);
int soundId2 = soundPool.load(context, R.raw.audio2, 1);
...
You can control playback as:
int idBeingPlayed = soundPool.play (soundID, 1, 1, 1, -1, 1); // volume params, returns the streamId
Other controls:
soundPool.pause(streamID);
soundPool.resume(streamID);
For more info, visit the offfical docs. Hope this helps!

Soundpool different streamid

I have soundpool that will load file from sd-card and I used mstreamID to stop it and I use mstreamID in OnLoadCompleteListener so the files can load. I use 2 buttons to play soundpool diffrent sounds. But when I click first button and while it plays 1 sound I click second button and it stops the sound from first button.
Here is my code:
public class MainActivity extends Settings {
SoundPool mSoundPool;
int mSoundId;
int mStreamId = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
mSoundPool
.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool,
int sampleId, int status) {
mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f);
}
});
}
//OnClick
public void button1(View view) {
if (mStreamId != 0) {
mSoundPool.stop(mStreamId);
}
// et1 is extended from Settings.java
String path = getFullFilePath(getApplicationContext(), et1.getText()
.toString());
mSoundId = mSoundPool.load(path, 1);
}
public void button2(View view) {
if (mStreamId != 0) {
mSoundPool.stop(mStreamId);
}
String path = getFullFilePath(getApplicationContext(), et2.getText()
.toString());
mSoundId = mSoundPool.load(path, 1);
}
Read doc please, and what is the thing you want to say?
do not load same file more than one time if you not unload it.
load all sound at start when use SoundPool.
streamId is increasing all the time, you never get same streamId.

Play Sequence of audio files with SoundPool class of Android

I have three sound files in raw folder. I want to play them one after another with no delay between them. I tried them playing with below code but all three files are playing together, not one after another. All three files are less then 50 kb. I also want to change their playback rate, that's why I am using SoundPool instead of MediaPlayer.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
final int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPoolMap.put(1, mSoundPool.load(this, R.raw.nancy, 1));
mSoundPoolMap.put(2, mSoundPool.load(this, R.raw.cymbal, 1));
mSoundPoolMap.put(3, mSoundPool.load(this, R.raw.njs, 1));
Button SoundButton = (Button)findViewById(R.id.Button);
SoundButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 0, 1f);
mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);
mSoundPool.play(mSoundPoolMap.get(3), streamVolume, streamVolume, 1, 0, 1f);
}
});
In order to do what you're trying to do, you'll probably have to create your own custom AsyncTask that takes in an array of SoundPool objects (or perhaps a HashMap), the implementation along the lines of:
private class SoundPoolTask extends AsyncTask <Soundpool, String, String>
{
#Override
protected String doInBackground(SoundPool... params) {
try {
//For each SoundPool object
// int soundLength = however long the SoundPool object is
// Play the sound
// Thread.sleep(soundLength);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(String... values) {}
#Override
protected void onPostExecute(String result) {
//Clean up your class objects
}
}
You'll probably have to do some sort of calculation with the original length of the sound and the playrate to determine how long each sound is. I hope this helps!
I got same problem, so I create my own logic. Her it is:-
I have three audio file in my raw folder. I want to run these three file in sequence. To do this I follow this flow:-
Step1:- On clicking Button a thread will be generated.
Step2:- Thread will start a timer of 20 milliseconds.
Step3:- In timer it will check if media player is not playing.
If media player is not playing it will call a function to start media player.
Step4:- In function which will start the media player have switch logic which will decide which audio
file will be ON ( with help of a variable v).
Here is my code:-
public class MainActivity extends Activity {
int v=0;
TimerTask time;
MediaPlayer mp ;
Button btn;
void playmp(int a)
{
switch (a)
{
case 0:
{
mp = MediaPlayer.create(this,R.raw.a);
mp.start();
v++;
break;
}
case 1:
{
mp = MediaPlayer.create(this,R.raw.b);
mp.start();
v++;
break;
}
case 2:
{
mp = MediaPlayer.create(this,R.raw.c);
mp.start();
v++;
break;
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn= (Button) findViewById(R.id.button1);
mp = MediaPlayer.create(this,R.raw.a);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Creating Thread
Thread thread = new Thread()
{
#Override
public void run() {
try {
sleep(1);
// Body Of Timer
time = new TimerTask() {
#Override
public void run() {
//Perform background work here
if(!mp.isPlaying())
{
playmp(v);
}
}
};
//Starting Timer
Timer timer = new Timer();
timer.schedule(time, 10, 20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}
where "a","b"and "c" are my audio file name . If you want repeat audio after completing all three , replace this:-
case 2:
{
mp = MediaPlayer.create(this,R.raw.c);
mp.start();
v++;
break;
}
by this
case 2:
{
mp = MediaPlayer.create(this,R.raw.c);
mp.start();
v=0;
break;
}

Categories

Resources