android当url音频流,音乐没有停止

时间:2022-02-19 14:46:49

Codes in below. when i add mediaplayer.stop();, media player hasn't stop. same thing for mediaplayer.pause(); if works. because icon is changing. But music hasn't stop. And i can't do debuging in android studio. Thanksi in advance.

代码如下。当我添加mediaplayer.stop();时,媒体播放器还没有停止。 mediaplayer.pause();同样的事情;如果有效。因为图标正在改变。但音乐并未停止。我不能在android studio中进行调试。提前谢谢。

package ceyhun.musicpuzzle.com;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import java.io.IOException;


public class MainActivity extends ActionBarActivity {

Button PlayPause;
private boolean boolMusicPlaying = false;


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

    Baslamak();
}
private void Baslamak() {

PlayPause = (Button)findViewById(R.id.PlayPause);
PlayPause.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
    oynatim();


    }
});

}
private void oynatim() {

 final MediaPlayer mediaPlayer = new MediaPlayer();

if (!boolMusicPlaying) {
    boolMusicPlaying = true;
    PlayPause.setBackgroundResource(R.drawable.ic_action_pause);

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try {
        mediaPlayer.setDataSource(getString(R.string.rihanna));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
    Baslamak();
  }
   else {
    mediaPlayer.stop();
    //there is a problem. music hasn't stop.
    mediaPlayer.reset();

    PlayPause.setBackgroundResource(R.drawable.ic_action_play);
    boolMusicPlaying = false;


    Baslamak();
  }
  }







  }

2 个解决方案

#1


Each time you run the "oynatim" method, you're instantiating a new class.

每次运行“oynatim”方法时,都会实例化一个新类。

final MediaPlayer mediaPlayer = new MediaPlayer();

The line above should only be run once and not every time the method is run. I ran into the same problem when using Android Studio.

上面的行应该只运行一次,而不是每次运行该方法。我在使用Android Studio时遇到了同样的问题。

On a side note, you have to call mediaPlayer.prepare() or mediaPlayer.prepareAsync() if you want to get mediaPlayer out of a "stopped" states. This happens when you call mediaPlayer.stop().

另外,如果要使mediaPlayer脱离“已停止”状态,则必须调用mediaPlayer.prepare()或mediaPlayer.prepareAsync()。调用mediaPlayer.stop()时会发生这种情况。

Edit: Put the final MediaPlayer in the class and not the methods.

编辑:将最终的MediaPlayer放在类中,而不是方法中。

#2


private void stopPlaying {
            if ( mediaPlayer != null ) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
 }

mediaPlayer is the instance of MediaPlayer in android that you first assign your URL and start play music :)

mediaPlayer是android中的MediaPlayer实例,您首先分配您的URL并开始播放音乐:)

#1


Each time you run the "oynatim" method, you're instantiating a new class.

每次运行“oynatim”方法时,都会实例化一个新类。

final MediaPlayer mediaPlayer = new MediaPlayer();

The line above should only be run once and not every time the method is run. I ran into the same problem when using Android Studio.

上面的行应该只运行一次,而不是每次运行该方法。我在使用Android Studio时遇到了同样的问题。

On a side note, you have to call mediaPlayer.prepare() or mediaPlayer.prepareAsync() if you want to get mediaPlayer out of a "stopped" states. This happens when you call mediaPlayer.stop().

另外,如果要使mediaPlayer脱离“已停止”状态,则必须调用mediaPlayer.prepare()或mediaPlayer.prepareAsync()。调用mediaPlayer.stop()时会发生这种情况。

Edit: Put the final MediaPlayer in the class and not the methods.

编辑:将最终的MediaPlayer放在类中,而不是方法中。

#2


private void stopPlaying {
            if ( mediaPlayer != null ) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
 }

mediaPlayer is the instance of MediaPlayer in android that you first assign your URL and start play music :)

mediaPlayer是android中的MediaPlayer实例,您首先分配您的URL并开始播放音乐:)