我正在尝试制作一个演示Android MediaPlayer使用的简单应用。应用程序布局显示“选择”按钮,TextView和“播放”按钮。用户单击选择按钮,TextView(到目前为止成功)显示URI,播放按钮应启动MediaPlayer。 但我不知道如何让onActivityResult()在任何地方发送路径。
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
这是Sound构造函数:
public class Sound {
public String path;
// constructor
public Sound () {
path = null;
}
public void setPath(String filePath) {
path = filePath;
}
public String getPath() {
return path;
}
}
我不知道如何在不启动onActivityResult内部的MediaPlayer的情况下将onActivityResult的路径发送到playButtonListener。有一个简单的解决方案,我只是不知道吗?
答案 0 :(得分:0)
您在onActivityResult中创建的Sound对象是一个局部变量,它会覆盖您的全局变量。从
更改以下行 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
到
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
另一个选择是甚至不打扰创建声音对象,因为你的playButtonListener应该访问文本视图并在那里获取URL。这两种方法都应该足够好。
祝你好运