Estimados, tengo una función llamada Reproductor()
, el fin de esta es utilizar la clase MediaPlayer
, tanto como para reproducir como para darle pause.
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import java.io.IOException;
public class Reproductor extends Activity {
public String urlMediaPlayer ="url-media";
public MediaPlayer mediaPlayer = new MediaPlayer();
ProgressBar progressBar;
public void onCreate(Bundle savedInstance) {
System.out.println("Reproductor, onCreate");
super.onCreate(savedInstance);
setContentView(R.layout.activity_container);
this.progressBar = (ProgressBar) findViewById(R.id.fabProgressBar);
reproduceMedia();
}
public Reproductor() {
}
public void reproduceMedia()
{
cargaMedia cmedia = new cargaMedia();
cmedia.execute();
}
public class cargaMedia extends AsyncTask<Void,Integer,Boolean> {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Boolean aBoolean) {
progressBar.setVisibility(View.INVISIBLE);
}
@Override
protected Boolean doInBackground(Void... params) {
try{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(urlMediaPlayer);
mediaPlayer.prepare();
mediaPlayer.setVolume(1,1);
mediaPlayer.start();
}catch (IOException e)
{
e.printStackTrace();
}
return true;
}
}
public void pausaMedia() throws IOException{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(urlMediaPlayer);
mediaPlayer.setVolume(0,0);
mediaPlayer.pause();
}
}
Esta funcion es llamada desde la clase ContainerActivity
, presionando un FloatingActionButton
.
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;
import cl.cooperativa.readxmlfrominternetmaterial.R;
import cl.cooperativa.readxmlfrominternetmaterial.Reproductor;
import
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.DeportesFragment;
import
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.HomeFragment;
import
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.PoliticaFragment;
public class ContainerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottonbar);
bottomBar.setDefaultTab(R.id.portada);
HomeFragment homeFragment = new HomeFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,homeFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null).commit();
FloatingActionButton floatingActionButton = (FloatingActionButton)
findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Reproductor reproductor = new Reproductor();
// reproductor.reproduceMedia();
Intent intent = new Intent(ContainerActivity.this,
Reproductor.class);
startActivity(intent);
/*Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
}
});
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
switch (tabId){
case R.id.portada:
HomeFragment homeFragment = new HomeFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,homeFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null).commit();
break;
case R.id.deportes:
DeportesFragment deportesFragment = new DeportesFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,deportesFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null).commit();
break;
case R.id.politica:
PoliticaFragment politicaFragment = new PoliticaFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,politicaFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null).commit();
break;
}
}
});
}
@Override
protected void onPause()
{
super.onPause();
}
protected void onResume() {
super.onResume();
}
protected void onDestroy() {
super.onDestroy();
}
}
Deseo que por mientras se carga el audio me muestre el progressbar
, y una vez esté listo, desaparezca y me muestre un botón pause. Pero con el código que tengo al presionar en el floating
actionbutton
me envía un error de NullPointerException
.
PID:936
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2090) at cl.cooperativa.readxmlfrominternetmaterial.Reproductor. (Reproductor.java:21) at cl.cooperativa.readxmlfrominternetmaterial.view.ContainerActivity$1.onClick(ContainerActivity.java:42) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
¿Qué estoy haciendo mal?