public void playClickSound() {
AssetFileDescriptor afd;
MediaPlayer sound = new MediaPlayer();
try {
sound.setAudioStreamType(AudioManager.STREAM_MUSIC);
afd = getAssets().openFd("click.mp3");
sound.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
sound.prepare();
sound.start();
} catch (IllegalStateException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
This is my code for playing sound. I call this method on a few buttons in mu GUI.
It works fine the first time i press the button, but second time i get IllegalStateException.
What should I do to make this work?
You need to manage the life-cycle of the Media Player. Following the below flow, should work out:
RefreshPlayer()
{
if (mediaPlayer != null) {
{
mediaPlayer.stop();
mediaPlayer.reset();
}
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getSherlockActivity(),
Uri.fromFile(new File(VidPath)));
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
Related
This is how I am doing this.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next: {
currentFile = input.get(currentPosition + 1);
if (mediaPlayer != null) {
mediaPlayer.release();
}
try{
mediaPlayer.setDataSource(currentFile);
mediaPlayer.reset();
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
case R.id.prev: {
currentFile = input.get(currentPosition - 1);
if (mediaPlayer != null) {
mediaPlayer.release();
}
try {
mediaPlayer.setDataSource(currentFile);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Can't play the next and previous buttons. I want to play next and previous track from raw folder. I tried to do so but I am unable to do this. Give some suggestions. Thank You...
I used this code to play a mp3 file. Its playing in any device without Samsung... I search a lot but can't solved this problem. Please can anyone give any method to play mp3 in any device.
//Not working in Samsung Device
MediaPlayer mMediaPlayer;
void playSound()
{
try {
mMediaPlayer = MediaPlayer.create(this, R.raw.recipe_tune);
mMediaPlayer.start();
} catch (Exception e) {
// TODO: handle exception
}
}
Call prepare method before starting player and also print exception in your logcat and also catch like this,
try {
//code lies here
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I am using MediaPlayer in my app. I am following the following tutorial
http://blog.lemberg.co.uk/surface-view-playing-video
Here's part of the code
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(FILE_URL));
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
Everything works fine except the video does not loop. When I set the data source a file from assets it loops just fine. But when I stream the video from URL it does not loop.
Thanks
I am developing app that search for song and able play and download music/song.I used this url http://tinysong.com/Phdj , this is redirecting to music web page and there its playing song. i used below code to play song.
//play music
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try{
mMediaPlayer.setDataSource("http://tinysong.com/Phdj");
mMediaPlayer.prepareAsync();
}catch(IOException e){
e.printStackTrace();
}
catch (IllegalArgumentException e){
e.printStackTrace();
}catch (IllegalStateException e){
e.printStackTrace();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
ERROR:
09-21 11:26:49.674: I/MediaPlayer(6735): setLPAflag() in
09-21 11:26:49.674: I/MediaPlayer(6735): mContext is null, can't getMirrorDisplayStatus!!!
09-21 11:26:49.674: I/MediaPlayer(6735): setLPAflag() out
09-21 11:26:58.693: E/MediaPlayer(6735): error (1, -2147483648)
09-21 11:26:58.693: E/MediaPlayer(6735): Error (1,-2147483648)
Is AndroidMedia Library play only .mp3 format? Can i play music from above Url?How can i do that?
You have not passed the context in setDataSource()
Try this
String path="http://tinysong.com/Phdj";
Uri myUri = Uri.parse(path);
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(this, myUri);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
// TODO Auto-generated method stub
}
});
mp.prepareAsync();
} 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();
}
I copied song.mp3 to my project's assets directory and wrote this code:
private MediaPlayer mp;
Uri uri = Uri.parse("file:///android_asset/song.mp3");
mp=MediaPlayer.create(this, uri);
After running the create statement, the variable mp is null. What is wrong?
Thanks.
Try this:
try {
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
}
catch (IllegalArgumentException e) { }
catch (IllegalStateException e) { }
catch (IOException e) { }
Try this and see if any exceptions are caught:
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
}
catch (NullReferenceArgument e) {
Log.d(TAG, "NullReferenceException: " + e.getMessage());
}
catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException: " + e.getMessage());
}
catch (IOException e) {
Log.d(TAG, "IOException: " + e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
}
catch (SecurityException e) {
Log.d(TAG, "SecurityException: " + e.getMessage());
}
The exception caught will explain what is going wrong in your create. According the the docs, the static create method is just shorthand for what is in the try block above. The major difference that I can see is that the static method create doesn't throw while setDataSource does.
You'd better try this on the devices running on Android N or the latest:
try {
AssetFileDescriptor afd = getAssets().openFd("*.mp3 / *.mp4");
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
player.setDataSource(afd);
player.prepareAsync();
player.start();
} catch (...) {
}
else, do like the best answer below.