片段中的 mPlayer
mPlayer in fragment
你好,我在片段中添加 MdiaPlayer 时遇到了一些麻烦,但我想现在它会起作用,我需要 1 个 lats 东西,因为我的代码:private MediaPlayer mPlayer = null;是 "null" 并且我现在不需要在这一行的 () 中放入什么 mPlayer = new MediaPlayer();如果有人可以帮助我,非常感谢。
public class thekairi78 extends Fragment implements OnClickListener{
View v;
Button france2;
private MediaPlayer mPlayer = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.thekairi78, container, false);
france2 = (Button) v.findViewById(R.id.france2);
france2.setOnClickListener(this);
mPlayer = new MediaPlayer();
return v;
}
private void playSound(int resId){
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(getActivity(), resId);
mPlayer.start();
}
@Override
public void onPause() {
if(mPlayer != null && mPlayer.isPlaying()){
mPlayer.pause();
}
super.onPause();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.france2:
playSound(R.raw.france1);
break;
}
}
}
从
更改此行
mPlayer=new MediaPlayer()
至
mPlayer = MediaPlayer.create(getActivity(), R.raw.sample_song);
试试这个 class,它对我有用
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.media.MediaPlayer;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.prolab2.learn_android.Activitys.Activity_Info;
import com.example.prolab2.learn_android.R;
import java.util.concurrent.TimeUnit;
public class Fragment_Audio extends Fragment implements View.OnClickListener
{
public Fragment_Audio(){
}
private MediaPlayer mediaPlayer;
public TextView songName, duration;
ImageButton btn_play, btn_pause, btn_forward, btn_rewind;
Button btn_audio_player_show;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
private SeekBar seekbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_audio, container, false);
btn_audio_player_show = (Button) view.findViewById(R.id.btn_audio_player_show);
btn_audio_player_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Activity_Info.class);
intent.putExtra("Buttons_id", v.getId());
startActivity(intent);
}
});
songName = (TextView) view.findViewById(R.id.songName);
btn_forward = (ImageButton) view.findViewById(R.id.media_ff);
btn_play = (ImageButton) view.findViewById(R.id.media_play);
btn_pause = (ImageButton) view.findViewById(R.id.media_pause);
btn_rewind = (ImageButton) view.findViewById(R.id.media_rew);
btn_forward.setOnClickListener(this);
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_rewind.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(getActivity(), R.raw.sample_song);
finalTime = mediaPlayer.getDuration();
duration = (TextView) view.findViewById(R.id.songDuration);
seekbar = (SeekBar) view.findViewById(R.id.seekBar);
songName.setText("Sample_Song.mp3");
seekbar.setMax((int) finalTime);
seekbar.setClickable(false);
return view;
}
// play mp3 song
public void play() {
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);
}
};
// pause mp3 song
public void pause() {
mediaPlayer.pause();
}
// go forward at forwardTime seconds
public void forward() {
//check if we can go forward at forwardTime seconds before song endes
if ((timeElapsed + forwardTime) <= finalTime) {
timeElapsed = timeElapsed + forwardTime;
//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}
// go backwards at backwardTime seconds
public void rewind() {
//check if we can go back at backwardTime seconds after song starts
if ((timeElapsed - backwardTime) > 0) {
timeElapsed = timeElapsed - backwardTime;
//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.media_ff:
forward();
break;
case R.id.media_play:
play();
break;
case R.id.media_pause:
pause();
break;
case R.id.media_rew:
rewind();
break;
}
}
}
你好,我在片段中添加 MdiaPlayer 时遇到了一些麻烦,但我想现在它会起作用,我需要 1 个 lats 东西,因为我的代码:private MediaPlayer mPlayer = null;是 "null" 并且我现在不需要在这一行的 () 中放入什么 mPlayer = new MediaPlayer();如果有人可以帮助我,非常感谢。
public class thekairi78 extends Fragment implements OnClickListener{
View v;
Button france2;
private MediaPlayer mPlayer = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.thekairi78, container, false);
france2 = (Button) v.findViewById(R.id.france2);
france2.setOnClickListener(this);
mPlayer = new MediaPlayer();
return v;
}
private void playSound(int resId){
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(getActivity(), resId);
mPlayer.start();
}
@Override
public void onPause() {
if(mPlayer != null && mPlayer.isPlaying()){
mPlayer.pause();
}
super.onPause();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.france2:
playSound(R.raw.france1);
break;
}
}
}
从
更改此行mPlayer=new MediaPlayer()
至
mPlayer = MediaPlayer.create(getActivity(), R.raw.sample_song);
试试这个 class,它对我有用
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.media.MediaPlayer;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.prolab2.learn_android.Activitys.Activity_Info;
import com.example.prolab2.learn_android.R;
import java.util.concurrent.TimeUnit;
public class Fragment_Audio extends Fragment implements View.OnClickListener
{
public Fragment_Audio(){
}
private MediaPlayer mediaPlayer;
public TextView songName, duration;
ImageButton btn_play, btn_pause, btn_forward, btn_rewind;
Button btn_audio_player_show;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
private SeekBar seekbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_audio, container, false);
btn_audio_player_show = (Button) view.findViewById(R.id.btn_audio_player_show);
btn_audio_player_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Activity_Info.class);
intent.putExtra("Buttons_id", v.getId());
startActivity(intent);
}
});
songName = (TextView) view.findViewById(R.id.songName);
btn_forward = (ImageButton) view.findViewById(R.id.media_ff);
btn_play = (ImageButton) view.findViewById(R.id.media_play);
btn_pause = (ImageButton) view.findViewById(R.id.media_pause);
btn_rewind = (ImageButton) view.findViewById(R.id.media_rew);
btn_forward.setOnClickListener(this);
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_rewind.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(getActivity(), R.raw.sample_song);
finalTime = mediaPlayer.getDuration();
duration = (TextView) view.findViewById(R.id.songDuration);
seekbar = (SeekBar) view.findViewById(R.id.seekBar);
songName.setText("Sample_Song.mp3");
seekbar.setMax((int) finalTime);
seekbar.setClickable(false);
return view;
}
// play mp3 song
public void play() {
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);
}
};
// pause mp3 song
public void pause() {
mediaPlayer.pause();
}
// go forward at forwardTime seconds
public void forward() {
//check if we can go forward at forwardTime seconds before song endes
if ((timeElapsed + forwardTime) <= finalTime) {
timeElapsed = timeElapsed + forwardTime;
//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}
// go backwards at backwardTime seconds
public void rewind() {
//check if we can go back at backwardTime seconds after song starts
if ((timeElapsed - backwardTime) > 0) {
timeElapsed = timeElapsed - backwardTime;
//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.media_ff:
forward();
break;
case R.id.media_play:
play();
break;
case R.id.media_pause:
pause();
break;
case R.id.media_rew:
rewind();
break;
}
}
}