So I have a .mp3 in assets folder (I plan to have many more later) and I have a simple click associated to play it:
public static MediaPlayer m = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.btnDrum);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
PlayMusic(v);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void PlayMusic(View v) throws IOException {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd(Kit.KickDrum);
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
However, every-time I try to click the button, I the music plays in loop! I just need it to play once and stop! How can this be done?
Thanks!
you are setting it for looping using m.setLooping(true); change it to false
try this code
try {
Uri mp3 = Uri.parse("url");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(context, mp3);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(onCompletionListener);
} 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();
}
// onCompletionListerner
private OnCompletionListener onCompletionListener = new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
}
};
Related
I have an application which streams mp3 file from AWS server via https protocol.
It works fine on Android 4.4 devices, but not on Android 5.0+ devices.
According to this doc Supported Media Formats I can't understand whether mp3 is supported via http/https or not.
Here is my code:
private void playSong(String songPath, final ArtistSongData data) {
if (currentVersionSupportLockScreenControls) {
UpdateMetadata(data);
remoteControlClient
.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
}
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();
PlayerConstants.isMediaPlayerPrepared = false;
try {
mp.setDataSource(songPath);
// mp.setDataSource("http://p48.ve.vc/data/48/32549/272565/Kede_Pind_Di_-_Jassi_Gill_-_Jassi_Gill_-_48Kbps_-_www.DjPunjab.Com.mp3");
} 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.setAudioStreamType(AudioStream.MODE_NORMAL);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
System.out.println("Music player prepared");
mp.start();
PlayerConstants.isMediaPlayerPrepared = true;
System.out.println("starting now ...");
timer = new Timer();
timer.scheduleAtFixedRate(new MainTask(), 0, 100);
if (PlayerConstants.isHomeShown == true) {
HomeFragment.changeUI();
HomeFragment.setSeekBAr(mp.getDuration());
}
}
});
}
Any help will be appreciated.
Thanks
i want to put progressbar while the oncreat finish...it's take about 10 second to strat activty and show View and prepare mediaplayer...
what i can use to show progressbar until It show the views and prepare media player..
If there any tutorial videos please could you send it in comment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String url = "url";
play = (Button)findViewById(R.id.play);
pause = (Button)findViewById(R.id.pause);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int min = am.getStreamVolume(AudioManager.STREAM_MUSIC);
seekbar.setMax(max);
seekbar.setProgress(min);
seekbar.setOnSeekBarChangeListener( this);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.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();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
mediaPlayer.start();
}});
pause.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mediaPlayer.pause();
}} );
}
after
mediaPlayer = new MediaPlayer();
add this code
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("your message");
pd.setCancelable(false);
pd.setIndeterminate(true);
//Show Dialog
pd.show();
and if you want to cancel or remove Dialog simply call pd.dismiss();
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 am trying to stream an shoutcast audio file in my device android 2.2...i heard that this shoutcast will not work in android 2.2. so, i tried with android emulator 2.3 then also song not playing why could you tell me where i am going wrong...and the code is below...
public class AudioDemo extends Activity {
final String song_uris="http://stream.radiosai.net:8004/";
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button Previous = (Button) findViewById(R.id.Previous);
Button Next = (Button) findViewById(R.id.Next);
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mediaplayer.setDataSource(song_uris);
} 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 {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
mediaplayer.start();
}
});
}
}
How to Reduce the Gap of two Video Playing?
I have two videos, one video is played on onCreate Method & second is onClick Event of Button but Problem is, if I Clicked the Button that time the Black Screen is Come for 5-10 seconds & after second video is start.
My onCreate Code:-
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
String stringPath = "/sdcard/video.mp4";
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(stringPath);
mediaPlayer.prepare();
} 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();
}
mediaPlayer.start();
}
Button Click Event:-
if (v == btnnext) {
String stringPath = "/sdcard/video2.mp4";
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(stringPath);
mediaPlayer.prepare();
} 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();
}
mediaPlayer.start();
}