How to stream mp3 files via https, on Android Lollipop? - android

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

Related

media player play song with buffer

I'm trying to play live stream mp3 audio by media player.
The problem is that when I am using URL, it blocks UI thread for sometime then it works fine. But I need that it plays the song with the buffering as well.
Thanks in Advance.Please help.
try {
audioPlayer = new MediaPlayer();
audioPlayer.setOnBufferingUpdateListener(this);
audioPlayer.setOnCompletionListener(this);
audioPlayer.setAudioStreamType(useFrontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC);
try {
audioPlayer.reset();
audioPlayer.setDataSource(mSongDetail.getPath());
audioPlayer.prepare();
} 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();
}
audioDuration = audioPlayer.getDuration();
audioPlayer.start();
startProgressTimer();
} catch (Exception e) {
if (audioPlayer != null) {
audioPlayer.reset();
audioPlayer.release();
audioPlayer = null;
isPaused = false;
MusicPreferance.playingSongDetail = null;
}
return false;
}
Try to use audioPlayer.prepareAsync() + asyncHandler for it, or use a separate Thread.

Playing RTSP Audio stream

I am trying to play RTSP stream with URL format like rtsp://xxxxxxxx.sdp
Many people are trying to play RTSP video stream in Android some try with intent and some with video view like below code
video1=(VideoView)findViewById(R.id.videoview);
video1.setVideoURI(Uri.parse("rtsp://208.77.20.52:1935/dmm1/ten"));
video1.requestFocus();
video1.start();
Is it possible to play audio stream in Android using MediaPlayer object? If yes, Please provide some solution for that I have an audio stream working fine on VLC but does not work in Android.
Below is the code what I have tried for audio stream from RTSP in Android
mediaPlayer.setDataSource(RTSPurl);
mediaPlayer.prepare();
mediaPlayer.start();
Here is full code I am trying:
public class Radio extends AsyncTask<String, Void, String> {
String rtspUrl = "rtsp://192.168.9.59:8080/test.sdp";
MediaPlayer player;
Context c;
public Radio(Context c) {
this.c = c;
Log.i("TAG","Initializing...");
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
player.start();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
Toast.makeText(c, "starting..", Toast.LENGTH_LONG).show();
}
});
}
private void setDataSource(String path2) {
// TODO Auto-generated method stub
player = new MediaPlayer();
try {
Uri a = Uri.parse(rtspUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(c, a);
player.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();
}
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
setDataSource(rtspUrl);
Log.e("inside doinbackground....", rtspUrl);
return null;
}
}

Media Player called in state 0, error (-38,0) error

i'm trying to use this code which i called from a button but it doesn't work and when i see the LogCat window i found Media Player called in state 0, error (-38,0)
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(HeyActivity.this, Uri.parse(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
System.out.print("hey");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
Try setting OnPreparedListener and call the start method within it. Like this:
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener( new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};
);
I finally solved the problem by running on an emulator that supported audio!!
Can't believe it was that simple!
Window > AVD Manager > Edit your AVD (I created a new one to be on the safe side cause I was running from snapshot) > Hardware > New > Audio Playback Support

Android play sound - forceclose error

I am using below code to play wav sound on touch of imageview
MediaPlayer mp = MediaPlayer.create(this, R.raw.ok);
mp.start();
while (mp.isPlaying()) {
// donothing
};
mp.release();
this gives me Force Close error, am i missing anything? or do i need to provide the permission in manifest file?..Please help.
Thanks
after created media player you have to call prepare() before start. Try this code
MediaPlayer mp = MediaPlayer.create(this, R.raw.ok);
mp.prepare();
mp.start();
Try with this and see.
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();
}
Change this method according to your code. Also if there is any error, you should post the error log so that others could identify the exact issue.

Known streaming mp3 link, Correct Android Version. What is wrong with streaming?

I'm using Android 2.3, so according to this post: Streaming Audio from A URL in Android using MediaPlayer? streaming should work. I'm using a proven mp3 stream from kexp.org.
05-19 13:29:17.141: INFO/StagefrightPlayer(68): setDataSource('http://kexp-mp3-1.cac.washington.edu:8000')
05-19 13:29:17.141: INFO/AwesomePlayer(68): prepare was cancelled before doing anything
try {
mp = new MediaPlayer();
mp.reset();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setDataSource("http://kexp-mp3-1.cac.washington.edu:8000");
mp.prepareAsync();
} 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();
} finally {
if(mp!=null) {
mp.release();
mp =null;
}
}
Try this:
MediaPlayer.setDataSource() and prepare() not working - android

Categories

Resources