How to do I check when a YouTube video has finished playing in the official YouTube Android app? In the LogCat I see things such as: VideoStage: MEDIA_PLAYING_VIDEO. Is there any way to detect when this finishes?
You could use MediaPlayer. It has an isPlaying method to check if music/videos are currently playing. Just set a Boolean to true while it's playing and check the isPlaying method against that Boolean to see if it changed from True to False.
Otherwise Youtube (or google) may have some specific API's
EDIT:
I looked into here; and it appears that there is an API for youtube.
you can use the method PlayerStateChangeListener for the YoutubePlayerclass to get the listener and implement the method OnVideoEnded
Related
I have a small app that plays sequential sounds (a teaching app playing the sillables of a word)
This could be accomplished by firing an event right after each sound stopped playing. Something like:
var sounds = new Array(new Audio("1.mp3"), new Audio("2.mp3"));
var i = -1;
playSnd();
function playSnd() {
i++;
if (i == sounds.length) return;
sounds[i].addEventListener('ended', playSnd);
sounds[i].play();
}
(source)
However, now android chrome has implemented some new restrictions on how to play sound: Sound events must all be fired by a user action.
So, when I run code very similar to the above, the first sound plays, and then I get
Uncaught (in promise) DOMException: play() can only be initiated by a user gesture.
How can a sequence of sounds, determined at run time, be played on Android's Chrome?
To start with, Google Chrome on Android has been having the limitation of not allowing applications to play HTML audio(s) without an explicit action by the user. However, it is different than how stock browser(s), in most cases, handles it.
The reason, as Chromium Org puts it, is that, Autoplay is not honored on android as it will cost data usage.
You may find more details on the same here.
Apart from the fact that this results in wastage of bandwidth, this also makes some sense, since mobile devices are used in public and in houses, where unsolicited sound from random Web sites could be a nuisance.
However, in the later versions, this idea was over ruled and Chrome on Android started allowing autoplay of HTML audios and videos on it. Again after a set of reviews and discussions, this feature was reverted to what it was, making it mandatory for a user action to invoke HTML audios and videos on the Chrome for Android.
Here is something that I found more on the same. As it says, the reason stated was that "We're going to gather some data about how users react to autoplaying videos in order to decide whether to keep this restriction". And hence the playing option without a user action was reverted back.
You can also find more about the blocking of _autoplay of audio(s) and video(s) here on Forbes and The Verge.
However, this is something that I can suggest you to try which will help you achieve what you intend to. All you have to do is copy this code and paste in your Chrome for Android. This helps you reset the flag which is default set to not allowing to play HTML audios and videos without user interaction:
chrome://flags/#disable-gesture-requirement-for-media-playback
OR
about:flags/#disable-gesture-requirement-for-media-playback
If the above procedure doesn't help/work for you, you can do this:
Go into chrome://flags OR about:flags (this will direct you to chrome://flags) and Enable the "Disable gesture requirement for media playback" option (which is actually the same as the above URL specified).
Steps to reproduce the problem:
Start a application with a in-app WebView.
Call webview.loadUrl("http://n-g.nxtomo.hk/CYvsCP/app.html?nocache=true");
Click the web page once time and start to play a background music.
Press HOME button that the app will go to background, but the music keep playing.
Expected the WebView being pause when the activity go to background.
I've tried to call:
webview.onResume();
webview.onPause();
or
Class.forName("android.webkit.WebView").getMethod("onResume", (Class[]) null).invoke(webview, (Object[]) null);
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webview, (Object[]) null);
or
wv.resumeTimers();
wv.pauseTimers();
Both ways are not working (means no effect and keep playing sound in background) for Android 4.4.2 and 5.0 , I just have two devices.
And I found a suggestion is to remove the MediaPlayer's callback that force stop the sound play.
#Override
protected void onPause() {
super.onPause();
((AudioManager) getSystemService(
Context.AUDIO_SERVICE)).requestAudioFocus(
null,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
It's work for stop the sound.
BUT! When I resume the app that the sound will not auto play continually. So, I'm looking for a officially way to fix this problem. :(
Refer to
[Issue 532359: Cannot pause HTML5 sound on Android in-app WebView when application in background. - Sep 16, 2015] https://code.google.com/p/chromium/issues/detail?id=532359
[Issue 10282: Public API for WebView.onPause and WebView.onResume - Aug 7, 2010] https://code.google.com/p/android/issues/detail?id=10282
I have the exact same problem and looking for an official fix.
Only workaround I have found was to
loadUrl("");
but it's far from a good fix. As soon as you multitask the page reloads, this can lead to a lot of problems if you need the webview session to be kept alive or persistent.
Reply from Google:
https://code.google.com/p/chromium/issues/detail?id=532359#c5
Hm, if you only want to change the Android app, you're probably out of
luck with what you can do there.
If you can change the page, you may look into page visibility API to
pause/resume playback (Tim may know if it's enabled for WebView or
not).
But ultimately, since it's WebView, your app can communicate with the
page and cause it to play/pause playback in the app's activity
onPause/onResume.
I am using this tutorial and the RemoteController to control the currently playing music player:
http://forum.xda-developers.com/showthread.php?t=2579415
Is there a way to get the application/package of the currently playing music application? I want to be able to open it.
I'm sure it is possible somehow as Aviate manages to do it, but afaict this class does not provide that information:
https://developer.android.com/reference/android/media/RemoteController.OnClientUpdateListener.html
Maybe there is some unrelated way of getting the currently playing music player...
Thanks :)
I worked it out... had to use reflection on the RemoteController though.
Method method = mRemoteController.getClass().getDeclaredMethod("getRemoteControlClientPackageName");
if (method != null) {
return (String) method.invoke(mRemoteController);
}
i am developing one application which
needs to play an audio song to callee when call is lifted by callee,
and automatically call has to be disconnected automatically when audio
song playing completed . i tried to find in android API for any
classes or methods to do this, but failed to find... please help me
how to do this..
your answer could be helpful to me...please do reply
Thank you in advance..
AFAIK, it is not possible to play a sound in the ongoing call and also to automatically dosconnect a call. Telephont API doesnt provide any methods to do any od these. So both od these requirement can't be fullfilled in ANdroid.
I've used the voice recognition feature on Android and I love it. It's one of my customers' most praised features. However, the format is somewhat restrictive. You have to call the recognizer intent, have it send the recording for transcription to google, and wait for the text back.
Some of my ideas would require recording the audio within my app and then sending the clip to google for transcription.
Is there any way I can send an audio clip to be processed with speech to text?
I got a solution that is working well to have speech recognizing and audio recording. Here is the link to a simple Android project I created to show the solution's working. Also, I put some print screens inside the project to illustrate the app.
I'm gonna try to explain briefly the approach I used. I combined two features in that project: Google Speech API and Flac recording.
Google Speech API is called through HTTP connections. Mike Pultz gives more details about the API:
"(...) the new [Google] API is a full-duplex streaming API. What this means, is that it actually uses two HTTP connections- one POST request to upload the content as a “live” chunked stream, and a second GET request to access the results, which makes much more sense for longer audio samples, or for streaming audio."
However, this API needs to receive a FLAC sound file to work properly. That makes us to go to the second part: Flac recording
I implemented Flac recording in that project through extracting and adapting some pieces of code and libraries from an open source app called AudioBoo. AudioBoo uses native code to record and play flac format.
Thus, it's possible to record a flac sound, send it to Google Speech API, get the text, and play the sound that was just recorded.
The project I created has the basic principles to make it work and can be improved for specific situations. In order to make it work in a different scenario, it's necessary to get a Google Speech API key, which is obtained by being part of Google Chromium-dev group. I left one key in that project just to show it's working, but I'll remove it eventually. If someone needs more information about it, let me know cause I'm not able to put more than 2 links in this post.
Unfortunately not at this time. The only interface currently supported by Android's voice recognition service is the RecognizerIntent, which doesn't allow you to provide your own sound data.
If this is something you'd like to see, file a feature request at http://b.android.com. This is also tangentially related to existing issue 4541.
As far as I know there is still no way to directly send an audio clip to Google for transcription. However, Froyo (API level 8) introduced the SpeechRecognizer class, which provides direct access to the speech recognition service. So, for example, you can start playback of an audio clip and have your Activity start the speech recognizer listening in the background, which will return results after completion to a user-defined listener callback method.
The following sample code should be defined within an Activity since SpeechRecognizer's methods must be run in the main application thread. Also you will need to add the RECORD_AUDIO permission to your AndroidManifest.xml.
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
if (available) {
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new RecognitionListener() {
#Override
public void onResults(Bundle results) {
// process results here
}
// define your other overloaded listener methods here
});
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// the following appears to be a requirement, but can be a "dummy" value
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.dummy");
// define any other intent extras you want
// start playback of audio clip here
// this will start the speech recognizer service in the background
// without starting a separate activity
sr.startListening(intent);
}
You can also define your own speech recognition service by extending RecognitionService, but that is beyond the scope of this answer :)