I am developing a Radio application in android that will stream from url and in background will show progress dialog loading streams but shows ANR. I am using service for mediaplayer to start and stop. Need help.I have used asynctask but can't get a solution.
MainActivity.class
button_Play.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new DownloadTask().execute();
}
});
class DownloadTask extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(MainActivity.this , "Android Streaming
Player" , "Connecting to Stream..." , true , false);
Log.d("onPreExecute()----->" , "MainActivity.class");
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params)
{
Log.d("doInBackground()----->" , "MainActivity.class");
startService(new Intent(MainActivity.this , RadioService.class));
return null;
}
#Override
protected void onPostExecute(Void result)
{
Log.d("onPostExecute()----->" , "MainActivity.class");
progressDialog.dismiss();
super.onPostExecute(result);
}
}
Here is service class:
RadioService.class
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("onStartCommand------->", "RadioService.class");
try
{
Log.d("MediaPlayer--->TRY BLOCK", "MediaPlayer");
URL url2 = new URL("http://141.139.35.23:8000");
URLConnection urlConnection = url2.openConnection();
player.setDataSource(urlConnection.getURL().toString());
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
Log.d("MediaPlayer--->TRY BLOCK AFTER ASYNC", "MediaPlayer");
player.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
Log.d("MediaPlayer--->ON PREPARED BEGINS", "MediaPlayer");
//mp.setOnErrorListener(MainActivity.this);
mp.start();
//mp.setOnErrorListener(this);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
Toast.makeText(getApplicationContext() , "Streaming started " , Toast.LENGTH_SHORT).show();
Log.d("MediaPlayer--->ON PREPARED ENDS", "MediaPlayer");
}
});
} catch (Exception e)
{
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
class Loading extends AsyncTask<Object, Object, Object>{
protected void onPreExecute(){
loadDialog.show();
}
#Override
protected Object doInBackground(Object... arg0) {
if(rFlag==false){
mediaPlayer = new MediaPlayer();
try{
url = "rtsp://ra.dio";
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
loadDialog.dismiss();
errmsg = "error";
// TODO: handle exception
}
while(rFlag==false){
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
// TODO Auto-generated method stub
if(arg1 >= 98){
loadDialog.dismiss();
rFlag=true;
}
}
});
}
}
else if(rFlag==true){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
rFlag=false;
}
return null;
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// loaded
}
}
Related
I have some problem with Android AsyncTask.In the Fragment i use async task for streaming a audio from url. At first play button it executes without any problem but when i click to go to next song the UI skip the song when i click again in next button then its working fine. It means i have to click two times to execute next button functionality.Here is my async task class:
private class LodingToFetchMp3Url extends AsyncTask<Void, Integer, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("Loading...!");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected void onProgressUpdate(Integer... progress) {
}
#Override
protected String doInBackground(Void... params) {
try {
mediaPlayer.setDataSource(Constants.musicArrayListConstants.get(position).getMp3FileUrl());
mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
#Override
protected void onPostExecute(String result) {
pDialog.dismiss();
mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
imgPlayPause.setBackgroundResource(R.mipmap.btn_pause_black);
} else {
mediaPlayer.pause();
imgPlayPause.setBackgroundResource(R.mipmap.btn_play);
}
primarySeekBarProgressUpdater();
super.onPostExecute(result);
}
}
primarySeekBarProgressUpdater(); method:
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100)); // This math construction give a percentage of "was playing"/"song length"
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 100);
}
}
Next Button onClick :
else if (v.getId() == R.id.imgNext) {
if(Constants.dropsModelArrayListConstants.size() > position) {
position = position + 1;
mediaPlayer.pause();
mediaPlayer = new MediaPlayer();
lodingToFetchMp3Url.cancel(true);
lodingToFetchMp3Url = new LodingToFetchMp3Url();
lodingToFetchMp3Url.execute();
}
}
I am trying to play an audio using service and update seekbar item in adapter. But I am unable to get duration of audio from service. Also when scrolling recyclerview, more than one item view is updated instead of updating currently playing seekbar item. I have tried below code.
public class LocalService extends Service {
boolean mBound = false;
private final IBinder mBinder = new LocalBinder();
private String path = "";
private int timer;
private static MediaPlayer mediaPlayer = new MediaPlayer();
// Random number generator
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/**
* method for clients
*/
public int getTotalTime() {
return mediaPlayer.getDuration();
}
public int getDuration() {
// Log.v("player duration",timer+"");
try {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
Log.v("playing", (mediaPlayer.getCurrentPosition() / 1000) + "");
return mediaPlayer.getCurrentPosition() / 1000;
} else {
//Log.v("not playing", (mediaPlayer.getCurrentPosition() / 1000) + "");
return 0;
}
}catch (IllegalStateException i)
{
i.printStackTrace();
return 0;
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
#Override
public void onCreate() {
super.onCreate();
try {
mediaPlayer = new MediaPlayer();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setAudio(String path)
{
try {
mediaPlayer.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void playAudio() {
Log.v("audio path", path);
try {
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.prepareAsync();
mediaPlayer.start();
timer=mediaPlayer.getDuration();
Log.v("total timer1",timer+"");
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
timer=0;
mediaPlayer.stop();
mediaPlayer.reset();
//mediaPlayer.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
mediaPlayer.reset();
mediaPlayer.release();
}
#Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.reset();
mediaPlayer.release();
}
Service is started from adapter constructor.
Intent intent = new Intent(mContext, LocalService.class);
mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
player item click in recycler adpater
vhItemHolder.playPauseImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
currentlyPlayingPosition = position;
if (mBound) {
Log.v("service","bound");
//Make sure you update Seekbar on UI thread
mService.setAudio(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer=new MediaPlayer();
try {
mPlayer.setDataSource(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer.prepareAsync();
vhItemHolder.seekBar.setMax(mPlayer.getDuration());
} catch (IOException e) {
e.printStackTrace();
}
mService.playAudio();
Log.v("duration",(mService.getTotalTime())+"");
vhItemHolder.seekBar.setMax(mService.getTotalTime());
mHandler.removeCallbacks(null);
mHandler=new Handler();
((DoctorActivity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
num = mService.getDuration();
Log.v("progress",num+"");
vhItemHolder.seekBar.setProgress((num));
vhItemHolder.runningTimerTextView.setText(convertMilliToMinutes((long)mService.getDuration()));
mHandler.postDelayed(this, 50);
}
});
}
else
{
Log.v("service","not bound");
}
Inside your adapter class:
You are making a new object of MediaPlayer (mPlayer) in viewHolder's playPauseImageView's onClickListener and calling setDataSource() method on it which sets data source for this local mPlayer object not to the mediaPlayer object of your LocalService class.
Calling mService.playAudio() and mService.getDuration() methods access your LocalService class' mediaPlayer object for which setDataSource() method is not called yet, hence must be returning wrong duration and unable to play.
You should make a change in your adapter class like below:
vhItemHolder.playPauseImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
currentlyPlayingPosition = position;
if (mBound) {
Log.v("service","bound");
//Make sure you update Seekbar on UI thread
mService.setAudio(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer = mService.mediaPlayer;// or mPlayer = LocalService.mediaPlayer; as being static object.
//rather new MediaPlayer();
try {
mPlayer.setDataSource(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer.prepareAsync();
vhItemHolder.seekBar.setMax(mPlayer.getDuration());
} catch (IOException e) {
e.printStackTrace();
}
mService.playAudio();
Log.v("duration",(mService.getTotalTime())+"");
vhItemHolder.seekBar.setMax(mService.getTotalTime());
mHandler.removeCallbacks(null);
mHandler=new Handler();
((DoctorActivity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
num = mService.getDuration();
Log.v("progress",num+"");
vhItemHolder.seekBar.setProgress((num));
vhItemHolder.runningTimerTextView.setText(convertMilliToMinutes((long)mService.getDuration()));
mHandler.postDelayed(this, 50);
}
});
}
else
{
Log.v("service","not bound");
}
I am developing an android app which play a mp3 songs by using a remote url. everything is working fine in android devices below api 5.0.But when starting app in samsung s5(5.1) it suddenly crashes
mu logcat is giving error "QCmediaPlayer mediaplayer is not present.Here is my code of Media Player
public class MainActivity2 extends Activity implements OnClickListener, OnPreparedListener {
private ProgressBar playSeekBar;
private final static String RADIO_STATION_URL ="https://aryaradio.s3.amazonaws.com/";
private String KEYNAME,encodedurl;
private List<String> playlistarray;
private List<MediaPlayer> mplayerList;
private ImageButton buttonPlay;
private List<S3ObjectSummary> playlist=null;
private ImageButton buttonStopPlay;
ProgressDialog progress;
URL url,currentsongurl;
private MediaPlayer player;
private AmazonS3Client mClient;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
playlistarray=new ArrayList<String>();
mplayerList=new ArrayList<MediaPlayer>();
if(isNetworkAvailable()) {
mClient = Util.getS3Client(MainActivity2.this);
initializeUIElements();
//new RefreshTask().execute();
buttonStopPlay.setVisibility(View.INVISIBLE);
} else {
new AlertDialog.Builder(MainActivity2.this)
.setTitle(getResources().getString(R.string.app_name))
.setMessage(
getResources().getString(
R.string.internet_error))
.setPositiveButton("OK", null).show();
buttonStopPlay.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.INVISIBLE);
}
}
private void initializeUIElements() {
buttonPlay = (ImageButton) findViewById(R.id.Play);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (ImageButton) findViewById(R.id.Stop);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
finish();
startActivity(new Intent(MainActivity2.this, MainActivity2.class));
}
}
private void startPlaying() {
buttonPlay.setVisibility(View.INVISIBLE);
buttonStopPlay.setVisibility(View.VISIBLE);
progress = new ProgressDialog(this);
progress.setTitle("Message");
progress.setMessage("Loading Song ...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progress.dismiss();
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
try {
// player.setNextMediaPlayer(mplayerList.get(2));
stopPlaying();
} 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();
}
}
});
}
});
}
private void stopPlaying() {
killMediaPlayer();
initializeMediaPlayer(playlistarray);
buttonPlay.setVisibility(View.VISIBLE);
buttonStopPlay.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer(List<String> playlist) {
player = new MediaPlayer();
int noOfSongs=playlist.size();
String url = "https://aryaradio.s3.amazonaws.com/us-east-1:eb604ac1-c4e3-4226-bea8-22f214a6b0b0/RecordingArya-9459.mp3.null";
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(url);
player.setOnPreparedListener(this);
player.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
return false;
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("Buffering", "" + percent);
}
});
}
}
I'm facing with a problem. I want to get duration of video when i 'm recording it.
I can get duration of video when it's finish by code
MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(video));
if(mp == null)
return -1;
int duration = mp.getDuration();
mp.release();
But i want to get duration everytime when i record video to update to progressbar.
Hope your reply!
private class PlaybackObserver extends Thread {
public void run() {
currentPosition = 0;
try {
while (!killObserverThread) {
Thread.sleep(1000);
currentPosition = (int) mediaPlayer.getCurrentPosition();
runOnUiThread(new Runnable() {
public void run() {
yourProgressBar.setProgress(currentPosition);
}
});
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
killObserverThread = false;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (task == null || !task.isAlive()) {
task = new PlaybackObserver();
task.start();
}
startPlaying();
}
add a private class for your UiThread to update your seekbar/progressbar. Then start it from onResume()
I am trying to make a service that plays in background which is unbounded. I have walked myself through some of the example codes on the internet but I can't get my application to play the radio when I'm calling the service class.
Please have a look at the code and tell me where I am going wrong... When I call MyService class from ArmanFMRadio onClick It toasts "My Service Created" & "My Service Started" but doesnt get to play the audio for the radio stream link. I've checked it otherwise and the link seems fine, so problem lies somewhere in the code to my understanding:
package com.etc.etcc;
public class ArmanFMRadio extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.armanfm);
initializeUIElements();
//initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://50.117.26.26:3953/Live");
} 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();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setVisibility(View.VISIBLE);
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonPlay:
playSeekBar.setVisibility(View.VISIBLE);
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
startService(new Intent(this, MyService.class));
//startPlaying();
break;
case R.id.buttonStopPlay:
stopPlaying();
break;
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
Just look at onClick on the above code, because this class works fine to my thinking.
MyService class:
package com.etc.etcc;
public class MyService extends Service {
private static final String TAG = "MyService";
private MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player = new MediaPlayer();
try {
player.setDataSource("http://50.117.26.26:3953/Live");
} 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();
}
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
}
onStart is deprecated method. you should use onStartCommand method.
Debug your code and check weather there is any Exception or something
Also you are using the service so possible that you service will call twice in that case your onStartCommand method will be called twice so there you will have to check the startId which you will get as a parameter. If startId > 1 that means previously your service is started so you can stop media player and again start a media player with latest source or you can just ignore the second request.
If you are not confidence with service you can put your code in the activity and check weather your code is working fine or not after that you can replace this code in the service.