Ajuda com Jsoup e AsyncTask
Bom basicamente eu já estou com do de cabeça por causa desta p****, o plano era baixar uma página web para pegar o atributo src (e futuramente outros) para usar como url no video player.
Código do Player:
Código da página de download:
E o código da Activity principal:
Bom basicamente, quando vou executar um vídeo direto pela url dele ele executa normalmente, porém em alguns dispositivos android (pelo menos no meu J2) não da pra copiar a url do player, então fiz um sistema simples para pegar a url do atributo src da tag vídeo para usar no src do player, só que sempre da este maldito error:
Código do Player:
package com.samuelives.videoplayer; import android.content.Intent; import android.content.pm.ActivityInfo; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.WindowManager; import android.widget.MediaController; import android.widget.VideoView; import com.samuelives.videoplayer.system.PageDownloader; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import java.io.IOException; /** * Created by sives on 17/08/2017. */ public class Player extends AppCompatActivity{ private PageDownloader page; @Override protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.activity_player_offline); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); VideoView mVideoView = (VideoView)findViewById(R.id.vVideoPlayerOffline); MediaController mc = new MediaController(this); Intent it = getIntent(); String mUrl = it.getStringExtra("URLVideo"); Log.i("Video Player URL: ", mUrl); //Src do video String mSrc = mUrl; //verifica se é a url de um video if(!(mUrl.contains(".mp4") || mUrl.contains(".3gp") || mUrl.contains(".MP4") || mUrl.contains(".3GP"))){ page = new PageDownloader(mUrl); page.execute(); mSrc = page.getVideoSrc(); Log.i("SRC do vídeo", mSrc); }else { mSrc = mUrl; } //Url do video Uri url = Uri.parse(mSrc); //Video Player mVideoView.setMediaController(mc); mVideoView.setVideoURI(url); mVideoView.start(); } }
Código da página de download:
package com.samuelives.videoplayer.system; import android.os.AsyncTask; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import java.io.IOException; /** * Created by sives on 18/08/2017. */ public class PageDownloader extends AsyncTask<String, Void, String>{ private String mUrl, mSrc; private Document page; public PageDownloader(String url){ this.mUrl = url; } public String getVideoSrc(){ return mSrc; } @Override protected String doInBackground(String... strings) { try { page = Jsoup.connect(mUrl).get(); Element video = page.select("video").first(); //Seta para o src mSrc = video.attr("src"); } catch (IOException e) { e.printStackTrace(); } return null; } }
E o código da Activity principal:
package com.samuelives.videoplayer; import android.content.Intent; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.View; import android.widget.EditText; import com.mikepenz.materialdrawer.Drawer; import com.mikepenz.materialdrawer.DrawerBuilder; import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; public class Main extends AppCompatActivity { private Drawer mNavDrawer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar tbMain = (Toolbar)findViewById(R.id.tbMain); tbMain.setTitle("Video Player+"); tbMain.setTitleTextColor(Color.parseColor("#F5F5F5")); setSupportActionBar(tbMain); //Items PrimaryDrawerItem itemOffline = new PrimaryDrawerItem().withIdentifier(1).withName("Salvos Offline").withTextColor(Color.parseColor("#263238")).withIcon(R.mipmap.ic_file_download_black_24px); //Navigation Drawer mNavDrawer = new DrawerBuilder() .withActivity(this) .withToolbar(tbMain) .withDisplayBelowStatusBar(true) .withActionBarDrawerToggleAnimated(true) .withDrawerGravity(Gravity.LEFT) .withSavedInstance(savedInstanceState) .withSelectedItem(0) .addDrawerItems( itemOffline ) .build(); } public void openPlayer(View v){ EditText editurl = (EditText)findViewById(R.id.editText); Intent player = new Intent(this, Player.class); player.putExtra("URLVideo", editurl.getText().toString()); startActivity(player); } }
Bom basicamente, quando vou executar um vídeo direto pela url dele ele executa normalmente, porém em alguns dispositivos android (pelo menos no meu J2) não da pra copiar a url do player, então fiz um sistema simples para pegar a url do atributo src da tag vídeo para usar no src do player, só que sempre da este maldito error:
FATAL EXCEPTION: main Process: com.samuelives.videoplayer, PID: 21207 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samuelives.videoplayer/com.samuelives.videoplayer.Player}: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.i(Log.java:160) at com.samuelives.videoplayer.Player.onCreate(Player.java:50) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.
Samuel Ives
Curtidas 0