gpt4 book ai didi

java - (Android Media Player) 为什么不能使用prepareAsync()而不是prepre()与AsyncTask

转载 作者:行者123 更新时间:2023-12-01 11:10:29 25 4
gpt4 key购买 nike

我确实是这方面的新手(也是英语)。最近,我花时间阅读(或至少尝试)有关 Android Media Player 的内容。

这一次,我通过 Google 找到了这个:http://dev-space.googlecode.com/svn/trunk/StreamPlayer/src/com/pozheg/

我尝试使用原始源代码播放 mp3 文件( http://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3 ),效果很好。但是当我在流中使用prepareAsync() 时,mMediaPlayer 会传递给release()。

我正在尝试使用prepareAsync()而不是prepare()AsyncTask来播放音频流

问题是:为什么不能?

我知道我的问题可能很愚蠢,但我想和你一起学习。

这样:

package com.myaudioservice.app;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;

/**
* Service to serv MediaPlayer in background
*
* @author Eugeny Pozharsky
*/
public class PlayerService extends Service {
private final IBinder mBinder = new LocalBinder();
private MediaPlayer mediaPlayer;
private PlayerCallback callback;

public class LocalBinder extends Binder {
PlayerService getService() {
return PlayerService.this;
}
}

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

/**
* Kill MediaPlayer to release resources
*/
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
stop();
mediaPlayer.release();
}
}

/**
* Starts playing stream.
* Note that we just start media player and do not obtain real URL of the stream.
* So, URL that will be redirected by server will not work. To make such URLs works, adding
* algorithm to obtain the real URL (f.e., create HttpConnection, connect to the server and
* get real URL from connection).
*
* @param url String with URL of a server to connect
*/
public void start(String url) {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
if (isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
}
new Player().execute(url);
}

/**
* Stops playing of the stream.
*/
public void stop() {
// stopping MediaPlayer in separate thread because it can take a time
new Thread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
}).start();
if (callback != null) {
callback.onStopped();
}
}

/**
* Is stream playing?
*
* @return true or false
*/
public Boolean isPlaying() {
return mediaPlayer != null && mediaPlayer.isPlaying();
}

public void setCallback(PlayerCallback callback) {
this.callback = callback;
}

/**
* Background task to start MediaPlayer. It is used because starting playing of remote stream may
* take long time and we must not block UI thread.
* Also, this approach allows to cancel starting process (not implemented in current version)
*/
private class Player extends AsyncTask<String, Void, Void> {

/**
* This function called in UI thread and we callback to activity to update UI elements
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
if (callback != null) {
callback.onPreStart();
}
}

/**
* This function called in UI thread and we callback to activity to update UI elements
*/
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
// Start MediaPlayer fail.
if (callback != null) {
callback.onStartFailed();
}
} else {
if (callback != null) {
callback.onStarted();
}
}
}

/**
* This function called from separate thread and we do long-time operation in it
*
* @param strings params
* @return null
*/
@Override
protected Void doInBackground(String... strings) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.start();
} catch (Exception e) {
if (mediaPlayer != null) {
mediaPlayer.release();
}
mediaPlayer = null;
}
return null;
}
}
}

最佳答案

您必须等到流准备好后才能调用start()。当您想使用 prepareAsync() 时,您应该将 OnPrepareListener 设置为媒体播放器并调用 start()

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();

关于java - (Android Media Player) 为什么不能使用prepareAsync()而不是prepre()与AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32445136/

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