con un intent selecciono un archivo mp3 y guardo con URI la localización en preferencias compartidas tal como encontré en Google; en otro activity se lanza un elemento media player que rescata el uri. Pero...
- le doy play a reproducirlo y funciona
- cierro y abro app
- doy en el botón play y truena la app
- vuelvo a seleccionar un audio y ya funciona
El problema parece estar al cerrar y abrir. ¿Por qué?
Éste es el código:
public class MainActivity extends AppCompatActivity {
public SharedPreferences prefs;
Button mp3,play;
int u;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs=getSharedPreferences("MisPreferencias",this.MODE_PRIVATE);
mp3=findViewById(R.id.mp3);
play=findViewById(R.id.play);
mp3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
elijemp3(v);
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,musica.class);
startActivity(i);
}
});
}
public void elijemp3(View view){
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("audio");
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Toast.makeText(this,file.getPath(),Toast.LENGTH_LONG).show();
Toast.makeText(this,file.toString(),Toast.LENGTH_LONG).show();*/
Intent intent = new Intent();
intent.setType("audio/mp3");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Open Audio (mp3) file"), 1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK){
if ((data != null) && (data.getData() != null)){
Uri audioFileUri = data.getData();
***String s=getRealPathFromUri(this,audioFileUri);***
SharedPreferences.Editor editor = prefs.edit();
***editor.putString("urime", s);***
editor.apply();
Toast.makeText(this,s,Toast.LENGTH_LONG).show();
}
}
}
public String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
ESTE EL ES OTRO activity
public class musica extends AppCompatActivity {
public SharedPreferences prefs;
public Uri urim;
MediaPlayer mediaPlayer,mp;
public String urimp3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_musica);
prefs=getSharedPreferences("MisPreferencias",this.MODE_PRIVATE);
urimp3 = prefs.getString("urime", null);
urim=Uri.parse(urimp3);
playOnOffSound();
// urim=Uri.parse("sdcard/bep.mp3");
/* mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), urim);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}*/
}
private void playOnOffSound(){
mp = MediaPlayer.create(this, urim);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
/*if(mediaPlayer!=null){
mediaPlayer.release();
mediaPlayer = null;
}*/ if(mp!=null){
mp.release();
mp = null;
}
}
@Override
protected void onResume() {
super.onResume();
/* if(mediaPlayer!=null)
{
mediaPlayer.start();
}*/ if(mp!=null)
{
mp.start();
}
}
@Override
protected void onPause() {
super.onPause();
/* if(mediaPlayer!=null)
{
mediaPlayer.pause();
}*/ if(mp!=null)
{
mp.pause();
}
/// musica.pause();
}
}
Nada del otro mundo pero, ¿por qué no funciona? Éste es el error que recibo:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
Ya intenté esta otra forma y nada. Recibo un nuevo error:
Éste es el código:
public class MainActivity extends AppCompatActivity {
public SharedPreferences prefs;
Button stop,play,examinar;
String FilePathAudio;
TextView url;
private final int PICKER=1;
private int state=1;
private final int PLAYING=1;
private final int PAUSING=2;
Uri FilePath;
MediaPlayer mediaPlayer=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// mediaPlayer=MediaPlayer.create(this,R.raw.mp3);
// mediaPlayer.setLooping(true);
stop=findViewById(R.id.stop);
play=findViewById(R.id.play);
examinar=findViewById(R.id.examinar);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (state)
{
case PLAYING:
mediaPlayer.start();
state=PAUSING;
play.setText("Pause");
break;
case PAUSING:
mediaPlayer.pause();
state=PLAYING;
play.setText("Play");
break;
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
play.setText("Play");
}
});
examinar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickfile();
}
});
}
private void pickfile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "selecione un audio"), PICKER);
} catch (android.content.ActivityNotFoundException ex) {
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICKER:
if (requestCode == RESULT_OK) {
FilePath = data.getData();
FilePathAudio=getRealPathFromUri(FilePath);
}
try{
mediaPlayer.setDataSource(FilePathAudio);
//mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
public String getRealPathFromUri (Uri contentUri){
Cursor cursor = null;
try {
String[] proj = {MediaStore.Audio.Media.DATA};
cursor = getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}