我是 android 编程新手,现在我的应用程序出现问题。我打算编写具有以下功能的 MediaPlayer 应用程序:使用意图从存储中选择一个文件,然后开始播放该文件。我使用“MediaPlayer.create(context, uri)”方法,但目前出现错误
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//openFile();
Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
openFile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
Uri uri = intent.getData();
initViews(uri);
}
});
}
private void initViews(Uri uri) {
mButtonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
mButtonPlayStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonClick();
}
});
mMediaPlayer = MediaPlayer.create(this, uri);
}
private void starPlayProgressUpdater() {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
if (mMediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
@Override
public void run() {
starPlayProgressUpdater();
}
};
mHandler.postDelayed(notification, 1000);
} else {
mMediaPlayer.pause();
mButtonPlayStop.setText(getString(R.string.play_str));
mSeekBar.setProgress(0);
}
}
private void buttonClick() {
if (mButtonPlayStop.getText() == getString(R.string.play_str)) {
mButtonPlayStop.setText(getString(R.string.pause_str));
try {
mMediaPlayer.start();
starPlayProgressUpdater();
} catch (IllegalStateException e) {
mMediaPlayer.pause();
}
} else {
mButtonPlayStop.setText(getString(R.string.play_str));
mMediaPlayer.pause();
}
}
它在“mMediaPlayer = MediaPlayer.create(this, uri);”中抛出 NullPointerException。uri有问题。任何人都可以指导我吗?
谢谢 Ilango j,我修改了我的代码,现在我可以选择和播放音乐文件。但我的期望是选择文件,然后仅在单击播放/暂停按钮(带有进度条)后播放,但我的 buttonClick 得到了新的 NullPointerException
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = (Context) getApplicationContext();
Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
openFile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initView();
}
});
}
private void initView() {
mButtonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
mButtonPlayStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonClick();
}
});
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 10);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_OK && requestCode == 10) {
Uri uri = data.getData();
mMediaPlayer = MediaPlayer.create(mContext, uri);
mSeekBar = (SeekBar) findViewById(R.id.SeekBar01);
mSeekBar.setMax(mMediaPlayer.getDuration());
mSeekBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
}
private void starPlayProgressUpdater() {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
if (mMediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
@Override
public void run() {
starPlayProgressUpdater();
}
};
mHandler.postDelayed(notification, 1000);
} else {
mMediaPlayer.pause();
mButtonPlayStop.setText(getString(R.string.play_str));
mSeekBar.setProgress(0);
}
}
private void seekChange(View v) {
if (mMediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
mMediaPlayer.seekTo(sb.getProgress());
}
}
private void buttonClick() {
if (mButtonPlayStop.getText().equals(getString(R.string.play_str))) {
mButtonPlayStop.setText(getString(R.string.pause_str));
try {
mMediaPlayer.start();
starPlayProgressUpdater();
} catch (IllegalStateException e) {
mMediaPlayer.pause();
}
} else {
mButtonPlayStop.setText(getString(R.string.play_str));
mMediaPlayer.pause();
}
}
你能给个建议吗?