使用AACDecoder启动新流

时间:2014-07-11 09:46:52

标签: java android oop android-mediaplayer audiotrack

我正在做一个无线电应用程序(Tune In like)

情况:

  • 当收音机首先开始播放音频AD然后启动收音机(AD和收音机是2个不同的流)
  • 当AACPlayer当前正在播放音乐且用户更换电台时

两个问题:

  • 广告完成后,AACPlayer需要停止并以其他网址启动
  • 当用户选择另一台收音机时,它是相同的

使用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
}

我无法找到解决方案停止() - >在已播放流的播放器上启动(网址),同时保护播放器的状态(我必须考虑网络的延迟)

我如何实现这一目标?

修改

以下是对事情进展情况的快速了解:

[ PlayerActivity - > StreamingService - >的 PlayerObject

按顺序:

点击按钮播放 - >从StreamingService调用方法playRadio通过实例获取来自serviceConnection->来自PlayerObject的调用方法播放 - > playerState =开始 - >方法playerStarted auto-called - > playerState =已启动 - >来自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)

1 个答案:

答案 0 :(得分:1)

我认为应该通过回调函数来完成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有一个指向Activity源的链接: https://code.google.com/p/aacplayer-android/source/browse/trunk/src/com/spoledge/aacplayer/AACPlayerActivity.java

你应该注意到在回调函数中使用了一个Handler,而一个代码是在不在玩家线程中执行的单独线程中执行的:

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

链接到simmilar问题:

Android Application crashes after generating an audio signal a few times

AudioFlinger could not create track. status: -12