这就是我在我的Android当前播放的内容但无法阻止它在mediaPlayer.stop();
private static MediaPlayer mediaPlayer;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view= inflater.inflate(R.layout.fragment_home, container, false);
Button play = (Button) view.findViewById(R.id.play);
Button stop = (Button) view.findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
try {
if (mediaPlayer == null){
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://www.xxxxx/001.mp3");
mediaPlayer.prepare();
if (!mediaPlayer.isPlaying()) {
System.out.println("Should Play");
mediaPlayer.start();
}}
}catch(Exception e){
e.printStackTrace();
}
}
});
public synchronized void stopMedia()
{
if( mediaPlayer.isPlaying())
{
mediaPlayer.stop();
//status = MediaStatus.STOP;
}
}
它播放但我无法阻止它,它在应用程序崩溃了
mediaplayer.stop();
不知道为什么浪费很长时间寻找线索。
=============== 这是根据评论的新代码
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view= inflater.inflate(R.layout.fragment_home, container, false);
Button play = (Button) view.findViewById(R.id.play);
Button stop = (Button) view.findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String url = "http://www.brothershouse.narod.ru/music/pepe_link_-_guitar_vibe_113_club_mix.mp3"; // your URL here
MediaPlayer myMediaPlayer = new MediaPlayer();
myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
myMediaPlayer.setDataSource(url);
myMediaPlayer.prepareAsync(); // might take long! (for buffering, etc)
} catch (IOException e) {
// Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
//mp3 will be started after completion of preparing...
myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
player.start();
}
});
}});
now how do i stop this from a different button
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stop
stopPlaying();
try {
} catch (Exception e) {
e.printStackTrace();
}
}
});
private void stopPlaying() {
myMediaPlayer.pause();
}
现在如何再次停止播放