媒体播放器Shoutcast错误

时间:2015-01-20 14:13:40

标签: android android-mediaplayer shoutcast

我正在为本地社区广播电台构建一个Android应用程序,我在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)

这是我的活动代码

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);
    }
}

任何帮助都会受到赞赏,这是我第一次进行此类开发。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题......它必须只是带有端口号的IP地址。

为此: private final String URL =“http://37.187.193.36:8002/listen.pls”;

替换为: private final String URL =“http://37.187.193.36:8002/”;

答案 1 :(得分:0)

所以最后我终于开始工作......首先,感谢@ TennyTech.com的小提示。

所以为了帮助你们的人们,我希望今晚能够在我的github repo上找到答案:)但请注意。

希望这有帮助,

PatrickMelia