开发者

Android seekbar with streaming audio

I currently have a media player that is streaming a mp3. I have a seekbar, hwoever, it is not working. I have tried it with an actual raw file and that seems to work. I think the seekbar is not getting the duration of the song. any ideas?

Code:

public class SeekMe extends Activity implements Runnable{
    /** Called when the activity is first created. */
    MediaPlayer mp; 
    Button playButton;
    Button stopButton;
    SeekBar seekMe; 
    int total;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    playButton = (Button)findViewById(R.id.play);
    stopButton = (Button)findViewById(R.id.stop);
    seekMe = (SeekBar)findViewById(R.id.seekBar1);

    final MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource("http://dl.dropbox.com/u/24535120/Week%20of%20May%201/Swanky%20Tunes%20%26%20Hard%20Rock%20Sofa%20-%20Smolengrad%20%28Original%20Mix%29.mp3");
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.prepareAsync();
    } catch (IllegalArgumentException 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();
    }

    mp.setOnPreparedListener(new OnPreparedListener(){

        @Override
        public void onPrepared(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            total = mp.getDuration();
        }

    });

    seekMe.setProgress(0);


    playButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            mp.start();
            seekMe.setMax(total);

        }

    });

    stopButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0){
            mp.pause();
        }
    });

    seekMe.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto开发者_C百科-generated method stub

            if(fromUser){
                mp.seekTo(progress);
                seekMe.setProgress(progress);
            }

        }
    });

    Thread currentThread = new Thread(this);
    currentThread.start();


}

@Override
public void run() {
    // TODO Auto-generated method stub

    try{
        while(mp != null){
            int currentPosition = mp.getCurrentPosition();
            Message msg = new Message();
            msg.what = currentPosition;
            threadHandler.sendMessage(msg);
        }
        Thread.sleep(100);
    }catch (InterruptedException e){
        e.printStackTrace();
    }
}

private Handler threadHandler = new Handler(){
    public void handleMessage(Message msg){
        seekMe.setProgress(msg.what);
    }
};

}


Have you tried calling the default music player with the url of the mp3 file?

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse(url), "audio/*");
context.startActivity(i);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜