I am trying to stream m3u from url. The below code is working with an MP3 url but no with an m3u url.
This is how I initialize the player
private void initializeMediaPlayer() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
e.printStackTrace();
}
mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Log.i("Buffering", "" + percent);
}
});
}
And this is my listener for play button
radioPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioPlayButton.setBackgroundResource(R.drawable.ic_pause);
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
}
});
I am testing in an Android 4.1.2 API 16 device and I am getting
E/MediaPlayer: Error (1,-2147483648)
Related
I want to play/stream audio from url one after other automatically.
here what I have tried:
final String audioUrl[] = {"https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/NV.wav?alt=media", "https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/cqo.wav?alt=media"};
backtobackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
Log.d("Current track",currentTrack+"");
Log.d("audioUrl.length",audioUrl.length+"");
if (currentTrack < audioUrl.length) {
currentTrack++;
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
mediaPlayer.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
// make something
}
}
});
My first audio url gets played when I click on backtoback named button but failed to play second audio from url.
changed this part of code
mediaPlayer.reset();
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
currentTrack++;
and worked.
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mediaPlayer=new MediaPlayer();
try {
mediaPlayer.setDataSource("https://firebasestorage.googleapis.com/v0/b/photo-on-birthday-cake-cccf5.appspot.com/o/birthday%20(1).mp3?alt=media&token=c1e988b4-f482-4020-8057-53c68195d2fc");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if(flag==0) {
mediaPlayer.start();
Toast.makeText(MusicActivity.this, "start", Toast.LENGTH_SHORT).show();
flag=1;
play.setBackgroundResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else {
mediaPlayer.pause();
Toast.makeText(MusicActivity.this, "Stop", Toast.LENGTH_SHORT).show();
flag=0;
}
}
});
mediaPlayer.prepare();
}
catch (IOException e){
e.printStackTrace();
}
}
});
I tried this code but this is not working on pause function while working on play.
I am trying to play an audio file when the app started, but I am getting an error says that start called in state 1 Error (-38,0), here is my code:
public void audioPlayer(){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.create(this, R.raw.music);
onPrepared(mp);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mp) {
mp.start();
}
public void audioPlayer(){
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener(mListener);
try {
mp.setDataSource(this, R.raw.music);
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
private OnPreparedListener mListener=new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};
I want to play this streaming url:
http://www.mediacorpradio.sg/radioliveplayer/asx/class95/fm950.asx
What I tried:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// player
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
button.setImageResource(R.drawable.pause);
}
});
try {
player.setDataSource(radio_url);
} catch (Exception e) {
toast("Unable to play this radio. Please email us if you see this constantly.");
return;
}
player.prepareAsync();
}
private void toggleState(){
if (player.isPlaying()) {
player.pause();
button.setImageResource(R.drawable.play);
}else{
player.prepareAsync();
}
}
#Override
public void onDestroy() {
super.onDestroy();
player.release();
}
Error I get: (1, -1004)
I am developing a simple radio streaming application to play radio using a URL. This application is working in all the versions except for V>=4.0
Do anyone have any Idea over this.
initializeUIElements();
StartPlaying();
private void initializeUIElements() {
buttonPlay = (ImageView) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
playSeekBar=(ProgressBar)findViewById(R.id.progressBar1);
}
private void startPlaying() {
initializeMediaPlayer();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.i("on prepared", "on prepared");
mp.start();
}
});
player.setOnErrorListener(new OnErrorListener(){
public boolean onError(MediaPlayer arg0, int arg1,
int arg2) {
Toast.makeText(getApplicationContext(),"An error happened while preparing radio",Toast.LENGTH_LONG).show();
player.reset();
initializeMediaPlayer();
return false;
}
});
private void initializeMediaPlayer() {
player = new MediaPlayer();
player.reset();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://***************");
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("percent", ""+percent);
if( (percent!=0)||(percent==100) )
{
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.VISIBLE);
}
}
});
In Android version 4.0, It is giving an error as:
11-23 13:06:37.329: E/MediaPlayer(4011): Error (1,-2147483648)
and Player.bufferingUpdateListener() is not called. Here It is showing just that playseekbar revolving and revolving.. Please help on this.
It can be a problem with your particular device. Try running the same on 4.0 emulator.