willblaschko / AlexaAndroid

A library and sample app to abstract access to the Amazon Alexa service for Android applications.
GNU General Public License v2.0
280 stars 133 forks source link

how to pause/stop when the application is speaking #139

Closed wantitmore closed 6 years ago

wantitmore commented 6 years ago

for example,when the app is speaking flash briefing,I want to pause it,how to do that?

prasannakumark commented 6 years ago

@wantitmore , we can do it with mediaPlayer.pause()/stop() And for start again use mediaPlayer.play();

wantitmore commented 6 years ago

thanks @prasannakumark , but now I am not sure where to set mediaPlayer.pause()/stop() and resume it .had you work it successfully?

prasannakumark commented 6 years ago

Yes. Already code is available in AlexaAudioPlayer class. Just we have to init object we can call those method.

ankitmaisuriya commented 6 years ago

I have implemented wakeword with the my android application.Its working fine. I want to stop listing if nothing is recorded by the mic. so again I can say Alexa, Open...., Please help me out.How can i stop listing in android app.

Thanks

prasannakumark commented 6 years ago

@ankitmaisuriya using pause score RawAudioRecorder can stop listening . FYI,

private DataRequestBody requestBody = new DataRequestBody() {
        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            if(BuildConfig.DEBUG){
                Log.i(TAG, "Received audio");
            }

            while (mRawAudioRecorder != null && !mRawAudioRecorder.isPausing()) {
                if(mRawAudioRecorder != null && mRawAudioRecorder.getState() == AudioRecorder.State.ERROR) {
                    LogUtils.i(TAG,"Recorder timeout.");
                    return;
                }

                if(mRawAudioRecorder != null) {
                    if(sink != null && mRawAudioRecorder != null) {
                        sink.write(mRawAudioRecorder.consumeRecording());
                    }
                }

                try {
                    Thread.sleep(TimeUtils.RAW_AUDIO_RECORDER_SLEEP_TIME);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //Once loop exit its stop listening.
        }
    };

//code for stop listing

public void stopListening(){
        LogUtils.d(TAG, "stopListening");

        if(mRawAudioRecorder != null) {
            mRawAudioRecorder.stop();
            if(mRawAudioRecorder != null) {
                mRawAudioRecorder.release();
            }
            mRawAudioRecorder = null;
        }
    }

You can use above method for stop listing.

wantitmore commented 6 years ago

@prasannakumark HI,now I can stop the speaking of playing news, but when I insert a command,such as weather quary,when the weather response end, I want to resume the playing news response from last speaking position, how to do that? thx

prasannakumark commented 6 years ago

@wantitmore , you have to maintain three separate queue for that. please follow the multi-turn concept

https://developer.amazon.com/docs/alexa-voice-service/interaction-model.html#voice-lifecycle

chowdary60 commented 6 years ago

@prasannakumark

Hi , I compiled the sample app ,but when i try to get the weather info,"what is weather is novi,michigan". Alexa saying something and asking for location again, if tell the location "novi" again, alexa saying "i don't know that one" and also it saying open the alexa app and enter the zip code. how did you manage to get the weather information?

prasannakumark commented 6 years ago

@chowdary60 ,

I think audio is not clear so Alexa will give response as per its understand. Do test using https://alexa.amazon.in/spa/index.html#settings/dialogs and login with same account As per your LWA. Ideally we check here all request and response. Here request and responses are correct then there cloud be some code issues. Anyhow we can fix if any code issues.

chowdary60 commented 6 years ago

Hello @prasannakumark , thank you for the quick response ,i have managed to get the weather information. now i have this problem. when alexa speaking something, when i said stop it's not stop speaking. as you mentioned AlexaAudiopplayer has method pause() and stop to do that. in sample app we have somethng like in BaseActivity.java class

    } else if (current instanceof AvsSpeakItem) {
        //play a sound file
        if (!audioPlayer.isPlaying()) {
            audioPlayer.playItem((AvsSpeakItem) current);
        }
        setState(STATE_SPEAKING);
    } else if (current instanceof AvsStopItem) {
        //stop our play
        Log.i("BaseActivity","stop play");
        audioPlayer.stop();
        avsQueue.remove(current);
    } else if (current instanceof AvsReplaceAllItem) {
        //clear all items
        //mAvsItemQueue.clear();

        audioPlayer.stop();
        avsQueue.remove(current);

}

when i looked in to the AvsStopItem, it saying to "Directive to stop device playback" . when will this callback will be called and how do we stop the response when alexa giving some response?

when alexa speaking something and and if you try to interrupt or ask something,it going to Queue and after the alexa done with speaking earlier response only it's loking at Queue and send the item from the queue to aws cloud to get some response.

@wantitmore achieved to stop the the alexa response when he say "stop" but i coudn't. Do i need any changes to existing code in BaseActivity.java or in any other class.

i have linked my amazon music account in alexa app settings and i am testing in emulator. alexa saying"amazon music not supported in this device." but i have installed the amazon music app and able to play songs in the emulator it self. what could be the problem?

is there a way to to achieve this? Thanks, Madhu

wantitmore commented 6 years ago

@chowdary60 , sorry, until now ,I can not also get the "stop" response, when I say "stop" during the music is playing. If you know why, please tell me =_=

prasannakumark commented 6 years ago

Hi @wantitmore , @chowdary60

Basically we have to send current item of information in context obj while making "SpeechRecognizer" event.So that Alexa will come to know does audioplayer is playing or not. If is playing/pause there will get stop directive as response. Once receive stop directive clear your complete queue and reset your audioplayer.

Please find below link for FYI, https://forums.developer.amazon.com/questions/108032/stop-directive-not-working.html?childToView=146570

wantitmore commented 6 years ago

@prasannakumark thx,it works well

chowdary60 commented 6 years ago

@wantimore I have also tried ad @prasannakumar said But couldn't able to achieve. Can u attach the code what did u change to get "stop" working. Thank, Madhu.

On Fri, Mar 30, 2018, 6:05 AM wantitmore notifications@github.com wrote:

Closed #139 https://github.com/willblaschko/AlexaAndroid/issues/139.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/willblaschko/AlexaAndroid/issues/139#event-1549523956, or mute the thread https://github.com/notifications/unsubscribe-auth/AbUKquKfEsCwXG4BphexRphcz3PLo7vKks5tjgNYgaJpZM4R2vs9 .