先从AACDecoder新流(Start new stream with AACDecoder)

2019-10-20 10:39发布

我正在做一个无线应用(调等)

的情况:

  • 当无线电在启动第一它播放音频AD,然后启动无线电(AD和收音机2个diferents流)
  • 当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
}

我无法找到一个解决方案做停止() -已经播放的流播放器>开始(URL),同时确保玩家的状态(我必须在考虑网络的延迟)

我怎样才能做到这一点?

编辑

下面是如何做事的快速视图:

[PlayerActivity - > StreamingService - > PlayerObject]

为了 :

点击播放按钮 - >调用方法playRadio从StreamingService通过实例从serviceConnection->调用方法该剧从PlayerObject-> playerState =开始 - >法playerStarted自动叫 - > playerState =开始 - >从StreamingService回调方法 - >广播意图告知UI - > UI获得的意图和更新接口...

这里是广播接收器:

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)

Answer 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到活动的源的链接: https://code.google.com/p/aacplayer-android/source/browse/trunk/src/com/spoledge/aacplayer/AACPlayerActivity.java

你应该注意到,在回调函数处理程序使用和代码是在玩家线程不执行单独的线程中执行:

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应用程序崩溃信号几次

AudioFlinger无法创建轨道。 状态:-12



文章来源: Start new stream with AACDecoder