Well, this is the onClick
method
of a button
:
public void aggiungi (View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(Intent.createChooser(intent, "Select"), PICK_AUDIO_REQUEST_CODE);
}
And outside I've handled the output:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_AUDIO_REQUEST_CODE){
if (resultCode == RESULT_OK){
Uri uri_to_use = data.getData();
setAudioToButton(uri_to_use);
}
}
}
And here's the setAudioToButton method
called
public void setAudioToButton(final Uri audio){
PercentRelativeLayout percentRelativeLayout = (PercentRelativeLayout)findViewById(R.id.main_layout);
PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(43, 12);
layoutParams.addRule(PercentRelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(PercentRelativeLayout.BELOW, R.id.quattro);
Button button = new Button(this);
button.setLayoutParams(layoutParams);
PercentRelativeLayout.LayoutParams layoutParams1 = (PercentRelativeLayout.LayoutParams)button.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams1.getPercentLayoutInfo();
percentLayoutInfo.heightPercent = 0.12f;
percentLayoutInfo.widthPercent = 0.43f;
percentLayoutInfo.topMarginPercent = 0.063f;
percentLayoutInfo.leftMarginPercent = 0.007f;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null){mediaPlayer.stop();}
try {mediaPlayer.setDataSource(getBaseContext(), audio);} catch (IOException error){error.printStackTrace();}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});
button.setText("start");
percentRelativeLayout.addView(button);
}
The problem is when I click on the new button to make it sound, I have a NullPointerException
:
08-22 02:32:43.164 11880-11880/com.example.utente.usefulsounds E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.utente.usefulsounds, PID: 11880
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(android.content.Context, android.net.Uri)' on a null object reference
at com.example.utente.usefulsounds.MainActivity$1.onClick(MainActivity.java:182)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
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:5461)
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)
I am wondering why this happens, I can't understand
**
**
I've put this in the onCreate
method
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST_CODE);
}
But it didn't worked
Now I've tried in a different way: intead of passing an Uri
to the MediaPlayer
, I've passed a filedescriptor
:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null){mediaPlayer.stop();}
File file = new File(audio.getPath());
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
try {mediaPlayer.setDataSource(fileInputStream.getFD());} catch (IOException error){error.printStackTrace();}
}catch (IOException ioe){button.setText("FILE ERROR");}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});
The button appears with the text "select", so there isn't any problem with the file
(otherwise it should have been "file error"), but when I click on that I still have a crash caused by a NullPointerException
with mediaPlayer
. Why this? I can't understand
Instead of doing a setDataSource()
method
you have to use simply a create()
method
:)
Eg.
MediaPlayer.create(getApplicationContext(), yourAudioUri).start();
Happy coding and have fun :)