I've been trying to find a good way to load a large amount of audio files.
The code below works fine, but I need to load even more files and that's when I run into issues.
It's a quiz and when the users choose the correct answer, a new sound should be played. I have to load 50+ sounds.
The current way I load the files:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
Context myActivity = getApplicationContext();
primSounds.add(spool.load(myActivity, R.raw.int_prim_c_mel, 1));
primSounds.add(spool.load(myActivity, R.raw.int_prim_d_mel, 1));
primSounds.add(spool.load(myActivity, R.raw.int_prim_e_mel, 1));
kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_c_mel, 1));
kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_d_mel, 1));
kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_e_mel, 1));
oktSounds.add(spool.load(myActivity, R.raw.int_okt_c_mel, 1));
oktSounds.add(spool.load(myActivity, R.raw.int_okt_d_mel, 1));
oktSounds.add(spool.load(myActivity, R.raw.int_okt_e_mel, 1));
...
The problem is that this way make the onCreate "too heavy"
Here you have a possible workaround.
But in your case, you'll have 50 sounds loaded at once at some point. This means that you'll have quite a LOT of memory in use.
Having to face a similar situation before myself ,i think you can use the following code:
SoundtrackManager.java
public void prepareSoundtrack(int soundtrackId, Context context) {
try {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundtrackId);
} catch (Exception e) {
mMediaPlayer.release();
e.printStackTrace();
}
}
public void play() {
if (mMediaPlayer != null) {
try {
mMediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
using:
mSoundTrackManager.prepareSoundtrack(R.raw.the_sound, mContext);
mSoundTrackManager.play();
You can call play() as long as the sound being played does not change.
The downside is having to create a MediaPlayer for each sound. But you wont have all of them on memory at the same time. Also, they are loaded/played instantly.
EDIT:
This could be improved by making a "pool" of MediaPlayer objects in the SoundtrackManager class. So, if one of the random sounds is needed and happens to be on that "pool", you can avoid the constant MediaPlayer re-setting.
Related
for last three weeks I have worked on a Media Player in Android.I am trying to find a solution of how can I make my Media Player to change the song when it's already playing one.
Here is my Listener on the RecyclerView
musicList.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, final int position) {
currentPosition = position;
if(!mediaPlayer.isPlaying()){
musicThread.start();
} else {
mediaPlayer.reset();
}
}
})
);
}
and my Thread is this:
final Thread musicThread = new Thread(new Runnable(){
#Override
public void run() {
try {
URL = getMusicURL(myDataset[currentPosition]);
try {
mediaPlayer.setDataSource(URL);
//mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.prepareAsync(); // prepare async to not block main thread
} catch (IOException e) {
e.printStackTrace();
Log.i("TEST","Eroare: "+e.getMessage());
}
} catch (StorageApiException e) {
e.printStackTrace();
Log.i("TEST","Eroare: "+e.getMessage());
}
}
});
I think you have a mess. First of all, you dont need a thread to play music, the own mediaplayer API does it for you when you call mediaPlayer.start(). However, you have to care about the time it takes to prepare the data source if you are for example streaming online music. For this, just use mediaPlayer.prepareAsync() and register a callback. When it has finished preparing, you can automatically start playing or do whatever you want.
If you want to change the data source, just follow the automaton map that you can find in MediaPlayer docs. Essentially, when the user selects another track, you register the call in your button listener, then reset the mediaPlayer, and recall all prepare, start... cycle again. By the way, it is advised to deploy all your mediaplayer code into a service so that it can keep playing even though the user has closed your activity.
I'm implementing an Android app to handle PCM sound data (for long sounds i.e. music tracks).
I decided to use the AudioTrack class to play the music, but it can't be placed in the same thread as the activity, because is blocking the whole app - and here is the question - should I put AudioTrack operations in a separate thread or in an async task (or any other option)? What would be the best way?
I need to play / pause / stop and change music files.
Now I'm trying to manage it by a java thread and its almost ok - playing is ok, pausing almost (its "dropping" a part of the sound), but when I'm changing the music file, the previous isn't stopping and theres a mixed output (I think theres something wrong with my thread implementation).
And - no, I can't use the MediaPlayer in this app (I want to modify the PCM data on the fly).
I was searching for some help in google and here on stackoverflow, but nothing helped me.
Here is my current implementation, if somebody want to take a look.
public class AudioPlayerManager {
private AudioTrack track;
private Thread thread;
private AudioTrackThread trackThread = new AudioTrackThread();
public int getPlayState() {
return track.getPlayState();
}
public int getAudioSession() {
return track.getAudioSessionId();
}
class AudioTrackThread implements Runnable {
private String filePath = "/music/wav/musicfile.wav";
#Override
public void run() {
try {
playWaveFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void playWaveFile() throws FileNotFoundException {
int minSize = AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT, minSize,
AudioTrack.MODE_STREAM);
File file = new File(FileListingUtils.getExternalStorageRootFile(),
filePath);
InputStream is = new FileInputStream(file);
byte[] music = new byte[512];
track.play();
try {
handleSound(is, music);
} catch (IOException e) {
e.printStackTrace();
}
track.stop();
track.release();
}
private void handleSound(InputStream is, byte[] music)
throws IOException {
int i;
while ((i = is.read(music)) != -1) {
track.write(music, 0, i);
}
}
}
public void playNew() {
if (track != null) {
track.stop();
//track.flush();
track.release();
}
thread = new Thread(trackThread);
thread.start();
}
public void playOrResume() {
this.track.play();
}
public void pause() {
this.track.pause();
}
}
Thanks for every help!
M.
Your thread life cycle doesnt seem to be right. Firstly You will have to close(interrupt)your current thread every time playback is stopped or finished and create a new instance of the thread and close the old thread.
Secondly, you are calling track.stop()from outside your thread. You must implement a public stop method inside your thread and then insert finalisation code there and then call yourthread.stop() in your playNew() method.
so here is the pseudocode:
public void playNew() {
//the following if clause should go to thread's stop() method.
// if (track != null) {
// track.stop();
//track.flush();
// track.release();
// }
//create a new thread if its null
if(thread ==null){
thread = new Thread(new AudioTrackThread());
thread.start();}
//stop the current thread
else{
thread.stop();
thread=null;}
}
about your implementation, somewhere i read that to play music, a service and a thread will be a better option.Read about services and threads in android documentation.
I would like to play sound after touching the button. MediaPlayer works fine, but I read somewhere that this library is for long .wav (like music).
Is there any better way to play short .wav(2-3 sec.)?
The SoundPool is the correct class for this. The below code is an example of how to use it. It is also the code I use in several apps of mine to manage the sounds. You can have as may sounds as you like (or as memory permits).
public class SoundPoolPlayer {
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();
public SoundPoolPlayer(Context pContext)
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSounds.put(R.raw.<sound_1_name>, this.mShortPlayer.load(pContext, R.raw.<sound_1_name>, 1));
mSounds.put(R.raw.<sound_2_name>, this.mShortPlayer.load(pContext, R.raw.<sound_2_name>, 1));
}
public void playShortResource(int piResource) {
int iSoundId = (Integer) mSounds.get(piResource);
this.mShortPlayer.play(iSoundId, 0.99f, 0.99f, 0, 0, 1);
}
// Cleanup
public void release() {
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}
You would use this by calling:
SoundPoolPlayer sound = new SoundPoolPlayer(this);
in your Activity's onCreate() (or anytime after it). After that, to play a sound simple call:
sound.playShortResource(R.raw.<sound_name>);
Finally, once you're done with the sounds, call:
sound.release();
to free up resources.
I have a list of songs that I'm streaming using the MediaPlayer. Some of the songs consistently work and others consistently do not work. I can't see a difference between these files, and they seem to play fine in itunes and such.
When the songs fail it is throwing an IllegalStateException on the mediaPlayer.prepare() line. The IllegalStateException that is thrown has no useful info in it, (detailMessage is null, stackState is null)
Here is my code
try {
mediaPlayer.setDataSource(media.url);
setPlayerState(PlayerState.PREPARING);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
Here is a url to the file that does NOT work:
skdy.bryceb.dev.mediarain.com/song.m4a
Here is one that DOES work:
skdy.bryceb.dev.mediarain.com/song2.m4a
Any ideas why this works on some songs and fails on others?
Thanks MisterSquonk I'm sure that way would work.
In my particular case after beating my head against the wall for a while I realized that on some songs, I was getting to the buffered amount before the player state was getting set to prepared. So I added a check to make sure that the MediaPlayer was in the "PREPARED" state and then it worked great:
// Media prepared listener
mediaPlayer.setOnPreparedListener(
new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
setPlayerState(PlayerState.PREPARED);
}
});
// Media buffer listener
mediaPlayer.setOnBufferingUpdateListener(
new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Sometimes the song will finish playing before the 100% loaded in has been
// dispatched, which result in the song playing again, so check to see if the
// song has completed first
if(getPlayerState() == PlayerState.COMPLETED)
return;
if(getPlayerState() == PlayerState.PAUSED)
return;
// If the music isn't already playing, and the buffer has been reached
if(!mediaPlayer.isPlaying() && percent > PERCENT_BUFFER) {
if(getPlayerState() == PlayerState.PREPARED)
{
mediaPlayer.start();
setPlayerState(PlayerState.PLAYING);
}
//if it isn't prepared, then we'll wait till the next buffering
//update
return;
}
}
});
OK, I hacked together a minimal Mediaplayer implementation in a 'sandbox' app/activity I always keep spare for testing.
I might be wrong but if you're streaming these songs over the net, you'll need to prefix the url with http://.
I tried the urls with Winamp and Chrome verbatim (no protocol prefix string) and they worked fine although it's likely both of those applications will use some form of intelligence to work out how to connect/stream.
If I tried that in my mediaPlayer code, I get the same exception as you but if I prefix the urls with http:// the songs play fine.
Example...
// Activity scope
Button button;
CheckBox checkBox;
String url = "";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//button declared in my activity
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
if (!checkBox.isChecked())
url = getString(R.string.url_song1);
else
url = getString(R.string.url_song2);
mediaPlayer = new MediaPlayer();
}
#Override
public void onClick(View arg0) {
try {
Log.i(TAG, "onClick() entered...");
mediaPlayer.setDataSource(url);
Log.i(TAG, "Preparing mediaplayer...");
mediaPlayer.prepare();
Log.i(TAG, "Starting mediaplayer...");
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
}
If I copy the songs to my SD card both play fine and as long as the internet url strings have an 'http://' prefix then they also work.
I have a small (200kb) mp3 in the res/raw folder of my android app. I am trying to run it in an emulator from Eclipse. It is recognized as a resource in the R file but when I try to prepare/start, my activity crashes! Was there something else I needed to change, perhaps in the manifest?
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
try {
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
// handle this later
}
When starting the activity i.e on onCreate put the following code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
}
When stopping the activity i.e on onDestroy put the following code.
public void onDestroy() {
mPlayer.stop();
super.onDestroy();
}
Hope it helps :)
You'll likely prefer to use the SoundPool class. It reduces latency when it's time to play the sound, and offers other niceties like being able to prioritise sounds when there are too many to play at once.
From the docs:
A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.
For example:
/**
* How many sounds can be played at once.
*/
private static final int MAX_SOUND_POOL_STREAMS = 4;
/**
* Modify this as part of your own priority scheme. Higher numbers mean higher
* priority. If you don't care, it's okay to use the same priority for every
* sound.
*/
private static final int NORMAL_PRIORITY = 10;
private int mySoundId;
#Override
public void setupContent() {
this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,
AudioManager.STREAM_MUSIC, 100);
this.mySoundId = this.soundPool.load(this.getApplicationContext(),
R.raw.mySound, 1);
}
#Override
private void playMySound() {
this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);
}
this is a static method I use in my projects.
I add it to my Utils class:
public static void playSound(final Context context, final SoundType type)
{
new Thread(new Runnable()
{
#Override
public void run()
{
MediaPlayer mediaPlayer = new MediaPlayer();
int resId = -1;
switch (type)
{
case INCOMING_NOTIFICATION:
resId=R.raw.noti_sound;
break;
case SEND_BETTING_SLIP:
resId=R.raw.slip_sent;
break;
case TRIVIA_RIGHT_ANSWER:
resId=R.raw.game_bonus;
break;
case TRIVIA_WRONG_ANSWER:
resId=R.raw.whistle_referee_trivia_bad_answer;
break;
}
if (resId != -1)
{
mediaPlayer = MediaPlayer.create(context, resId);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.start();
while (mediaPlayer.isPlaying() == true)
{
}
}
}
}).start();
}
}
now I defind an Enum (SoundType) and placed the mp3 files in raw folder under
res folder.