I set in MediaPlayer source as url with remote mp3 file. I want listen, where start playing song.
At start some time need to download mp3 header (caching) and only after start playing song. How I can know about this moment? I know only simple way: check at timer isPlaying status...
If you use Android native MediaPlayer, there's an event called onPrepare which is called when the mediaplayer is ready to play your music
For example how to catch the above event on VideoView.
video = new VideoView(activity);
video.setOnPreparedListener(new OnPreparedListener() {
}
Related
I am trying to play two video views using the same media controller but it seems impossible to play them and stop them and seek through both at the same time
can someone suggest a solution here is what I am trying :
//2 video views
videoview=(VideoView)findviewbyid(R.id.videowview);
videoviewtwo=(VideoView)findviewbyid(R.id.videowview2);
//the media controller
MediaController mc =new MediaController(this);
//setting uri for both:
videoview.setVideoURI(uri);
videoviewtwo.setVideoURI(uri1);
//setting same media controller object for both
videoview.setMediaController(mc);
videoviewtwo.setMediaController(mc);
//finally start both
videoview.Start();
videoviewtwo.Start();
after this both play well at the first time but when I try to repeat only one starts the other doesnt, thank you for your help.
Edit: I want them both to play at the same time each time I repeat the process .
You need to release and set the mediaplayer to null before you can start the new video. Try to call this when the video has stopped and before you start a new video so you know that the mediaplayer is not busy.
private void releaseMediaPlayer() {
mMediaPlayer.release();
mMediaPlayer = null;
}
So next is to find out when the mediaplayer have stopped playing your video. A hint: MediaPlayer.OnCompletionListener
I created an app in which I am using Android's mediaplayer to stream multiple video files from a HTTP source. I am using the Prepare() method instead of prepareAsync() since I can't continue unless something is being shown.
Here I have a simple method that return each of the mediaplayer instances:
MediaPlayer mediaPreparation(String filename) {
String url = "myURL"; // your URL here
// create mediaplayer instance
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.setLooping(true);
// mediaPlayer.start();
return mediaPlayer;
}
And then I start them one by one using start(). Now I have two questions:
1- How does Prepare() really work? It takes a while for my app to start. Isn't it supposed to first receive a portion of the video stream (not download the whole) and then start playing?!
2- Can I manually buffer the video so I have more control over the buffer, and feed it into a mediaplayer to play? In specific, given a condition I need to start the video from a specific time. So probably I need to buffer the video stream from a specific time of the video.
I have to 2 songs.I used radio buttons for both songs so when select first it should play me the first song and when i click on second. Second song should be played.I have used Play,pause and stop button so when i select first song and click play first song should be played.how to use media player for 2 songs.Previously i used 2 media players for different songs. How to use one media player.
Previously I have used this statements for two songs
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
mediaPlayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.rocky);
My question is i want to use only one media player for both songs
You better stop being lazy and search for the solution on your own aswell. #1Up pretty much answered your question. For your second question: This is Uri
Keep using two mediaplayers, in cases like this there is no problem doing that. Using only one reference would mean you'd have to recreate it each time you want to change clip, or stopping it and calling setDataSource(context, URI).
If you use only one reference to mediaplayer the user will have to wait for the clip to be ready each time it has to be played, while in your implementation both sound clips are ready to be played at any time.
Anyways, here is a setDataSource example:
MediaPlayer mp = MediaPlayer.create(context, firstSongUriOrRes);
public void play(int clip)
{
if(mp.isPlaying()) //Stop the mediaplayer if it's already playing
mp.stop();
switch(clip) //Choose the clip to be played
{
case 0:
mp.setDataSource(context, firstSongUriOrRes);
break;
case 1:
mp.setDataSource(context, secondSongUriOrRes);
break;
}
mp.prepare();
mp.start(); //Start the mediaplayer
}
Another way to use setDataSource is to place the audio files inside the asset directory and use this code:
AssetFileDescriptor fd = context.getAssets().openFd("pathInsideAssets/fileName");
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
I'm developing an android application which is collection of 100 sound effects.
After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop() method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer instance using the static create() method which means you create a new MediaPlayer object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.
I need to create only one media player object Mediaplayer mp = new Mediaplayer();
Using this I need to play multiple audio files one after the other for that i am using handler
and getting the duration.
If i create multiple media player objects it shows error(1, -17)
I also need to display images related to audio files.
Did you rule out SoundPool?
http://developer.android.com/reference/android/media/SoundPool.html
It typically good for short audio clips.
Implement the OnCompletionListener and register it with your MediaPlayer instance.
After the media has been played out it will call this callback method onCompletion
void onCompletion(MediaPlayer mp){
//Here you stop it.
mp.stop();
//Reset the data source path to the new file
mp.setDataSource(<uri>);
mp.prepare(); // or mp.prepareAsync();
// start the mediaplayer after the prepare has completed.
}
Release the mediaplayer instance once you are done playing all the files.