gpt4 book ai didi

android - 选择要用 MediaPlayer 播放的音乐文件

转载 作者:太空狗 更新时间:2023-10-29 16:05:16 26 4
gpt4 key购买 nike

我是 Android 编程的新手,现在我的应用程序出现问题。我打算编写一个具有以下功能的 MediaPlayer 应用程序:使用 intent 从存储中选择一个文件,然后开始播放该文件。我使用“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();
}
}

你能给个建议吗?

最佳答案

首先从 sd 卡启动 pic 媒体文件的 Activity 。替换以下代码

Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
openFile.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 10);
}
});

然后在onActivityResult

中添加如下代码
     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if(resultCode == RESULT_OK && requestCode == 10){
Uri uriSound=data.getData();
play(this, uriSound);
}
}

然后调用下面的方法来创建媒体播放器并播放选定的音频文件。

   private void play(Context context, Uri uri) {

try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(context, uri);
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于android - 选择要用 MediaPlayer 播放的音乐文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17042308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com