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;
}
Related
I wrote my own MediaPlayer class to play files at a specific path and to play files from the assets folder. Here is the class:
public class CMediaPlayer extends MediaPlayer{
public void play(String audioPath){
this.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
File f = new File(audioPath);
if(f.exists()){
try{
FileInputStream fis = new FileInputStream(f);
FileDescriptor fileD = fis.getFD();
this.setDataSource(fileD);
this.prepare();
}catch(IOException e){
}
this.start();
}
}
public void play(AssetFileDescriptor descriptor){
this.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
try {
this.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
this.prepare();
}catch (IOException e){
}
this.start();
}
}
I want to play several sounds with that class from an activity. Here is my code:
public class playGame extends Activity {
//a lot of variables
CMediaPlayer mediaPlayer; //declare my mediaplayer
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//release??????
Intent myIntent = new Intent(getBaseContext(), startView.class);
startActivity(myIntent);
}
return super.onKeyDown(keyCode, event);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_question2);
mediaPlayer = new CMediaPlayer(); //define my mediaplayer
//stuff
}
//more variables
public void playQuestion(File question){
//stuff
TextView myTextView = (TextView) findViewById(R.id.textViewQuestion);
//stuff
myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.play(pathSoundQuestion); //play sound when clicked
}
});
//stuff
myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.play(pathSoundQuestion); //play sound when clicked
}
});
//stuff
mediaPlayer.play(pathSoundQuestion); //plays sound immediatly, first played sound (works fine)
//button1
Button myButton = (Button) findViewById(R.id.button1);
//stuff
myButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
if(lastClickedButton == v){
//stuff
return;
}
//stuff
mediaPlayer.play(pathAudio1); //play sound when clicked (error)
}
});
//button2
myButton = (Button) findViewById(R.id.button2);
//stuff
myButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
if(lastClickedButton == v){
//stuff
return;
}
//stuff
mediaPlayer.play(pathAudio2); //play sound, same problem
}
});
//goes on like this some more times...
}
public void logIn(View v, String right){
//stuff
if(right.equals("true")){
//stuff
try {
AssetFileDescriptor descriptor = getAssets().openFd("Right");
mediaPlayer.play(descriptor); //play sound from assets
Intent myIntent = new Intent(getApplication(), playGame.class);
startActivity(myIntent);
}catch (IOException e){
}
}else{
//stuff
try {
AssetFileDescriptor descriptor = getAssets().openFd("Wrong");
mediaPlayer.play(descriptor); //play sound from assets
Intent myIntent = new Intent(getApplication(), playGame.class);
startActivity(myIntent);
}catch (IOException e){
}
}
}
}
The app plays the first sound as mentioned in my comments in my code. When I click a button which should start another sound I get the following error:
03-16 23:07:38.478 13646-13646/com.example.cello.myownquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1005)
Is my MediaPlayer class okay or am I missing something depending on the state? Is this class the only spot to call release or do I have to put it somewhere in my activity, too?
Does anybody see my mistake? This problem with the mediaPlayer took me a whole day of trying, hope you can help me...
change mp.release(); to mp.reset();
public void reset ()
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
public void release ()
Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.
You need to keep the object around.
You can do it in a simple way
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, ringtone);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MEDIA);
mediaPlayer.prepare();
mediaPlayer.start();
me too i have this problem but i have used:
public void play(String name){
try {
AssetFileDescriptor afd = getAssets().openFd(name);
if(myPlayer == null){
myPlayer = new MediaPlayer();
}
myPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
myPlayer.prepare();
myPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
and to stop:
public void stopPlayer(){
if(myPlayer!= null && myPlayer.isPlaying()){
myPlayer.stop();
myPlayer = null;
}else{
myPlayer = null;
}
}
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
My ProgressBar does not reset after audio is done. I ask a question before on this and got it working but now just stopped working and not sure why it doesn't. Any help would be awesome.
All I want is it to chose a audio at random then play one and when finished you can press play again to listen to the same audio it chose at random.
Heres code:
public class player1 extends Activity implements Runnable {
private MediaPlayer mp;
private ProgressBar progressBar;
private ImageButton pauseicon;
private final int NUM_SOUND_FILES = 3; //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****
private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_1);
pauseicon = (ImageButton) findViewById(R.id.pauseicon);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
getActionBar().setDisplayHomeAsUpEnabled(true);
mfile[0] = R.raw.sound01; //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
mfile[1] = R.raw.sound02; //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
mfile[2] = R.raw.sound03;
/**
* Play button click event
* plays a song and changes button to pause image
* pauses a song and changes button to play image
* */
try{
mp = MediaPlayer.create(player1.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
mp.seekTo(0);
mp.start(); ;
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(mp.getDuration());
new Thread(this).start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new OnCompletionListener() {
//When audio is done will change pause to play
public void onCompletion(MediaPlayer mp) {
pauseicon.setImageResource(R.drawable.playicon);
}
});
pauseicon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// No need to check if it is pauseicon
if(mp.isPlaying()){
mp.pause();
((ImageButton) v).setImageResource(R.drawable.playicon);
} else {
mp.start();
((ImageButton) v).setImageResource(R.drawable.pauseicon);
}}});
}
//To update progress bar
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<=total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
progressBar.setProgress(currentPosition);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
if (mp != null)
if(mp.isPlaying())
mp.stop();
mp.release();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed(){
if (mp != null){
if(mp.isPlaying())
mp.stop();
mp.release();
}
//there is no reason to call super.finish(); here
//call super.onBackPressed(); and it will finish that activity for you
super.onBackPressed();
}
}
You problem is mp.getDuration() is a milliseconds, which changes in each song. So don't use progressBar.setMax(mp.getDuration());. As it only works once for the very first song. Use progressBar.setMax(100); instead. Then use currentPosition= 100 * mp.getCurrentPosition() / mp.getDuration(); This should work fine.
Also don't forget adding progressBar.setProgress(0); when one song is finished or stop-button is clicked.
EDIT: Try resetting it inside of your completion listener. Scratch the setter then. Try using this:
Thread.sleep(1000);
currentPosition++;
Instead of this:
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
That is in your run() function. Then it will increment from 0 to total and you won't have to worry about getting or setting the currentposition, just the progress which is being done with the previous change below.
mp.setOnCompletionListener(new OnCompletionListener() {
//When audio is done will change pause to play
public void onCompletion(MediaPlayer mp) {
pauseicon.setImageResource(R.drawable.playicon);
//Reset here
progressBar.setProgress(0);
}
});
How can i get the value of repetitions completed from mp.setLooping(true). I can use OnCompletionListener to get number of repetitions, but it produces 1sec delay between looping, which is annoying in a music oriented app. OnCompletionListener looping is not smooth asmp.setLooping(true).
Any suggestions ? Thank in advance.
Code with 1sec delay or gap between looping:
public class MainActivity extends Activity {
MediaPlayer mp1;
Button play;
int maxCount=10,n=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button1);
mp = MediaPlayer.create(MainActivity.this, R.raw.soundfile);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpplay();
}
});
}
private void mpplay() {
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp1) {
if (n <= maxCount) {
mp1.start();
n++;
if (n >= maxCount) {
n = 1;
mp1.stop();
}
}
}
});
}}
Try this out:
MediaPlayer mp1;
double play_times = 10;
int sound_length;
Handler handler;
boolean sound_playing = false;
int times_played = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.home);
handler = new Handler();
mp1 = MediaPlayer.create(this, R.raw.soundfile);
sound_length = mp1.getDuration();
mpplay();
}
private void mpplay() {
times_played = 0;
mp1.setLooping(true);
mp1.start();
sound_playing = true;
handler.postDelayed(loop_stopper, (int)(sound_length*(play_times-.5)));
handler.postDelayed(counter, sound_length);
}
private Runnable loop_stopper = new Runnable() {
public void run() {
sound_playing = false;
mp1.setLooping(false);
}
};
private Runnable counter = new Runnable() {
public void run() {
if (sound_playing) {
times_played++;
handler.postDelayed(counter, sound_length);
}
}
};
handler.postDelayed(loop_stopper, (int)(sound_length*(play_times-.5)));
This line will stop the looping halfway through the last desired loop. Since this just means it won't continue looping, the sound file will still be fully played on the last repeat.
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.