Soundpool:样品没准备好

时间:2012-12-21 05:26:12

标签: android gstreamer soundpool

我已将Android中的默认媒体框架从Stagefright更改为Gstreamer。这样做是为了使我们的项目更加灵活。

但是,当我运行一些apks时,应用程序的所有声音都会在应用程序启动时播放,并且在Soundpool中显示错误“sample#not READY”之后无法播放。例如,在App Baby钢琴中,当我启动应用程序时,钢琴音节的声音会被播放,当我进入播放模式后实际点击钢琴时,它就不会被播放。

我认为问题在于,当声音被加载到Soundpool中时,会创建Gstreamer Mediaplayer对象并播放它并在应用程序启动时完成。

在显示的日志中,Sample Channel Count(0)超出范围。它发生在以下部分的SoundPoool.cpp文件中。

status_t Sample::doLoad() {
    uint32_t sampleRate;
    int numChannels;
    int format;
    sp<IMemory> p;
    LOGW("Start decode");
    if (mUrl) {
        p = MediaPlayer::decode(mUrl, &sampleRate, &numChannels, &format);
    } else {
        p = MediaPlayer::decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format);
        LOGW("close(%d)", mFd);
        ::close(mFd);
        mFd = -1;
    }
    if (p == 0) {
        LOGE("Unable to load sample: %s", mUrl);
        return -1;
    }
    LOGW("pointer = %p, size = %u, sampleRate = %u, numChannels = %d",
            p->pointer(), p->size(), sampleRate, numChannels);

    if (sampleRate > kMaxSampleRate) {
       LOGE("Sample rate (%u) out of range", sampleRate);
       return - 1;
    }

    if ((numChannels < 1) || (numChannels > 2)) {
        LOGE("Sample channel count (%d) out of range", numChannels);
        return - 1;
    }

    //_dumpBuffer(p->pointer(), p->size());
    uint8_t* q = static_cast<uint8_t*>(p->pointer()) + p->size() - 10;
    //_dumpBuffer(q, 10, 10, false);

    mData = p;
    mSize = p->size();
    mSampleRate = sampleRate;
    mNumChannels = numChannels;
    mFormat = format;
    mState = READY;
    return 0; }

并且MediaPlayerService decode函数从

下面的代码部分返回所有值为null
sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
{
    LOGD("decode(%d, %lld, %lld)", fd, offset, length);
    sp<MemoryBase> mem;
    sp<MediaPlayerBase> player;

    player_type playerType = getPlayerType(fd, offset, length);
    LOGD("player type = %d", playerType);

    // create the right type of player
    sp<AudioCache> cache = new AudioCache("decode_fd");
    player = android::createPlayer(playerType, cache.get(), cache->notify);
    if (player == NULL) goto Exit;
    if (player->hardwareOutput()) goto Exit;

    static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);

    // set data source
    if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;

    LOGD("prepare");
    player->prepareAsync();

    LOGD("wait for prepare");
    if (cache->wait() != NO_ERROR) goto Exit;

    LOGD("start");
    player->start();

    LOGD("wait for playback complete");
    if (cache->wait() != NO_ERROR) goto Exit;

    mem = new MemoryBase(cache->getHeap(), 0, cache->size());
    *pSampleRate = cache->sampleRate();//Nes
    *pNumChannels = cache->channelCount();
    *pFormat = cache->format();

    LOGD("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);

Exit:
    if (player != 0) player->reset();
    ::close(fd);
    return mem;
}

样本率,通道等从此函数返回值为0。

此后播放样本时,显示错误“”sample#not READY“

int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
        int priority, int loop, float rate)
{
    LOGW("sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
            sampleID, leftVolume, rightVolume, priority, loop, rate);
    sp<Sample> sample;
    SoundChannel* channel;
    int channelID;

    // scope for lock
    {
        Mutex::Autolock lock(&mLock);

        // is sample ready?
        sample = findSample(sampleID);
        if ((sample == 0) || (sample->state() != Sample::READY)) {
            LOGW("  sample %d not READY", sampleID);
            return 0;
        }

        dump();

        // allocate a channel
        channel = allocateChannel(priority);

        // no channel allocated - return 0
        if (!channel) {
            LOGW("No channel allocated");
            return 0;
        }

        channelID = ++mNextChannelID;
    }

    LOGW("channel state = %d", channel->state());
    channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate);
    return channelID;
}

是否有解决此问题的方法..Plz帮助..

3 个答案:

答案 0 :(得分:1)

您也可以使用此方法

public void loadSound (String strSound, int stream) {
     boolean loaded = false;
     mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
            }
        });
    try {
          stream= mSoundPool.load(aMan.openFd(strSound), 1);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

样品未准备好通常表示尚未加载,即它仍在加载。 (也许您切换到的框架可能会将其加载更长时间。)

您应该订阅其onLoadCompleteListener,当您收到回叫时,声音就可以播放了。在此之前,它将无法播放。

答案 2 :(得分:0)

我对Android 2.0有同样的问题,并使用.ogg格式而不是.mp3格式解决我使用的声音here。我希望这能解决你的问题。