Skip to content

📲 Android frontend library for Amplify Video, a category plug-in for AWS Amplify that makes it easy to deploy live and file-based streaming video services.

License

Notifications You must be signed in to change notification settings

alextyner/amplify-video-android

Repository files navigation

Amplify Video for Android

Frontend library for the Amplify Video Plugin.

New to AWS Amplify or Amplify Video? Consider following one of the tutorials or getting started guides first.

Getting Started

  1. Create a new Android Studio project with an Empty Activity.

Select a Template

  1. Give your project a name and finish project creation.

Configure Project

  1. At the top level of your project directory, initialize an Amplify project and an Amplify Video live streaming resource. Follow the Getting Started with Live guide.

  2. Open the the build.gradle script for your new app module.

Screen Shot 2020-07-29 at 3.13.49 PM

  1. Modify the dependencies section to include amplify-android, amplify-android-extended and amplify-video-android:
dependencies {
    // ... other depedencies ...

    implementation 'com.amplifyframework:core:1.0.0'
    implementation 'com.amplifyframework:extended:0.1.7'
    implementation 'com.amplify-video:aws-video:0.1.9'
}
  1. Because Amplify Android uses Java 8 features, modify the android section to include these compile options:
android {
    // ... other configuration ...
  
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
  1. In the onCreate() method of your MainActivity class, initialize Amplify Extended with the Video plugin:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            AmplifyExtended.addCategory(new VideoCategory(), new VideoCategoryConfiguration(), "amplifyvideoconfiguration");
            AmplifyExtended.addPlugin(AmplifyExtended.category("video"), new AWSVideoPlugin());
            AmplifyExtended.configure(getApplicationContext());
        } catch (AmplifyException error) {
            Log.e("MyVideoApp", "Couldn't initialize Amplify Extended.");
        }
      
    }
  1. Add a VideoView to your app's layout. In res/layout/activity_main.xml, replace the TextView with an AWSLiveVideoView called myVideoView:
    <com.amplifyframework.video.ui.AWSLiveVideoView
        android:id="@+id/myVideoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  1. Add the internet permission to manifests/AndroidManifest.xml outside the <application> tag:
    <uses-permission android:name="android.permission.INTERNET" />
  1. Extend your onCreate() method in the MainActivity class to attach your new view to an Amplify Video resource.

    Replace mylivestream with the name you gave your new resource at Step 3. You can use amplify status to list all of your project resources.

        // ... code to initialize Amplify Extended ...

        AWSLiveVideoView videoView = findViewById(R.id.myVideoView);
        AWSLiveVideoPlayer player = videoView.getPlayer();
        VideoCategory amplifyVideo = AmplifyExtended.category("video");
        LiveResource videoResource = amplifyVideo.getLiveResource("mylivestream");
        player.attach(videoResource);
        player.play();
  1. If you haven't already, begin streaming to the MediaLive ingest URI provided by Amplify Video.

    The URI can be viewed again by using amplify video get-info.

  2. Launch your app and view the live stream.

Going Further

Handling Events

To be notified about player events, implement the VideoPlayer.Listener interface and register your listener:

    player.addListener(myListener);

Supported events:

  • State Change (to any of Idle, Preparing, Ready, Playing, Buffering, Ended)
  • Preparing
  • Ready
  • Seek
  • Play
  • Pause
  • Buffering Start
  • Buffering Complete
  • Playback End
  • Screen Touch

Adding Analytics

To enable sending analytics events to AWS Pinpoint, you must add the analytics category to your project.

  1. From your project's root directory, use the Amplify CLI tool to add analytics.
$ amplify add analytics
  1. Accept the default prompts and provision the AWS resources.
$ amplify push
  1. Modify the initialization of Amplify Extended in your MainActivity class to also initialize Amplify with the analytics and auth categories.
        try {
            AmplifyExtended.addCategory(new VideoCategory(), new VideoCategoryConfiguration(), "amplifyvideoconfiguration");
            AmplifyExtended.addPlugin(AmplifyExtended.category("video"), new AWSVideoPlugin());
            AmplifyExtended.configure(getApplicationContext());
          
            Amplify.addPlugin(new AWSCognitoAuthPlugin());
            Amplify.addPlugin(new AWSPinpointAnalyticsPlugin(new Application()));
            Amplify.configure(getApplicationContext());
        } catch (AmplifyException error) {
            Log.e("MyVideoApp", "Couldn't initialize Amplify and Amplify Extended.");
        }
  1. Provide a copy of your analytics category to the Amplify Video player at any time to enable all default analytics:
        player.addAnalytics(Amplify.Analytics);

Using a Custom Video View

Any class that extends VideoView can be used to display Amplify Video streams (even VideoView itself).

  1. Instead of adding an AWSLiveVideoView to your res/layout/activity_main.xml file, replace it with a VideoView.
    <VideoView
        android:id="@+id/myVideoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  1. Instead of the code from Step 10 in Getting Started above, attach the player to your view like this:
        // ... code to initialize Amplify Extended ...

        AWSLiveVideoView videoView = findViewById(R.id.myVideoView);
        AWSLiveVideoPlayer player = new AWSLiveVideoPlayer(videoView); // <-- different
        VideoCategory amplifyVideo = AmplifyExtended.category("video");
        LiveResource videoResource = amplifyVideo.getLiveResource("mylivestream");
        player.attach(videoResource);
        player.play();

License

This library is licensed under the Apache 2.0 License.

Report a Bug

We appreciate your feedback -- comments, questions, and bug reports. Please submit a GitHub issue.

Contribute to the Project

Please see the Contributing Guidelines.

About

📲 Android frontend library for Amplify Video, a category plug-in for AWS Amplify that makes it easy to deploy live and file-based streaming video services.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages