开发者

Android: download mp3 from URL

I need to download a mp3 file from a server URL, and then play that mp3 file.

Basically the main motive is to play a remote mp3 file, now there are two options for me, either i stream that file and play it onto media player,

or i first download it and then play it. I am still trying the first one, but with one success, so just thinking on the second alternative.

So if any help can be provided开发者_Python百科 it will be great for me.......

Thanks


public class Mp3player extends Activity {

    private Button buttonPlayStop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;

    private final Handler handler = new Handler();
     static final String AUDIO_PATH =
              "http://sound18.mp3slash.net/indian/abcd/[Songs.PK]%20ABCD%20-%2002%20-%20Bezubaan.mp3";
    // Here i override onCreate method.
    //
    // setContentView() method set the layout that you will see then
    // the application will starts
    //
    // initViews() method i create to init views components.
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        initViews();

    }

    // This method set the setOnClickListener and method for it (buttonClick())
    private void initViews() {
        buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
        buttonPlayStop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClick();

            }
        });

        try {
            playAudio(AUDIO_PATH);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        seekBar = (SeekBar) findViewById(R.id.SeekBar01);
        seekBar.setMax(mediaPlayer.getDuration());
        seekBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                seekChange(v);
                return false;
            }
        });

    }
    private void playAudio(String url) throws Exception
    {
        killMediaPlayer();

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare();
    //    mediaPlayer.start();
    }
     private void killMediaPlayer() {
            if(mediaPlayer!=null) {
                try {
                    mediaPlayer.release();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    public void startPlayProgressUpdater() {
        seekBar.setProgress(mediaPlayer.getCurrentPosition());

        if (mediaPlayer.isPlaying()) {
            Runnable notification = new Runnable() {
                public void run() {
                    startPlayProgressUpdater();
                }
            };
            handler.postDelayed(notification, 1000);
        } else {
            mediaPlayer.pause();
            buttonPlayStop.setText(getString(R.string.play_str));
            seekBar.setProgress(0);
        }
    }

    // This is event handler thumb moving event
    private void seekChange(View v) {
        if (mediaPlayer.isPlaying()) {
            SeekBar sb = (SeekBar) v;
            mediaPlayer.seekTo(sb.getProgress());
        }
    }

    // This is event handler for buttonClick event
    private void buttonClick() {
        if (buttonPlayStop.getText() == getString(R.string.play_str)) {
            buttonPlayStop.setText(getString(R.string.pause_str));
            try {
                mediaPlayer.start();
                startPlayProgressUpdater();
            } catch (IllegalStateException e) {
                mediaPlayer.pause();
            }
        } else {
            buttonPlayStop.setText(getString(R.string.play_str));
            mediaPlayer.pause();
        }
    }
}


MediaPlayer mp=new MediaPlayer();  
mp.setDataSource(url_to_mp3);  
mp.prepareAsync();  
mp.start();    

It's better to use prepareAsync() than prepare().


It's very easy to play a stream

MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepare();
mp.start();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜