Hi
How to play media file in an application.I am trying it with the following code but do not know why it is not working for me
player= new MediaPlayer().create(context, R.raw.lonely);
player.start();
player.release();
Help me.
Thanks in advance.
I haven't played with MediaPlayer, but I would try without the release() call. This example doesn't use it. And the docs say it's a cleanup method to be called after the playback:
Releases resources associated with
this MediaPlayer object. It is
considered good practice to call this
method when you're done using the
MediaPlayer.
I think you're messing up the with constructors. You can instantiate the MediaPlayer statically: MediaPlayer.create(Context context, int resid), which is the easiest way, cause you just need to call play(). Also you need a valid context, it's to say, if you're creating rhe MediaPlayer within an Activity or a Service, just pass "this" as a context.
You can also use the "normal" constructor MediaPlayer(), but then you would have to explicity call to setDataSource() and prepare() before play().
Besides, as Grzegorz wrote, calling release() just after play() is not a good idea.
you can folow this sample :
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path+"/"+fileName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
Related
I'd like to switch the video method in MediaPlayer to TextureView's Surface at some point during playback. But all I get is a black screen (the sound is playing). I made sure the size of the TextureView is ok, so it's not the case.
If I set the Surface of TextureView before calling prepare on MediaPlayer, than the video is shown.
So it seems that the order of calling prepare and applying the surface is crucial.
What's interesting, in case of SurfaceView everything works fine (no matter when I set the holder of SurfaceView: before or after the preparing is done).
Is there a way to set TextureView to MediaPlayer during playback? Calling MediaPlayer.release() and recreating it again while having to take care of progress would be very inconvenient.
#Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
s = new Surface(surface);
Thread play = new Thread(new Runnable() {
#Override
public void run() {
try {
player = new MediaPlayer();
player.setSurface(s);
player.setDataSource(vidAddress);
player.prepare();
player.setOnPreparedListener(MainActivity.this);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(MainActivity.this);
player.setScreenOnWhilePlaying(true);
Log.e("mediaplayer", "true");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
play.start();
}
And your onPrepared listener like this
public void onPrepared(MediaPlayer mp) {
if (!player.isPlaying()) {
player.start();
}
}
setSurfaceTextureListener like this at onCreate
mTextureView.setSurfaceTextureListener(MainActivity.this);
I hope this will help :)
Require develop streaming audio playback with mediaplayer on android.
Any ideas ?!
Thanks
I try the following code:
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://stream.radiosai.net:8002/"; // your URL here
MP.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
MP.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MP.prepareAsync();
MP.start();
}
});
The above does not work Error deploying
MP.start();
Should be called after the mediaplager is ready, in setOnPreparedListener.
When you create MediaPlayer object, add prepared listener
MP.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
The German computer magazine C't recently published an article with the source code for an Android alarm clock radio. You can download the project files here. I think this contains everything that you need.
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
While I use almost the same code to play two videos, the first works perfectly, but the second does not. When I press the next video button, the program crashed with:
WARN/System.err(15726): java.lang.IllegalStateException
WARN/System.err(15726): at android.media.MediaPlayer.setDataSource(Native Method)
Source code:
the first play code:
mediaPlayer = new MediaPlayer();
playURI = receiveIntent.getStringExtra("playURI");
showDebugInfo("play uri "+playURI);
Log.e("Gplayer on create", "play uri "+playURI);
try {
mediaPlayer.setDataSource(playURI);
} catch (IllegalArgumentException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
Log.v(LOGTAG, e.getMessage());
finish();
}
when press next button it runs the following code:
String uri = listAdapter.getItem(position).getItem().getFirstResource().getValue();
showDebugInfo(" Uri "+uri);
if(mediaPlayer != null){
mediaPlayer.stop();
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(uri);
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
Why does this happen? and I check the two parts only the different that, the first one is new a mediaplayer and the second is let the mediaplayer stop and reset then the same.
I had the same problem.
You need to call mediaPlayer.reset(); before you are calling in the second video mediaPlayer.setDataSource(uri); the reason is, because the function mediaPlayer.setDataSource(uri); can be called only on idle state, and mediaPlayer.reset(); is the function that take you to idle state.
I understand it from this answer, even though the question is a bit different.
I want to play a sound. The first time it works well, but if I stop it and want to restart it nothing happens...Any idea?
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sex);
ImageButton andvib = (ImageButton)findViewById(R.id.vib_toggle);
final AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
andvib.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0);
Vibrator vibr = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibr.cancel();
if(vibrating==false) {
if(style == 0)
vibr.vibrate(durat, 0);
if(style == 1){
vibr.vibrate(staccato, 0);
}
if(style == 2){
vibr.vibrate(wild, 0);
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
vibrating = true;
}
else {
vibrating = false;
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vibr.cancel();
}
}
});
When using MediaPlayer, you should always refer to the state change diagram that you can see here:
http://developer.android.com/reference/android/media/MediaPlayer.html
As you can see from the diagram, after calling stop() on a MediaPlayer, it goes to the Stopped state and you need to call prepare() again to move it to the Prepared state before calling play().
Remember that preparation may take long, so doing that all the time may cause a poor user experience, especially if you are doing it from the main thread (the UI will freeze while the MediaPlayer is preparing). If you play the sound frequently, you should really only prepare() it once, and then always keep it in the Started, Paused or PlaybackCompleted states.
Bruno Oliveira, Developer Programs Engineer, Google
You may have to call mp.prepare() before you call start() the second time.