I have started to learn android for 3 months and I have some problems with MediaPlayer.setDataSource
I want to get path of mp3 file in my raw directory, which is used for Media Player.
I have tried many ways but the app's still not working, even though the program doesn't crash or show problems. I have tried many solutions from other posts but it's still not working.
Here is my code:
String path = "android.resource://com.example.acer.appdemo/raw/emer2";
bleeding1.setDataSource(path);
bleeding1.prepareAsync();
bleeding1.start();
textView.setText(getString(R.string.Firstaid2));
count = 2;
The reason why I choose this, because I want to make a program that change audio every time I swipe left or right. So I want the program setDataSource again each time I swipe left or right, and the code above is one of my cases (The audio doesn't start everytimes I put a new path).
You have to reset the MediaPlayer (call bleeding1.reset()) before you can set a new data source.
See https://developer.android.com/reference/android/media/MediaPlayer.html for a helpful lifecyle diagram.
Related
I have a VideoView in android that correctly plays a single video. It handles video fine with respect to muting, looping, etc.
I want to be able to switch between videos, however. To that end, I have two uris. I set the initial video like this:
_videoView = findViewById(R.id.video);
_videoView.setVideoURI(firstURI);
_videoView.requestFocus();
_videoView.start();
When I press Button2, it switches to the second uri like so:
_videoView.setVideoURI(secondURI);
_videoView.start();
When I press Button 1, it switches back to the first:
_videoView.setVideoURI(firstURI);
_videoView.start();
That all works fine, but after switching the uri a few times (sometimes as few as three, sometimes as many as seven; three or four is typical), the app freezes. It simply stops responding. There's no error in the log, nothing on screen, and no memory leak that LeakCanary can find. It simply stops responding and never recovers. This happens every single time.
Is there something wrong with how I'm changing the videos for this VideoView? (And yes, I want to use a VideoView.)
First Check your video is available or not then stop this video.It's better for you
Currently your first video still available in _videoView and you are playing another on same.
if (videoView!=null){
_videoView.stopPlayback();
_videoView.setVideoURI(URL);
_videoView.requestFocus();
_videoView.start();
}else{
_videoView.setVideoURI(URL);
_videoView.requestFocus();
_videoView.start();
}
I am using Python Kivy for an Android Game. I want to play a sound on an event
sound = SoundLoader.load("sound.wav")
def onEvent():
sound.play()
, and It works. But now the problem: Of course an event can, and in my case will happen again before the sound is done playing from the last event. And as the sounds are based on a play/pause idea I am getting a problem playing multiple sounds of the same object at once. That can be solved like this for first:
onEvent():
SoundLoader.load("sound.wav").play()
As this creates a new object all the time, and so is able to play it the same time another event plays the sound. But the problem using this method is quite obvious, because the sound must be loaded everytime the event occurs, and so causes a delay until it's played.
Is there a more useful way to do this?
{ if you don't understand what I am talking about, or just don't see the problem, feel free to ask }
You can workaround this by loading multiple instances of the sound.
For instance
sounds = [SoundLoader.load("sound.wav") for _ in range(10)]
index = 0
and then
def play():
sounds[index].play()
index = (index + 1) % len(sounds)
The more sounds you load, the more instances you can have playing at the same time (in this example 10).
hi ive been working on my app and have found a hurdle in an activity i just cant seem to overcome. the activity is essentially a large novelty piano with 8 keys that play sounds there are 6 buttons along the side that change the picture of the notes and the notes themselves, this i accomplished :-) now i would like to add a background song to each different set of notes (instruments) only when i do the app crashes i have set the mediaplayer at the top (globally?) using MediaPlayer mp; MediaPlayer mp2; etc and im using this in my code to check if any music is playing, stop it, release it, and then play the piece i want,
if(mp!=null&&mp.isPlaying()){
mp.stop();
mp.release();
}
if(mp2!=null&&mp2.isPlaying()){
mp2.stop();
mp2.release();
}
if(mp3!=null&&mp3.isPlaying()){
mp3.stop();
mp3.release();
}
mp3 = MediaPlayer.create(MusicActivity.this, R.raw.snaps);
mp3.setLooping(true);
mp3.start();
this again works but going from one to another and then back crashes the app. is there something else i should be doing? or is just an out of memory error?
EDIT--
Thanks to ranjk89 for his help
changed to soundpools but they wont stop ranjk89 suggests referring to the stream id and not the sound id looking for a little more clarification if possible i have
farmback =0;
drumback =0;
at the top then its loaded in oncreate using
drumback = sp.load(this, R.raw.snaps,1);
farmback = sp.load(this, R.raw.oldmacdonaldbeta,1);
and then way down, in the same method i change the button icons, not the same method i change all my other sounds for my notes i call
sp.stop(drumback);
sp.play(farmback, 1, 1, 0, -1, 1);
in one and
sp.stop(farmback);
sp.play(drumback, 1, 1, 0, -1, 1);
in another but there are 6 different instruments in total that all need a different backing track which should stop when the instrument is changed and play the one associated to it so something like
if (sp !=null && sp.isplaying()){
sp.stop();
sp.play(dumback);
}
but obviously this is not possible any help appreciated
My Initial reaction is, you shouldn't be using multiple MediaPlayer at all in the first place.
I would suggest you to use SoundPool as you can load all the media(audio) initially, once they are all loaded you can play them at will. Plus you won't have multiple players which would downsize the complexity of your code.
But if you do want to use Media player,
See this. But be aware that once you release you will not be able to reuse that instance of MediaPlayer. Even if you are using multiple MediaPlayers, do not call release on them unless you are sure you don't want to use them any more. Use stop()/prepare() combination.
For application doing too much work on the Main Thread,
If you either use a SoundPool or only one MediaPlayer and still bump in to the same issue, Use AsyncTask. Android dev site has a very good tutorial on this
In my app I set a mediaplayer with with this instructions:
mpBackGround = MediaPlayer.create(context, R.raw.bg_music);
mpBackGround.setVolume(0.7f, 0.7f);
mpBackGround.setLooping(true);
mpBackGround.start();
but at the end of first loop I have a little pause between first cycle and second cycle; I also have cut silence spaces in the file (bg_music). With files of type .mid I have not problem but with mp3, wav and other I have this problem...and I have all mp3 files (not midi).
Is there another solution to solve this problem? thanks
just out of curiosity, is this little pause between the cycles roughly equivalent to the time it takes to seek from one end of the file to the other? It sounds like a performance issue related issue to me.
If thats the case, I would create two instances of media players, and handle the repeating my self. As in, .start() one when the other one finishes. Then you can "rewind" one while the other is playing.
I have some audio data (raw AAC) inside a byte array for playback. During playback, I need to get its volume/amplitude to draw (something like an audio wave when playing).
What I'm thinking now is to get the volume/amplitude of the current audio every 200 milliseconds and use that for drawing (using a canvas), but I'm not sure how to do that.
.
.
.
.
** 2011/07/13 add following **
Sorry just been delayed on other project until now.
What I tried is run the following codes in a thread, and playing my AAC audio.
a loop
{
// int v=audio.getStreamVolume(AudioManager.MODE_NORMAL);
// int v=audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int v=audio.getStreamVolume(AudioManager.STREAM_DTMF);
// Tried 3 settings above
Log.i(HiCardConstants.TAG, "Volume - "+v);
try{Thread.sleep(200);}
catch(InterruptedException ie){}
}
But only get a fixed value, not dynamic volume...
And I also found a class named Visualizer, but unfortunately, my target platform is Android 2.2 ... :-(
Any suggestions are welcome :-)
After days and nights, I found that an Android app project called ringdroid
can solve my problem.
It helps me to get an audio gain value array, so that I can use to to draw my sound wave.
BTW, as my experience, some .AMR or .MP3 can't be parsed correctly, due to too low bitrate...