diff options
author | stom79 <tschneider.ac@gmail.com> | 2018-10-20 10:56:35 +0200 |
---|---|---|
committer | stom79 <tschneider.ac@gmail.com> | 2018-10-20 10:56:35 +0200 |
commit | 0fc0165d848b0eb4afd61573a59f51ea2b5b3ec1 (patch) | |
tree | 8dd850942bfa3be3331e192d1f23411c4c3c37e2 /app/src/main/java | |
parent | dab47f43ca956cf055f104d6c5afe5296847beda (diff) |
Allow to change video resolution
Diffstat (limited to 'app/src/main/java')
4 files changed, 132 insertions, 37 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java index 8a03247b7..a4ec8cf1f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java @@ -31,6 +31,7 @@ import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Html; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -81,8 +82,9 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube private RelativeLayout loader; private TextView peertube_view_count, peertube_like_count, peertube_dislike_count, peertube_share, peertube_download, peertube_description, peertube_title; private ScrollView peertube_information_container; - private ProgressDialog pDialog; - public static final int progress_bar_type = 0; + private MediaPlayer mediaPlayer; + private FullScreenMediaController fullScreenMediaController; + private int stopPosition; @Override protected void onCreate(Bundle savedInstanceState) { @@ -146,6 +148,8 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube } } + + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_webview, menu); @@ -217,7 +221,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube loader.setVisibility(View.GONE); return; } - if( apiResponse.getPeertubes().get(0).getFileUrl() == null){ + if( apiResponse.getPeertubes().get(0).getFileUrl(null) == null){ Toast.makeText(PeertubeActivity.this, R.string.toast_error,Toast.LENGTH_LONG).show(); loader.setVisibility(View.GONE); return; @@ -232,7 +236,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube peertube_like_count.setText(String.valueOf(peertube.getLike())); peertube_view_count.setText(String.valueOf(peertube.getView())); - Uri uri = Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl()); + Uri uri = Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null)); try { HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory()); } catch (KeyManagementException e) { @@ -241,17 +245,21 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube e.printStackTrace(); } videoView.setVideoURI(uri); - FullScreenMediaController mc = new FullScreenMediaController(PeertubeActivity.this); - mc.setAnchorView(videoView); - videoView.setMediaController(mc); - videoView.start(); + videoView.getCurrentPosition(); + fullScreenMediaController = new FullScreenMediaController(PeertubeActivity.this, peertube); + fullScreenMediaController.setAnchorView(videoView); + videoView.setMediaController(fullScreenMediaController); + mediaPlayer = MediaPlayer.create(PeertubeActivity.this, uri); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { loader.setVisibility(View.GONE); - //mp.start(); } }); + videoView.setZOrderOnTop(true); + videoView.setMediaController(fullScreenMediaController); + videoView.start(); + peertube_download.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -259,10 +267,10 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube if (ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(PeertubeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE); } else { - manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl()); + manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null)); } }else{ - manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl()); + manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null)); } } }); @@ -328,4 +336,31 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube } } + + @Override + protected void onPause() { + super.onPause(); + if( videoView != null) { + stopPosition = videoView.getCurrentPosition(); //stopPosition is an int + videoView.pause(); + } + } + + @Override + public void onResume(){ + super.onResume(); + if( videoView != null) { + videoView.seekTo(stopPosition); + videoView.resume(); + videoView.start(); + } + } + public void changeVideoResolution(Peertube peertube, String resolution){ + int position = videoView.getCurrentPosition(); + mediaPlayer.stop(); + videoView.setVideoURI(Uri.parse(peertube.getFileUrl(resolution))); + fullScreenMediaController.setResolutionVal(resolution + "p"); + videoView.seekTo(position); + mediaPlayer.start(); + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index b4ab04f8a..7b9170885 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -2416,14 +2416,13 @@ public class API { } catch (ParseException e) { e.printStackTrace(); } - JSONArray files = resobj.getJSONArray("files"); + ArrayList<String> resolutions = new ArrayList<>(); for(int j = 0 ; j < files.length() ; j++){ JSONObject attObj = files.getJSONObject(j); - peertube.setFileDownloadUrl(attObj.get("fileDownloadUrl").toString()); - peertube.setFileUrl(attObj.get("fileUrl").toString()); - peertube.setTorrentDownloadUrl(attObj.get("torrentDownloadUrl").toString()); + resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); } + peertube.setResolution(resolutions); } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Peertube.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Peertube.java index 3c90766d2..4d6d5828e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Peertube.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Peertube.java @@ -15,6 +15,7 @@ package fr.gouv.etalab.mastodon.client.Entities; import java.util.Date; +import java.util.List; /** * Created by Thomas on 29/09/2018. @@ -29,7 +30,6 @@ public class Peertube { private String thumbnailPath; private String previewPath; private String embedPath; - private String fileDownloadUrl; private int view; private int like; private int dislike; @@ -37,8 +37,8 @@ public class Peertube { private int duration; private String instance; private Account account; - private String fileUrl; - private String torrentDownloadUrl; + private List<String> resolution; + public Peertube() { } @@ -157,27 +157,36 @@ public class Peertube { this.account = account; } - public String getFileUrl() { - return fileUrl; + public String getFileUrl(String resolution) { + if( resolution == null) + resolution = this.getResolution().get(0); + if(resolution == null) + return null; + return "https://" + this.instance + "/static/webseed/" + getUuid()+ "-" + resolution + ".mp4"; } - public void setFileUrl(String fileUrl) { - this.fileUrl = fileUrl; - } - public String getTorrentDownloadUrl() { - return torrentDownloadUrl; - } + public String getTorrentDownloadUrl(String resolution) { + if( resolution == null) + resolution = this.getResolution().get(0); + if(resolution == null) + return null; + return "https://" + this.instance + "/download/torrents/" + getUuid()+ "-" + resolution + ".torrent"; - public void setTorrentDownloadUrl(String torrentDownloadUrl) { - this.torrentDownloadUrl = torrentDownloadUrl; + } + public String getFileDownloadUrl(String resolution) { + if( resolution == null) + resolution = this.getResolution().get(0); + if(resolution == null) + return null; + return "https://" + this.instance + "/download/videos/" + getUuid()+ "-" + resolution + ".mp4"; } - public String getFileDownloadUrl() { - return fileDownloadUrl; + public List<String> getResolution() { + return resolution; } - public void setFileDownloadUrl(String fileDownloadUrl) { - this.fileDownloadUrl = fileDownloadUrl; + public void setResolution(List<String> resolution) { + this.resolution = resolution; } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/FullScreenMediaController.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/FullScreenMediaController.java index 3ebafa17f..904e950ed 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/FullScreenMediaController.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/FullScreenMediaController.java @@ -17,14 +17,20 @@ package fr.gouv.etalab.mastodon.helper; import android.content.Context; import android.content.res.Resources; +import android.graphics.Color; +import android.support.v7.widget.PopupMenu; import android.view.Gravity; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; -import android.widget.FrameLayout; +import android.widget.Button; import android.widget.ImageButton; import android.widget.MediaController; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.activities.PeertubeActivity; +import fr.gouv.etalab.mastodon.client.Entities.Peertube; + /** * Created by Thomas on 14/10/2018. @@ -33,6 +39,10 @@ import fr.gouv.etalab.mastodon.activities.PeertubeActivity; public class FullScreenMediaController extends MediaController { private ImageButton fullScreen; + private Button resolution; + private Context context; + private Peertube peertube; + private String resolutionVal; public enum fullscreen{ OFF, @@ -40,8 +50,14 @@ public class FullScreenMediaController extends MediaController { } public FullScreenMediaController(Context context) { super(context); + this.context = context; } + public FullScreenMediaController(Context context, Peertube peertube) { + super(context); + this.peertube = peertube; + this.context = context; + } @Override public void setAnchorView(View view) { @@ -50,15 +66,47 @@ public class FullScreenMediaController extends MediaController { //image button for full screen to be added to media controller fullScreen = new ImageButton(super.getContext()); - - FrameLayout.LayoutParams params = - new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams params = + new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - params.gravity = Gravity.RIGHT; + params.gravity = Gravity.END; params.rightMargin = 80; params.topMargin = 22; addView(fullScreen, params); + if( resolutionVal == null) + resolutionVal = peertube.getResolution().get(0) +"p"; + resolution = new Button(super.getContext()); + resolution.setAllCaps(false); + resolution.setBackgroundColor(Color.TRANSPARENT); + resolution.setText(resolutionVal); + resolution.setPadding(0,0,0,0); + LayoutParams paramsButton = + new LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT); + paramsButton.gravity = Gravity.START; + paramsButton.rightMargin = 80; + paramsButton.topMargin = 22; + addView(resolution, paramsButton); + resolution.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + PopupMenu popup = new PopupMenu(context, resolution); + int i = 0; + for(String res: peertube.getResolution()){ + MenuItem item = popup.getMenu().add(0, i, Menu.NONE, res); + item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + ((PeertubeActivity)context).changeVideoResolution(peertube, res); + resolution.setText(String.format("%sp",res)); + return false; + } + }); + } + popup.show(); + } + }); if(((PeertubeActivity)getContext()).getFullscreen() == fullscreen.ON){ Resources resources = getResources(); fullScreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen_exit)); @@ -83,6 +131,10 @@ public class FullScreenMediaController extends MediaController { }); } + public void setResolutionVal(String resolutionVal){ + this.resolutionVal = resolutionVal; + } + private void changeIcon(){ //fullscreen indicator from intent if(((PeertubeActivity)getContext()).getFullscreen() == fullscreen.ON){ |