66import android .content .Intent ;
77import android .content .SharedPreferences ;
88import android .net .Uri ;
9+ import android .os .Bundle ;
910import android .preference .Preference ;
1011import android .preference .PreferenceManager ;
1112import android .support .v4 .view .MenuItemCompat ;
@@ -50,6 +51,7 @@ public class ActionBarHandler {
5051 private String webisteUrl = "" ;
5152 private AppCompatActivity activity ;
5253 private VideoInfo .VideoStream [] videoStreams = null ;
54+ private VideoInfo .AudioStream audioStream = null ;
5355 private int selectedStream = -1 ;
5456 private String videoTitle = "" ;
5557
@@ -75,18 +77,18 @@ public void setupNavMenu(AppCompatActivity activity) {
7577 activity .getSupportActionBar ().setNavigationMode (ActionBar .NAVIGATION_MODE_LIST );
7678 }
7779
78- public void setStreams (VideoInfo .VideoStream [] streams ) {
79- this .videoStreams = streams ;
80+ public void setStreams (VideoInfo .VideoStream [] videoStreams , VideoInfo . AudioStream [] audioStreams ) {
81+ this .videoStreams = videoStreams ;
8082 selectedStream = 0 ;
81- String [] itemArray = new String [streams .length ];
83+ String [] itemArray = new String [videoStreams .length ];
8284 String defaultResolution = defaultPreferences
8385 .getString (context .getString (R .string .defaultResolutionPreference ),
8486 context .getString (R .string .defaultResolutionListItem ));
8587 int defaultResolutionPos = 0 ;
8688
87- for (int i = 0 ; i < streams .length ; i ++) {
88- itemArray [i ] = streams [i ].format + " " + streams [i ].resolution ;
89- if (defaultResolution .equals (streams [i ].resolution )) {
89+ for (int i = 0 ; i < videoStreams .length ; i ++) {
90+ itemArray [i ] = VideoInfo . getNameById ( videoStreams [i ].format ) + " " + videoStreams [i ].resolution ;
91+ if (defaultResolution .equals (videoStreams [i ].resolution )) {
9092 defaultResolutionPos = i ;
9193 }
9294 }
@@ -99,6 +101,27 @@ public void setStreams(VideoInfo.VideoStream[] streams) {
99101 ,new ForamatItemSelectListener ());
100102 ab .setSelectedNavigationItem (defaultResolutionPos );
101103 }
104+
105+ // set audioStream
106+ audioStream = null ;
107+ String preferedFormat = PreferenceManager .getDefaultSharedPreferences (context )
108+ .getString (context .getString (R .string .defaultAudioFormatPreference ), "webm" );
109+ if (preferedFormat .equals ("webm" )) {
110+ for (VideoInfo .AudioStream s : audioStreams ) {
111+ if (s .format == VideoInfo .I_WEBMA ) {
112+ audioStream = s ;
113+ }
114+ }
115+ } else if (preferedFormat .equals ("m4a" )){
116+ for (VideoInfo .AudioStream s : audioStreams ) {
117+ Log .d (TAG , VideoInfo .getMimeById (s .format ) + " : " + Integer .toString (s .bandWidth ));
118+ if (s .format == VideoInfo .I_M4A &&
119+ (audioStream == null || audioStream .bandWidth > s .bandWidth )) {
120+ audioStream = s ;
121+ Log .d (TAG , "last choosen" );
122+ }
123+ }
124+ }
102125 }
103126
104127 private void selectFormatItem (int i ) {
@@ -159,6 +182,9 @@ public boolean onItemSelected(MenuItem item, Context context) {
159182 case R .id .action_play_with_kodi :
160183 playWithKodi ();
161184 break ;
185+ case R .id .menu_item_play_audio :
186+ playAudio ();
187+ break ;
162188 default :
163189 Log .e (TAG , "Menu Item not known" );
164190 }
@@ -179,7 +205,7 @@ public void playVideo() {
179205 try {
180206 intent .setAction (Intent .ACTION_VIEW );
181207 intent .setDataAndType (Uri .parse (videoStreams [selectedStream ].url ),
182- "video/" + videoStreams [selectedStream ].format );
208+ VideoInfo . getMimeById ( videoStreams [selectedStream ].format ) );
183209 context .startActivity (intent ); // HERE !!!
184210 } catch (Exception e ) {
185211 e .printStackTrace ();
@@ -216,29 +242,17 @@ public void onClick(DialogInterface dialog, int which) {
216242 public void downloadVideo () {
217243 Log .d (TAG , "bla" );
218244 if (!videoTitle .isEmpty ()) {
219- String suffix = "" ;
220- switch (videoStreams [selectedStream ].format ) {
221- case VideoInfo .F_WEBM :
222- suffix = ".webm" ;
223- break ;
224- case VideoInfo .F_MPEG_4 :
225- suffix = ".mp4" ;
226- break ;
227- case VideoInfo .F_3GPP :
228- suffix = ".3gp" ;
229- break ;
230- }
231- DownloadManager dm = (DownloadManager ) context .getSystemService (Context .DOWNLOAD_SERVICE );
232- DownloadManager .Request request = new DownloadManager .Request (
233- Uri .parse (videoStreams [selectedStream ].url ));
234- request .setDestinationUri (Uri .fromFile (new File (
235- defaultPreferences .getString ("download_path_preference" , "/storage/emulated/0/NewPipe" )
236- + "/" + videoTitle + suffix )));
237- try {
238- dm .enqueue (request );
239- } catch (Exception e ) {
240- e .printStackTrace ();
241- }
245+ String videoSuffix = "." + VideoInfo .getSuffixById (videoStreams [selectedStream ].format );
246+ String audioSuffix = "." + VideoInfo .getSuffixById (audioStream .format );
247+ Bundle args = new Bundle ();
248+ args .putString (DownloadDialog .FILE_SUFFIX_VIDEO , videoSuffix );
249+ args .putString (DownloadDialog .FILE_SUFFIX_AUDIO , audioSuffix );
250+ args .putString (DownloadDialog .TITLE , videoTitle );
251+ args .putString (DownloadDialog .VIDEO_URL , videoStreams [selectedStream ].url );
252+ args .putString (DownloadDialog .AUDIO_URL , audioStream .url );
253+ DownloadDialog downloadDialog = new DownloadDialog ();
254+ downloadDialog .setArguments (args );
255+ downloadDialog .show (activity .getSupportFragmentManager (), "downloadDialog" );
242256 }
243257 }
244258
@@ -282,4 +296,34 @@ public void onClick(DialogInterface dialog, int which) {
282296 }
283297 }
284298 }
299+
300+ public void playAudio () {
301+ Intent intent = new Intent ();
302+ try {
303+ intent .setAction (Intent .ACTION_VIEW );
304+ intent .setDataAndType (Uri .parse (audioStream .url ),
305+ VideoInfo .getMimeById (audioStream .format ));
306+ context .startActivity (intent ); // HERE !!!
307+ } catch (Exception e ) {
308+ e .printStackTrace ();
309+ AlertDialog .Builder builder = new AlertDialog .Builder (context );
310+ builder .setMessage (R .string .noPlayerFound )
311+ .setPositiveButton (R .string .installStreamPlayer , new DialogInterface .OnClickListener () {
312+ @ Override
313+ public void onClick (DialogInterface dialog , int which ) {
314+ Intent intent = new Intent ();
315+ intent .setAction (Intent .ACTION_VIEW );
316+ intent .setData (Uri .parse (context .getString (R .string .fdroidVLCurl )));
317+ context .startActivity (intent );
318+ }
319+ })
320+ .setNegativeButton (R .string .cancel , new DialogInterface .OnClickListener () {
321+ @ Override
322+ public void onClick (DialogInterface dialog , int which ) {
323+ Log .i (TAG , "You unlocked a secret unicorn." );
324+ }
325+ });
326+ builder .create ().show ();
327+ }
328+ }
285329}
0 commit comments