2016-05-27 02:21:04 +00:00
|
|
|
# Android tutorial 5: A Complete media player
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
## Goal!
|
|
|
|
|
|
|
|
![screenshot]
|
2016-05-16 14:30:34 +00:00
|
|
|
|
|
|
|
This tutorial wants to be the “demo application” that showcases what can
|
|
|
|
be done with GStreamer in the Android platform.
|
|
|
|
|
|
|
|
It is intended to be downloaded in final, compiled, form rather than
|
|
|
|
analyzed for its pedagogical value, since it adds very little GStreamer
|
2016-11-05 08:18:49 +00:00
|
|
|
knowledge over what has already been shown in [](tutorials/android/media-player.md).
|
2016-06-17 00:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
**FIXME: Do we want to provide a binary of the app?**
|
|
|
|
|
|
|
|
## Introduction
|
2016-05-16 14:30:34 +00:00
|
|
|
|
|
|
|
The previous tutorial already implemented a basic media player. This one
|
|
|
|
simply adds a few finishing touches. In particular, it adds the
|
|
|
|
capability to choose the media to play, and disables the screensaver
|
|
|
|
during media playback.
|
|
|
|
|
|
|
|
These are not features directly related to GStreamer, and are therefore
|
|
|
|
outside the scope of these tutorials. Only a few implementation pointers
|
|
|
|
are given here.
|
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
## Registering as a media player
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
The `AndroidManifest.xml` tells the Android system the capabilities of
|
|
|
|
the application. By specifying in the `intent-filter` of the activity
|
|
|
|
that it understands the `audio/*`, `video/*` and `image/*` MIME types,
|
2016-05-16 14:30:34 +00:00
|
|
|
the tutorial will be offered as an option whenever an application
|
|
|
|
requires such medias to be viewed.
|
|
|
|
|
|
|
|
“Unfortunately”, GStreamer knows more file formats than Android does,
|
|
|
|
so, for some files, Android will not provide a MIME type. For these
|
2016-06-17 00:01:54 +00:00
|
|
|
cases, a new `intent-filter` has to be provided which ignores MIME types
|
2016-05-16 14:30:34 +00:00
|
|
|
and focuses only in the filename extension. This is inconvenient because
|
|
|
|
the list of extensions can be large, but there does not seem to be
|
|
|
|
another option. In this tutorial, only a very short list of extensions
|
|
|
|
is provided, for simplicity.
|
|
|
|
|
|
|
|
Finally, GStreamer can also playback remote files, so URI schemes like
|
2016-06-17 00:01:54 +00:00
|
|
|
`http` are supported in another `intent-filter`. Android does not
|
2016-05-16 14:30:34 +00:00
|
|
|
provide MIME types for remote files, so the filename extension list has
|
|
|
|
to be provided again.
|
|
|
|
|
|
|
|
Once we have informed the system of our capabilities, it will start
|
|
|
|
sending
|
|
|
|
[Intents](http://developer.android.com/reference/android/content/Intent.html)
|
|
|
|
to invoke our activity, which will contain the desired URI to play. In
|
2016-06-17 00:01:54 +00:00
|
|
|
the `onCreate()` method the intent that invoked the activity is
|
2016-05-16 14:30:34 +00:00
|
|
|
retrieved and checked for such URI.
|
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
## Implementing a file chooser dialog
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
The UI includes a new button ![media-next) which
|
2016-11-05 08:18:49 +00:00
|
|
|
was not present in [](tutorials/android/media-player.md). It
|
2016-05-16 14:30:34 +00:00
|
|
|
invokes a file chooser dialog (based on the [Android File
|
|
|
|
Dialog](http://code.google.com/p/android-file-dialog/) project) that
|
|
|
|
allows you to choose a local media file, no matter what extension or
|
|
|
|
MIME type it has.
|
|
|
|
|
|
|
|
If a new media is selected, it is passed onto the native code (which
|
2016-05-27 18:19:02 +00:00
|
|
|
will set the pipeline to READY, pass the URI onto `playbin`, and bring
|
2016-05-16 14:30:34 +00:00
|
|
|
the pipeline back to the previous state). The current position is also
|
|
|
|
reset, so the new clip does not start in the previous position.
|
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
## Preventing the screen from turning off
|
2016-05-16 14:30:34 +00:00
|
|
|
|
|
|
|
While watching a movie, there is typically no user activity. After a
|
|
|
|
short period of such inactivity, Android will dim the screen, and then
|
|
|
|
turn it off completely. To prevent this, a [Wake
|
|
|
|
Lock](http://developer.android.com/reference/android/os/PowerManager.WakeLock.html)
|
|
|
|
is used. The application acquires the lock when the Play button is
|
|
|
|
pressed, so the screen is never turned off, and releases it when the
|
|
|
|
Pause button is pressed.
|
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
## Conclusion
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
This finishes the series of Android tutorials. Each one of the
|
|
|
|
preceding tutorials has evolved on top of the previous one, showing
|
|
|
|
how to implement a particular set of features, and concluding in this
|
|
|
|
tutorial 5. The goal of tutorial 5 is to build a complete media player
|
|
|
|
which can already be used to showcase the integration of GStreamer and
|
|
|
|
Android.
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-17 00:01:54 +00:00
|
|
|
It has been a pleasure having you here, and see you soon!
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-11-05 08:18:49 +00:00
|
|
|
[screenshot]: images/tutorials/android-a-complete-media-player-screenshot.png
|
2016-06-17 00:01:54 +00:00
|
|
|
[media-next]: images/media-next.png
|