gpt4 book ai didi

android - 从文件夹获取音频

转载 作者:行者123 更新时间:2023-12-03 01:57:15 25 4
gpt4 key购买 nike

我有一个警报,想让用户选择音乐。
我有一些代码,其中包含以下片段:

Button ring_button = (Button) findViewById(R.id.button2);
ring_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent_for_ring_button = new Intent();
intent_for_ring_button.setAction(Intent.ACTION_GET_CONTENT);
File file = new File(REPORTS_DIRECTORY);
intent_for_ring_button.setDataAndType(Uri.fromFile(file),"audio/*");
startActivityForResult(Intent.createChooser(intent_for_ring_button,"Open folder"), 0);
}
});

还有一个带有警报的 Activity ,该警报扩展了 BroadcastReceiver ,在这里,我将通过带有一些音乐的对话窗口开始下一个 Activity 。
就这个:
public class Dialog_window extends ActionBarActivity{

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_window_bckg);
getSupportActionBar().hide();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

final MediaPlayer Alarm_mp3 = MediaPlayer.create(Dialog_window.this, R.raw.data_don_t_sing);
Alarm_mp3.start();
Alarm_mp3.setLooping(true);
AlertDialog.Builder builder = new AlertDialog.Builder(Dialog_window.this);
builder.setTitle("Важное сообщение!")
.setMessage("Вставай!")
.setIcon(R.drawable.uncle_sam)
.setCancelable(false)
.setNegativeButton("ОК, встаю, встаю.",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Alarm_mp3.stop();
System.exit(0);
}
});
AlertDialog alert = builder.create();
alert.show();
}

}

我怎么能意识到这一点?我必须从第一次 Activity 中获得一些额外的东西,否则还有其他吗?

最佳答案

在使用startActivityForResult意图调用ACTION_GET_CONTENT之后,应该覆盖同一类中的onActivityResult,并获取所选文件的路径。有了路径后,您可以保存到共享的首选项,并轻松地从另一个 Activity 中检索(例如,在Dialog_window中)并使​​用MediaPlayer进行播放。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){ // you've set to 0 at startActivityForResult
if(resultCode==RESULT_OK){
String filePath = data.getData().getPath();
// save filePath to shared preferences
}
}
}

在另一个 Activity 中检索完之后,只需简单地播放它即可。
String savedPath = ..... ; // retrieve from shared preferences  

//check your saved path, it could be a full path already..
//if it is, you don't need to concatenate anything to it
String fullPath = Environment.getExternalStorageDirectory()+savedPath;

mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fullPath);
mediaPlayer.prepare();
mediaPlayer.start()

保存共享首选项的文件路径:
SharedPreferences.Editor editor = getSharedPreferences("mypref", MODE_PRIVATE).edit();
editor.putString("audioFilePath", filePath);
editor.commit();

从共享的首选项恢复文件路径:
SharedPreferences prefs = getSharedPreferences("mypref", MODE_PRIVATE); 
String restoredFilePath = prefs.getString("audioFilePath", null);
if (restoredFilePath != null) {
// here you have the restored file path
}

关于android - 从文件夹获取音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34371835/

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