I'm trying to make a simple media player with a play and stop button. But I'm getting an error in:
method MediaPlayer.create(Context, int)
is not applicable,
method MediaPlayer.create(Context, Uri, SurfaceHolder)
is not applicable,
method MediaPlayer.create(Context, Uri)
is not applicable.
I have looked at a few post on StakeOverflow but none of them seem to have helped. Can anyone enlighten me?
Here are a few that I have found:
Context not recognisied:Method not applicable for arguments
Can't use MediaPlayer.create in new View.OnFocusChangeListener() in Android app
public class MainFragment extends Fragment {
private MediaPlayer mediaPlayer;
public TextView songName, duration;
private double timeElapsed = 0, finalTime = 0;
private Handler durationHandler = new Handler();
private SeekBar seekbar;
public MainFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize views
initializeViews();
}
public void initializeViews() {
songName = (TextView) getView().findViewById(R.id.songName);
mediaPlayer = MediaPlayer.create(this, R.raw.no_diggity);
finalTime = mediaPlayer.getDuration();
duration = (TextView) getView().findViewById(R.id.songDuration);
seekbar = (SeekBar) getView().findViewById(R.id.seekBar);
//songName.setText("Sample_Song.mp3");
seekbar.setMax((int) finalTime);
seekbar.setClickable(false);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
// play mp3 song
public void play(View view) {
mediaPlayer.start();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
}
//handler to change seekBarTime
private Runnable updateSeekBarTime = new Runnable() {
public void run() {
//get current position
timeElapsed = mediaPlayer.getCurrentPosition();
//set seekbar progress
seekbar.setProgress((int) timeElapsed);
//set time remaing
double timeRemaining = finalTime - timeElapsed;
duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
//repeat yourself that again in 100 miliseconds
durationHandler.postDelayed(this, 100);
}
};
}