Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #29 from novoda/demo-audio-selection
Browse files Browse the repository at this point in the history
Demo audio track selection
  • Loading branch information
jszmltr authored Jun 2, 2017
2 parents 9ad3b0e + edc5b32 commit 304ee4a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
49 changes: 48 additions & 1 deletion demo/src/main/java/com/novoda/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
package com.novoda.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

import com.novoda.noplayer.ContentType;
import com.novoda.noplayer.Player;
import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.PlayerState;
import com.novoda.noplayer.PlayerView;
import com.novoda.noplayer.player.PlayerFactory;
import com.novoda.noplayer.player.PrioritisedPlayers;
import com.novoda.notils.caster.Views;
import com.novoda.notils.logger.simple.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

private static final String URI_VIDEO_MP4 = "http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-85.mp4";
private static final String URI_VIDEO_MPD = "https://storage.googleapis.com/content-samples/multi-audio/multi-audio.mpd";
private static final String URI_VIDEO_MPD = "https://storage.googleapis.com/content-samples/multi-audio/manifest.mpd";

private Player player;
private PlayerView playerView;
private View audioSelectionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.setShowLogs(true);
setContentView(R.layout.activity_main);
playerView = (PlayerView) findViewById(R.id.player_view);
audioSelectionButton = Views.findById(this, R.id.button_audio_selection);
}

@Override
Expand All @@ -39,15 +50,51 @@ public void onPrepared(PlayerState playerState) {
player.play();
}
});

player.attach(playerView);

audioSelectionButton.setOnClickListener(showAudioSelectionDialog);

Uri uri = Uri.parse(URI_VIDEO_MPD);
player.loadVideo(uri, ContentType.DASH);
}

private final View.OnClickListener showAudioSelectionDialog = new View.OnClickListener() {

@Override
public void onClick(View v) {
showAudioSelectionDialog();
}

private void showAudioSelectionDialog() {
final List<PlayerAudioTrack> audioTracks = player.getAudioTracks();
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.list_item);
adapter.addAll(mapAudioTrackToLabel(audioTracks));
AlertDialog audioSelectionDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Select audio track")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
PlayerAudioTrack audioTrack = audioTracks.get(position);
player.selectAudioTrack(audioTrack);
}
}).create();
audioSelectionDialog.show();
}

private List<String> mapAudioTrackToLabel(List<PlayerAudioTrack> audioTracks) {
List<String> labels = new ArrayList<>();
for (PlayerAudioTrack audioTrack : audioTracks) {
labels.add("Group: " + audioTrack.groupIndex() + " Format: " + audioTrack.formatIndex());
}
return labels;
}
};

@Override
protected void onStop() {
super.onStop();
player.release();
audioSelectionButton.setOnClickListener(null);
}
}
30 changes: 25 additions & 5 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<com.novoda.noplayer.NoPlayerView
android:id="@+id/player_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/button_audio_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audio" />

</LinearLayout>

<com.novoda.noplayer.NoPlayerView
android:id="@+id/player_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

</LinearLayout>

</merge>
9 changes: 9 additions & 0 deletions demo/src/main/res/layout/list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:textSize="20sp"
android:padding="8dp" />

0 comments on commit 304ee4a

Please sign in to comment.