I have knocked up a media player that plays a live stream from a single radio station. It's simple and it functions, the trouble is sometimes it's three or four minutes behind. I'm assuming it's buffring. Any idea how i reduce this so it plays as close to realtime as possible? I know there will always be some latency but three or fout minutes is ridiculous
Here's the relevent code
public class LCRStreamClass extends Service implements MediaPlayer.OnPreparedListener
{
IBinder mBinder;
boolean mAllowRebind;
int mStartMode;
private static String ACTION_PLAY;
private static String ACTION_PREP;
private static String ACTION_STOP;
static MediaPlayer mediaPlayer = null;
static boolean bAutoStart;
static String sHost, url = "localhost:8000/";
SharedPreferences settings;
static boolean ispreped=false;
@override
public void onCreate() {
int a=4;
settings = getSharedPreferences("LCR_Stream_cofig", 0);
ACTION_PLAY = getResources().getString(R.string.ACTION_PLAY);
ACTION_PREP=getResources().getString(R.string.ACTION_PREP);
ACTION_STOP=getResources().getString(R.string.ACTION_StOP);
}
@override
public int onStartCommand(Intent intent, int flags, int startId) {
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
if (intent.getAction()!=null)
{
if (intent.getAction().equals(ACTION_PREP)) {
mediaPlayer = new MediaPlayer();
bAutoStart = settings.getBoolean("cofig_auto", false);//.putBoolean("cofig_auto",myswitch.isChecked());
sHost = settings.getString("config_host", "");
if (sHost == null)
url =getResources().getString(R.string.default_host); // your URL here
else
if (sHost.isEmpty())
url =getResources().getString(R.string.default_host); // your URL here
else
url = sHost;
try {
mediaPlayer.setDataSource(url);
}
catch (java.io.IOException e)
{
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync(); // prepare async to not block main thread
}
if (intent.getAction().equals(ACTION_PLAY))
{
if ((mediaPlayer.isPlaying() == false) && ispreped) {
//mediaPlayer.seekTo(0);
mediaPlayer.start();
if (mediaPlayer.isPlaying())
Toast.makeText(this, "Playing Lincoln City radio", Toast.LENGTH_LONG).show();
// playButton.setText(R.string.stop_button);
// StatusView.setText("Stream Loaded");
}
}
if (intent.getAction().equals(ACTION_STOP))
{
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
ispreped=false;
intent.setAction(ACTION_PREP);
this.onStartCommand(intent,flags,startId);
}
}
}
return START_STICKY;
}
@override
public boolean stopService(Intent name) {
// TODO Auto-generated method stub
return super.stopService(name);
}
@override
public IBinder onBind(Intent intent) {
return null;
}
/** Called when all clients have unbound with unbindService() */
@override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
/** Called when a client is binding to the service with bindService()*/
@override
public void onRebind(Intent intent) {
}
@override
public void onPrepared(MediaPlayer player)
{
ispreped=true;
// player.start();
}
@override
public void onDestroy() {
super.onDestroy();
}
}Any advice would be great. Thank you.
Here's the relevent code
public class LCRStreamClass extends Service implements MediaPlayer.OnPreparedListener
{
IBinder mBinder;
boolean mAllowRebind;
int mStartMode;
private static String ACTION_PLAY;
private static String ACTION_PREP;
private static String ACTION_STOP;
static MediaPlayer mediaPlayer = null;
static boolean bAutoStart;
static String sHost, url = "localhost:8000/";
SharedPreferences settings;
static boolean ispreped=false;
@override
public void onCreate() {
int a=4;
settings = getSharedPreferences("LCR_Stream_cofig", 0);
ACTION_PLAY = getResources().getString(R.string.ACTION_PLAY);
ACTION_PREP=getResources().getString(R.string.ACTION_PREP);
ACTION_STOP=getResources().getString(R.string.ACTION_StOP);
}
@override
public int onStartCommand(Intent intent, int flags, int startId) {
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
if (intent.getAction()!=null)
{
if (intent.getAction().equals(ACTION_PREP)) {
mediaPlayer = new MediaPlayer();
bAutoStart = settings.getBoolean("cofig_auto", false);//.putBoolean("cofig_auto",myswitch.isChecked());
sHost = settings.getString("config_host", "");
if (sHost == null)
url =getResources().getString(R.string.default_host); // your URL here
else
if (sHost.isEmpty())
url =getResources().getString(R.string.default_host); // your URL here
else
url = sHost;
try {
mediaPlayer.setDataSource(url);
}
catch (java.io.IOException e)
{
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync(); // prepare async to not block main thread
}
if (intent.getAction().equals(ACTION_PLAY))
{
if ((mediaPlayer.isPlaying() == false) && ispreped) {
//mediaPlayer.seekTo(0);
mediaPlayer.start();
if (mediaPlayer.isPlaying())
Toast.makeText(this, "Playing Lincoln City radio", Toast.LENGTH_LONG).show();
// playButton.setText(R.string.stop_button);
// StatusView.setText("Stream Loaded");
}
}
if (intent.getAction().equals(ACTION_STOP))
{
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
ispreped=false;
intent.setAction(ACTION_PREP);
this.onStartCommand(intent,flags,startId);
}
}
}
return START_STICKY;
}
@override
public boolean stopService(Intent name) {
// TODO Auto-generated method stub
return super.stopService(name);
}
@override
public IBinder onBind(Intent intent) {
return null;
}
/** Called when all clients have unbound with unbindService() */
@override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
/** Called when a client is binding to the service with bindService()*/
@override
public void onRebind(Intent intent) {
}
@override
public void onPrepared(MediaPlayer player)
{
ispreped=true;
// player.start();
}
@override
public void onDestroy() {
super.onDestroy();
}
}Any advice would be great. Thank you.
from Android Forums at AndroidCentral.com - Ask a Question https://ift.tt/2rbyBib
via IFTTT
Aucun commentaire:
Enregistrer un commentaire