I am creating an application where music is streaming from direct link. It is working with emulator but not working on actual device. I created a class named HomeFragment and a nested class Player which extends AsyncTask and initailized MediaPlayer. It looks like that there is error on intantiating MediaPlayer. In the logcat it shows prepare() failed.
I have searched for it and found Attempt to call getDuration without a valid mediaplayer in media player on android ,but this is defferent because i am not getting error because of getDuration() method
public class HomeFragment extends Fragment {
View view;
private ImageButton btn_play_pause;
private TextView tv_time;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
private int realTimeLength;
private boolean playPause;
private boolean initialStage = true;
private ProgressDialog progressDialog;
final Handler handler = new Handler();
private Runnable updater;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment,container,false);
initialize();
return view;
}
private void initialize() {
btn_play_pause = (ImageButton) view.findViewById(R.id.btn_play_pause);
tv_time = (TextView) view.findViewById(R.id.tv_timer);
seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setMax(99);
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(mediaPlayer.isPlaying()){
SeekBar seekbar = (SeekBar) v;
int playPosition = (mediaFileLength / 100) * seekbar.getProgress();
mediaPlayer.seekTo(playPosition);
}
return false;
}
});
mediaPlayer = new MediaPlayer();
progressDialog = new ProgressDialog(view.getContext());
btn_play_pause.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!playPause){
btn_play_pause.setImageResource(R.drawable.ic_pause);
if(initialStage){
new Player().execute("http://www.url_of_song.mp3");
}
else {
if (mediaPlayer != null) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
}
playPause = true;
}
else{
btn_play_pause.setImageResource(R.drawable.ic_play);
if(mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
playPause = false;
}
}
});
}
class Player extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... params) {
boolean prepared = false;
try{
mediaPlayer.setDataSource(params[0]);
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
seekBar.setSecondaryProgress(percent);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
initialStage = true;
playPause = false;
btn_play_pause.setImageResource(R.drawable.ic_pause);
mp.stop();
mp.reset();
}
});
mediaPlayer.prepare();
prepared = true;
}
catch (Exception e){
Log.e("AudioStreaming", e.getMessage());
prepared = false;
}
return prepared;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Please Wait...");
progressDialog.show();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(progressDialog.isShowing()){
progressDialog.cancel();
}
// mediaFileLength = mediaPlayer.getDuration();
realTimeLength = mediaFileLength;
mediaPlayer.start();
initialStage = false;
updateSeekbar();
}
}
private void updateSeekbar(){
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / mediaFileLength)*100));
if(mediaPlayer.isPlaying()){
updater = new Runnable() {
@Override
public void run() {
updateSeekbar();
realTimeLength -= 1000;
tv_time.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes(realTimeLength), TimeUnit.MILLISECONDS.toSeconds(realTimeLength) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(realTimeLength))));
}
};
handler.postDelayed(updater, 1000);
}
}
What i am doing wrong, what is the error, The Logcat shows:
02-03 15:54:27.226 15845-15845/com.example.sk.voiceapplication I/ViewRootImpl: CPU Rendering VSync enable = true
02-03 15:54:27.266 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:27.266 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:29.496 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:33.126 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:33.126 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:36.926 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:36.926 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:40.486 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:40.486 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:44.326 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:44.336 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:48.176 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:48.176 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:51.696 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:51.696 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:55.526 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:55.526 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:59.366 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:59.366 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:03.206 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:03.206 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.046 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:07.046 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.886 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:08.536 15845-15991/com.example.sk.voiceapplication E/MediaPlayer: error (1, -2147483648)
02-03 15:55:08.536 15845-15878/com.example.sk.voiceapplication E/AudioStreaming: Prepare failed.: status=0x1
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: start called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: Error (-38,0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: stop called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)