Android - Mediaplayer Audio Streaming,倒带和转发

时间:2014-11-19 19:27:39

标签: android android-mediaplayer audio-streaming

我有一个Android应用程序,通过URL(它是一个无限的流)传输音频,它的工作原理。但是我需要允许用户快退X秒并转发X秒(如果你之前倒带,你只能转发)

这里是代码

public class MainActivity extends Activity implements View.OnClickListener {

private final String TAG = "Main";

private Button buttonPlay;
private MediaPlayer player;
private boolean paused = false;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
private Button forward, rewind;

private void initControls()
{
    try
    {
        volumeSeekbar = (SeekBar)findViewById(R.id.volume);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        volumeSeekbar.setMax(audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        volumeSeekbar.setProgress(audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC));


        volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar arg0)
            {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0)
            {
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
            {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                        progress, 0);
            }
        });
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    initControls();

    initializeUIElements();

    initializeMediaPlayer();
}

private void initializeUIElements() {

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    rewind = (Button) findViewById(R.id.rewind);
    forward = (Button) findViewById(R.id.forward);
    buttonPlay.setOnClickListener(this);
    rewind.setOnClickListener(this);
    forward.setOnClickListener(this);

}

public void onClick(View v) {

    if (v == buttonPlay) {
        if(!player.isPlaying())
        {
            Log.d(TAG,"Encender");
            if(!paused) {
                player.prepareAsync();

                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                    public void onPrepared(MediaPlayer mp) {
                        player.start();
                    }
                });
            }
            else
                player.start();
        }
        else
        {
            Log.d(TAG,"Pausar");
            player.pause();
            paused = true;

        }
    }
    if(v==rewind){
        Log.d(TAG,"RW");
        if(player.isPlaying()){
            //TODO: CODE FOR REWIND
        }
    }
    if( v== forward){
        Log.d(TAG,"FW");
        if(player.isPlaying()){
            //TODO: CODE FOR FORWARD
        }
    }
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource("MYURL");

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {

    }
}


@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) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void showOKDialog(Activity ac, String msg){
    AlertDialog.Builder builder = new AlertDialog.Builder(ac);
    builder.setMessage(msg)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do things
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

}
你能帮我解决这个问题吗? 有人知道这样做的合适方法吗?

谢谢!

0 个答案:

没有答案