gpt4 book ai didi

android - MediaPlayer.onPrepared() 未被调用

转载 作者:行者123 更新时间:2023-11-29 20:09:49 24 4
gpt4 key购买 nike

我通过按钮(播放/停止流)在服务和 Activity 中使用媒体播放器。
我正在尝试做这样一件事:
Activity 启动时仅启动一次服务,而不是使用 UI 部分与服务中的媒体播放器一起操作(启动、停止流式传输)。我正在使用 Service Binder。
但是当我点击按钮启动媒体播放器时,它会导致错误:

E/MediaPlayer: start called in state 1
E/MediaPlayer: error (-38, 0)

然后我将标志 isPrepared 添加到 onPrepared() 并在播放流按钮上单击检查标志。标志总是假的。似乎没有调用 onPrepare() 或类似的东西。为什么?怎么了?

已更新
Activity :

public class MainActivity extends AppCompatActivity {

String url = "http://62.80.190.246:8000/ProstoRadiO128";
Button mPlayPauseButton;
boolean musicPlaying = false;
Intent serviceIntent;
MyService mService;
boolean mBound = false;

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

@Override
protected void onStart() {
super.onStart();
serviceIntent = new Intent(this, MyService.class);
serviceIntent.putExtra("url", url);
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
startService(serviceIntent);
musicPlaying = true;
mPlayPauseButton.setBackgroundResource(R.drawable.pause);
Log.d("", "mConnection: " + mConnection);
}

@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}

private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.MyServiceBinder binder = (MyService.MyServiceBinder) service;
mService = binder.getService();
mBound = true;
Log.d("", "in onServiceConnected: mBound = " + mBound);
}

@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};


private void initViews() {
mPlayPauseButton = (Button) findViewById(R.id.btn_play_pause);
mPlayPauseButton.setBackgroundResource(R.drawable.play);
}

private void initListeners() {
mPlayPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playPauseClick();
}
});
}

private void playPauseClick() {
if (musicPlaying == false) { //mBound &&
Log.d("", "mBound: " + mBound);
mService.startStream();
mPlayPauseButton.setBackgroundResource(R.drawable.pause);
musicPlaying = true;
}
else if (musicPlaying == true) { //!mBound
mService.stopStream();
mPlayPauseButton.setBackgroundResource(R.drawable.play);
musicPlaying = false;
}
}
}

已更新服务:

public class MyService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener{

String TAG = "PlayerService__Log";
String url;
MediaPlayer mediaPlayer;
private final IBinder mBinder = new MyServiceBinder();
boolean isPrepared;

public class MyServiceBinder extends Binder {
MyService getService() {
return MyService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

@Override
public void onCreate() {
Log.v(TAG, "Creating Service");
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "onStartCommand");

url = intent.getStringExtra("url");
Log.d(TAG, "url: " + url);
if (!mediaPlayer.isPlaying()) {
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, e.getClass().getName() + " " + e.getMessage());
}
}
return START_STICKY;
}

@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
Log.d(TAG, "media player prepared");
isPrepared = true;
}

@Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "onCompletion");
}

@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}

public void startStream() {
if (!mediaPlayer.isPlaying() ) { //&& isPrepared == true
mediaPlayer.start();
Log.d(TAG, "media player started");
}
else {
Log.d(TAG, "media player is not prepared");
//isPrepared = false;
}
}

public void stopStream() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
Log.d(TAG, "media player stoped");
}
}
}

最佳答案

您忘记启动该服务。调用:

startService(serviceIntent);

请阅读this因为您也需要停止该服务。

对于暂停,而不是停止,调用:

mediaPlayer.pause();

熟悉 docs 中的状态图和无效状态.

关于android - MediaPlayer.onPrepared() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com