I'm trying to make a simple app that demonstrates the use of android MediaPlayer. The app layout displays a "choose" Button, a TextView, and a "play" Button. The user clicks the choose button, the TextView (successfully so far) displays the URI and the play button should start the MediaPlayer. But I don't know how to get onActivityResult() to send the path anywhere.
public class MainActivity extends Activity {
private static final int MUSIC_ID = 1;
Intent intent;
Button chooseButton;
Button playButton;
TextView pathText;
MediaPlayer mp;
Sound sound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set our buttons in the layout
chooseButton = (Button) findViewById(R.id.choose);
playButton = (Button) findViewById(R.id.play);
// and an TextView that will display the path
pathText = (TextView) findViewById(R.id.pathText);
// setOnClickListeners to the buttons
chooseButton.setOnClickListener(chooseButtonListener);
playButton.setOnClickListener(playButtonListener);
} // end onCreate
private OnClickListener chooseButtonListener = new OnClickListener(){
// When chooseButton is clicked, make an Intent that will get
// the path of the file and run startActivityForResult method
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(intent, MUSIC_ID);
}
};
protected final void onActivityResult(int requestCode, int resultCode, Intent data) {
// if everything went well, get the URI path from the intent.
if(resultCode == RESULT_OK && requestCode == MUSIC_ID){
Uri uri = data.getData();
// try to set the TextView to path (this works) and convert the URI
try{
pathText.setText(uri.toString()); // Here the path displays fine
Sound sound = new Sound(); // Make a Sound object
sound.setPath(uri.toString()); // set the path of the object
}
catch(Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Null!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
private OnClickListener playButtonListener = new OnClickListener(){
// When the playButton is clicked, try to create a
// MediaPlayer that will use the path of the Sound Object
// or otherwise display a toast.
@Override
public void onClick(View v) {
try {
mp.setDataSource(sound.getPath()); // get the path of the object
mp.prepare();
mp.setLooping(true);
mp.start();
}
catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Null!", Toast.LENGTH_SHORT);
toast.show();
}
}
};
} // end MainActivity
Here is the Sound constructor:
public class Sound {
public String path;
// constructor
public Sound () {
path = null;
}
public void setPath(String filePath) {
path = filePath;
}
public String getPath() {
return path;
}
}
I don't know how to send the path from onActivityResult to the playButtonListener without starting the MediaPlayer immediately inside onActivityResult. Is there a simple fix that I'm just ignorant of?
The Sound object that you are creating inside the onActivityResult is a local variable and it is overriding your global variable. Change the below lines from
pathText.setText(uri.toString()); // Here the path displays fine
Sound sound = new Sound(); // Make a Sound object
sound.setPath(uri.toString()); // set the path of the object
to
pathText.setText(uri.toString()); // Here the path displays fine
sound = new Sound(); // Make a Sound object
sound.setPath(uri.toString()); // set the path of the object
The other option is to not even bother creating the sound object as your playButtonListener should be access the text view and get the URL right there. Either approach should be good enough.
Good luck