Reputation: 87
I am in the process of building an android app for a local community radio station and I am getting this error in the LogCat:
2862-2862/pmelia.bcrfm V/MediaPlayer﹕ callback application
2862-2862/pmelia.bcrfm V/MediaPlayer﹕ back from callback
2862-2862/pmelia.bcrfm V/MediaPlayer﹕ isPlaying: 0
2862-2862/pmelia.bcrfm E/MediaPlayer﹕ Error (-38,0)
Here is my activity code
public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnTouchListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {
private Toolbar toolbar;
private Button btnPlay;
private Button btnPause;
private Button btnStop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
//official broadcasting URL
private final String URL = "http://37.187.193.36:8002/listen.pls";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() {
@Override
public void run() {
updateSeekProgress();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
init();
}
private void init() {
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout), toolbar);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(this);
btnPause = (Button) findViewById(R.id.btnPause);
btnPause.setOnClickListener(this);
btnPause.setEnabled(false);
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(this);
btnStop.setEnabled(false);
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, "Hey you just hit " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
seekBar.setSecondaryProgress(percent);
}
@Override
public void onCompletion(MediaPlayer mp) {
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
btnStop.setEnabled(false);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar tmpSeekBar = (SeekBar)v;
mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
}
return false;
}
@Override
public void onClick(View view) {
try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
lengthOfAudio = mediaPlayer.getDuration();
}
catch (Exception e) {
Log.e("Error", e.getMessage());
}
switch (view.getId()) {
case R.id.btnPlay:
playAudio();
break;
case R.id.btnPause:
pauseAudio();
break;
case R.id.btnStop:
stopAudio();
break;
default:
break;
}
updateSeekProgress();
}
private void updateSeekProgress() {
if (mediaPlayer.isPlaying()) {
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
handler.postDelayed(r, 1000);
}
}
private void stopAudio() {
mediaPlayer.stop();
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
btnStop.setEnabled(false);
seekBar.setProgress(0);
}
private void pauseAudio() {
mediaPlayer.pause();
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
}
private void playAudio() {
mediaPlayer.start();
btnPlay.setEnabled(false);
btnPause.setEnabled(true);
btnStop.setEnabled(true);
}
}
Any help would be appreciated, this is my first time doing this type of development.
Upvotes: 2
Views: 187
Reputation: 87
So I got it to work finally in the end....First of all, thanks to @TennyTech.com for the little hint.
So to help ye people out, I will have an ans up on my github repo by tonight hopefully :) But do keep an eye out.
Hope this helps,
PatrickMelia
Upvotes: 0
Reputation: 11
I had the same problem... It has to be just the IP address with port number.
For this: private final String URL = "http://37.187.193.36:8002/listen.pls";
Replace with: private final String URL = "http://37.187.193.36:8002/";
Upvotes: 1