Skip to content

Instantly share code, notes, and snippets.

@h6ah4i
Created February 19, 2015 15:00
Show Gist options
  • Save h6ah4i/52cba8959beb0b8dadf0 to your computer and use it in GitHub Desktop.
Save h6ah4i/52cba8959beb0b8dadf0 to your computer and use it in GitHub Desktop.
package com.h6ah4i.android.mediaplayertest;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements SeekBar.OnSeekBarChangeListener {
private static final int SEEKBAR_UPDATE_PERIOD = 100;
private SeekBar mSeekBar;
private MediaPlayer mMediaPlayer;
private Handler mHandler;
private Runnable mPeriodicUpdateSeekBar = new Runnable() {
@Override
public void run() {
onUpdateSeekBar();
}
};
private void onUpdateSeekBar() {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
mHandler.postDelayed(mPeriodicUpdateSeekBar, SEEKBAR_UPDATE_PERIOD);
}
public PlaceholderFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mSeekBar = (SeekBar) (rootView.findViewById(R.id.seekBar));
mSeekBar.setOnSeekBarChangeListener(this);
((Button) rootView.findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStopPlayback();
}
});
return rootView;
}
private void startStopPlayback() {
if (mMediaPlayer != null) {
mMediaPlayer.pause();
mMediaPlayer.release();
mMediaPlayer = null;
mHandler.removeCallbacksAndMessages(null);
} else {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(getActivity().getApplicationContext(), Uri.parse("http://192.168.11.2:8080/test.mp3"));
mMediaPlayer.prepare();
mMediaPlayer.start();
mSeekBar.setMax(mMediaPlayer.getDuration());
onUpdateSeekBar();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (mMediaPlayer != null) {
mMediaPlayer.seekTo(progress);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment