gpt4 book ai didi

android - 如何在本地/默认媒体播放器上播放 raw/assets 文件夹中的音频文件?

转载 作者:行者123 更新时间:2023-11-29 14:52:17 26 4
gpt4 key购买 nike

我知道我可以像这样在媒体播放器中播放 mp3 文件:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);

正在关注 this link我试着像这样获取 URI:

Uri audio = Uri.parse("android.resource://com.audio.test/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());

Uri audio = Uri.parse("android.resource://com.audio.test/raw/audio");
Log.d(TAG,"uri:"+audio.toString());

输出预期结果:

01-24 15:28:23.190: D/MyClass(30234): uri:android.resource://com.audio.test/2131034112
01-24 15:29:13.: D/MyClass(30234): uri:android.resource://com.audio.test/raw/audio1

但它不起作用。媒体播放器不启动。有什么想法吗?

更新

我包含了一个 createChooser,但我收到了“无法找到执行此操作的应用程序”消息,而不是预期的玩家列表。这是我的确切代码:

 public void playAudio(){
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri audio = Uri.parse("android.resource://com.audio.test/raw/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());
viewMediaIntent.setDataAndType(audio, "video/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d(TAG,"Starting");
Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
mGap.startActivity(i);
Log.d(TAG,"Started");
}

更新 2

感谢@CommonsWare 的解释。现在我明白为什么它不起作用了。但问题依然存在,我能达到我想要的吗?使用 file://方案获取存储在 raw/assets 文件夹中的文件的 Uri?

更新 3

我找到了一种方法,尽管它不是最好的方法。我只有 3 个文件,这根本不会延迟执行。我正在将文件从 res/raw 复制到手机上的本地目录并从该文件获取 Uri。如有任何关于如何避免该步骤的建议,我们将不胜感激。

 public void copyFileToDir(String audioFile){
File test = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
if (test.exists()){
Toast.makeText(mGap, "Exists", Toast.LENGTH_SHORT).show();
return;
}
File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
int i = mGap.getResources().getIdentifier("raw/"+audioFile, "string", mGap.getPackageName());
InputStream in = mGap.getResources().openRawResource(i);
// Used the File-constructor
OutputStream out;
try {
out = new FileOutputStream(new File(dest, audioFile + ".mp3"));
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
try {
// A little more explicit
while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
} finally {
// Ensure the Streams are closed:
in.close();
out.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


public void playAudio(String audioFile){
copyFileToDir(audioFile);
File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
Uri r = Uri.fromFile(dest);
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
viewMediaIntent.setDataAndType(r, "audio/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
mGap.startActivity(i);

}

public void playVideo(String movieurl){
Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
Log.d(TAG,"Playing:" + movieurl);
mGap.startActivity(intentToPlayVideo);
}

最佳答案

为什么不直接使用 MediaPlayer?可以直接传资源id

int resourceId = R.raw.your_media_resource;
MediaPlayer mediaPlayer = MediaPlayer.create( context, resourceId );
mediaPlayer.setOnCompletionListener( new OnCompletionListener()
{
@Override
public void onCompletion( MediaPlayer mp )
{
mp.release();
}
} );
mediaPlayer.start();

关于android - 如何在本地/默认媒体播放器上播放 raw/assets 文件夹中的音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14505153/

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