我正在广播应用程序(如调入)
情况:
当收音机首先启动时,它将播放音频AD,然后启动收音机(AD和收音机是2个不同的流)
当AACPlayer当前正在播放音乐并且用户更改电台时
两个问题:
广告结束后,AACPlayer需要停止并以另一个URL开头
当用户选择另一个收音机时,它是相同的
使用AACDecoder库,只有两种方法
-开始()
- 停()
这些方法可能会相互干扰并使玩家处于不确定状态
我使用以下代码启动播放器:
public void start(String url) throws Exception {
if (mPlayer == null) {
mPlayer = new MultiPlayer(this);
}
mPlayer.playAsync(url);
mState = StreamingState.PREPARING;
}
并像这样停止它:
public void stop() {
if (mPlayer == null) {
return;
}
try {
mPlayer.stop();
mPlayer = null;
} catch (Exception e) {
}
setState(StreamingState.STOPPING);
}
我使用这个回调接口:
static interface RadioPlayerCallBack {
public void radioPlayerStarted();
public void radioPlayerStopped(int perf);
public void radioPlayerException(Throwable e);
public void radioPlayerMetadata(String key, String value);
public void radioPlayerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs, int audioBufferCapacityMs);
}
我用这个枚举更新播放器的状态:
public enum StreamingState {
EMPTY, // media player rested or releasedS
CREATED, // created ready to prepare
PREPARING, // preparing...
PREPARED, // prepared
STARTED, // started, and maybe playing (ready to play)
PAUSED, // paused (media player ready!)
STOPPED, // stopped and not prepared to play
ERROR, // an error occured, mediaplayer is reseted
STOPPING, // startAsync after stop
}
我找不到在确保播放器状态的同时对已经播放流的播放器执行Stop()-> start(url)的解决方案(我必须考虑网络的延迟)
我该如何实现?
编辑
这是完成事情的快速视图:
[PlayerActivity-> StreamingService-> PlayerObject]
为了 :
单击按钮play->从streamingService调用方法playRadio通过从serviceConnection获得的实例->从PlayerObject调用方法play-> playerState = starting->方法playerStarted自动调用-> playerState = started->从StreamingService回调方法->广播意图通知UI-> ui获取意图并更新界面...
这是BroadcastReceiver:
final private BroadcastReceiver mPlayBackReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String actionString = intent.getAction();
if (actionString == ServiceStreaming.SERVICE_PLAY) {
} else if (actionString == ServiceStreaming.SERVICE_STOP) {
} else if (actionString == ServiceStreaming.SERVICE_LOADING) {
} else if (actionString == ServiceStreaming.SERVICE_ERROR) {
} else if (actionString == ServiceStreaming.SERVICE_KILLED) {
}
}
};
Somethimes我得到这个例外:
06-04 10:15:43.531: E/AudioTrack(8218): AudioFlinger could not create track, status: -12
06-04 10:15:43.531: E/AudioTrack-JNI(8218): Error initializing AudioTrack
06-04 10:15:43.531: E/android.media.AudioTrack(8218): Error code -20 when initializing AudioTrack.
06-04 10:15:43.531: E/PCMFeed(8218): error in playback feed: -3
06-04 10:15:43.541: E/BufferReader(8218): Exception when reading: java.net.SocketException: Socket closed
06-04 10:15:43.901: A/libc(8218): Fatal signal 11 (SIGSEGV) at 0x76652748 (code=1), thread 8955 (Thread-5266)
06-04 10:15:43.911: E/AACPlayer(8218): playAsync():
06-04 10:15:43.911: E/AACPlayer(8218): java.lang.IllegalStateException
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.Decoder.start(Decoder.java:231)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.playImpl(AACPlayer.java:424)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.play(AACPlayer.java:386)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.play(AACPlayer.java:338)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer$1.run(AACPlayer.java:296)
06-04 10:15:43.911: E/AACPlayer(8218): at java.lang.Thread.run(Thread.java:841)
最佳答案
我认为应该通过回调函数playercallback接口来完成,可以在这里找到:
https://code.google.com/p/aacplayer-android/source/browse/trunk/src/com/spoledge/aacplayer/PlayerCallback.java
并且AACPlayer本身的功能是:
/**
* Sets the PlayerCallback.
* NOTE: this should be set BEFORE any of the play methods are called.
*/
public void setPlayerCallback( PlayerCallback playerCallback )
然后在播放器停止时执行此功能:
/**
* This method is called when the player is stopped.
* Note: __after__ this method the method playerException might be also called.
*/
public void playerStopped( int perf );
只需将重新启动逻辑放在此函数中即可。
链接到aacPlayer的来源:
https://code.google.com/p/aacplayer-android/source/browse/trunk/src/com/spoledge/aacplayer/AACPlayer.java
编辑1
有使用aacPlayer指向活动来源的链接:
https://code.google.com/p/aacplayer-android/source/browse/trunk/src/com/spoledge/aacplayer/AACPlayerActivity.java
您应该注意到,在回调函数中使用了Handler,并且在单独的线程中执行的代码不在players线程中执行:
public void playerStopped( final int perf ) {
uiHandler.post( new Runnable() {
public void run() {
enableButtons();
...
playerStarted = false;
}
});
}
Somethimes我得到这个例外:
06-04 10:15:43.531: E/AudioTrack(8218): AudioFlinger could not create track, status: -12
06-04 10:15:43.531: E/AudioTrack-JNI(8218): Error initializing AudioTrack
06-04 10:15:43.531: E/android.media.AudioTrack(8218): Error code -20 when initializing AudioTrack.
06-04 10:15:43.531: E/PCMFeed(8218): error in playback feed: -3
06-04 10:15:43.541: E/BufferReader(8218): Exception when reading: java.net.SocketException: Socket closed
06-04 10:15:43.901: A/libc(8218): Fatal signal 11 (SIGSEGV) at 0x76652748 (code=1), thread 8955 (Thread-5266)
06-04 10:15:43.911: E/AACPlayer(8218): playAsync():
06-04 10:15:43.911: E/AACPlayer(8218): java.lang.IllegalStateException
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.Decoder.start(Decoder.java:231)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.playImpl(AACPlayer.java:424)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.play(AACPlayer.java:386)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer.play(AACPlayer.java:338)
06-04 10:15:43.911: E/AACPlayer(8218): at com.spoledge.aacdecoder.AACPlayer$1.run(AACPlayer.java:296)
06-04 10:15:43.911: E/AACPlayer(8218): at java.lang.Thread.run(Thread.java:841)
编辑2
链接到类似问题:
Android Application crashes after generating an audio signal a few times
AudioFlinger could not create track. status: -12