I have listview, mediaplayer and mediacontroller. I'd like to make mediaplayer play stream musics from url when I touch list in listview.
My problem is mediaplayer plays only one song. When I touch second list while playing first list audio, it does not play second list audio.But I do not know the cause, mediaplayer plays second audio while playing first audio, when mediacontroller hides.
I want mediaplayer to stop and play another audio while mediacontroller shows and plays audio.
I tried two patterns, but these were same result.Please teach me what is wrong and sorry bad english skill.First pattern.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
try {
mp.reset();
mp.setDataSource(url);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
case 1:
try {
mp.reset();
mp.setDataSource(url2);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
case 2:
try {
mp.reset();
mp.setDataSource(url3);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
}
Second pattern.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try {
mp.reset();
mp.setDataSource(url[position]);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
Preparedlistner.
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
handler.post(new Runnable() {
#Override
public void run() {
controller.setEnabled(true);
controller.show(mp.getDuration());
mp.start();
try {
Method m = android.widget.MediaController.class.getDeclaredMethod("updatePausePlay");
m.setAccessible(true);
m.invoke(controller);
} catch (Exception e) {
}
}
});
}
});
}
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
ItemBean sample1 = new ItemBean();
fishing.setName("sample1");
ItemBean sample2 = new ItemBean();
hana.setName("sample2");
ItemBean sample3 = new ItemBean();
takane.setName("sample3");
List<ItemBean> list = new ArrayList<ItemBean>();
list.add(sample1);
list.add(sample2);
list.add(sample3);
sampleurl = getResources().getStringArray(R.array.sampleurl);
mp = new MediaPlayer();
controller = new MediaController(this);
controller.setAnchorView(findViewById(R.id.mediaController));
controller.setMediaPlayer(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
samplelist = (ListView) findViewById(R.id.song_list);
SonglistAdapter_test adapter = new SonglistAdapter_test(getApplicationContext(),list);
samplelist.setAdapter(adapter);
samplelist.setOnItemClickListener(this);
}
Do you call mcontroller = new MediaController(context); and doClean() somewhere? Most likely the controller needs to be reset before you put a new media source. Add this before your handler.post(new Runnable() {:
doClean();
mcontroller = new MediaController(getActivity());//or this, depending whats your context
Related
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;
}
};
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...
When I open the app first I can load the Url to the surface view but later on itemclick when i try to laod another url it cannot load.
My code to load url to player is :
private void setChannelLink(String url){
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(this, Uri.parse(url));
player.setOnPreparedListener(this);
player.setOnErrorListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
when I try to load another url i am doing player.stop() and player.reset() before calling this method.. What am i missing?
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();
}
}
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();
}