我正在学习android中的mediaplayer。
我想要一些简单易懂的MediaPlayer.setDataSource()代码示例。
发布于 2021-03-14 02:49:47
要想更深入地理解MediaPlayer,最好阅读官方文档https://developer.android.com/reference/android/media/MediaPlayer#setDataSource(android.content.res.AssetFileDescriptor)。但为了基本理解,这里是代码示例。
MediaPlayer mp = new MediaPlayer();
// Here you may set which stream to use either MEDIA or ALARM etc.
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
if (isAnyActiveSongExist){
// Here you may set dataSource as path of the file
mp.setDataSource(firstPrioritySongEntityPath);
}
else{
// Here you may set dataSource using Uri
mp.setDataSource(context, Settings.System.DEFAULT_RINGTONE_URI);
}
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();setDataSource()定义MediaPlayer应该使用哪个文件来播放。
发布于 2021-03-14 02:50:36
首先,代码从来都不是简单和容易的,除非你不去经历它。检查这个链接click here,我想你会从这里得到答案
关于setDataSource(字符串)调用
在看到您的评论之后,看起来您确实希望使用setDataSource(string)来实现您的目的。我不明白为什么。但是,我假设的是,出于某种原因,您试图避免使用“上下文”。如果不是这样,那么上面的两个解决方案应该可以完美地为您工作,或者如果您试图避免上下文,我担心这是不可能的函数与签名setDataSource(字符串)调用。原因如下:
MediaPlayer setDataSource()函数具有以下选项,其中您只对setDataSource(String)感兴趣。

public void setDataSource(String path)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
setDataSource(path, null, null);
}如果你检查setDataSource(字符串路径,JNI键,JNI值)代码,你会看到下面的条件根据它的方案过滤路径,特别是如果它是称为setDataSource(FileDescriptor)的“文件”方案,或者如果方案不是“文件”,它会调用原生String[]媒体函数。
{
final Uri uri = Uri.parse(path);
final String scheme = uri.getScheme();
if ("file".equals(scheme)) {
path = uri.getPath();
} else if (scheme != null) {
// handle non-file sources
nativeSetDataSource(
MediaHTTPService.createHttpServiceBinderIfNecessary(path),
path,
keys,
values);
return;
}
final File file = new File(path);
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
FileDescriptor fd = is.getFD();
setDataSource(fd);
is.close();
} else {
throw new IOException("setDataSource failed.");
}
}在上面的代码中,您的资源文件URI方案将不为空(android.resource://),并且setDataSource(字符串)将尝试使用原生JNI函数nativeSetDataSource(),认为您的路径是http/https/rtsp,显然,该调用也将失败,不会抛出任何异常。这就是为什么您对setDataSource(String)的调用可以在没有异常的情况下转义,并通过以下异常来准备()调用。
Prepare failed.: status=0x1因此setDataSource(字符串)覆盖不能处理您资源文件。为此,您需要选择另一个覆盖。
另一方面,检查setDataSource(Context context,Uri uri)使用的setDataSource(Context context,URI uri,Map headers),它使用AssetFileDescriptor、来自您的上下文的ContentResolver和openAssetFileDescriptor打开Uri,成功的原因是openAssetFileDescriptor()可以打开您的资源文件,最后生成的fd用于调用setDataSource(FileDescriptor)覆盖。
AssetFileDescriptor fd = null;
try {
ContentResolver resolver = context.getContentResolver();
fd = resolver.openAssetFileDescriptor(uri, "r");
// :
// :
// :
if (fd.getDeclaredLength() < 0) {
setDataSource(fd.getFileDescriptor());
} else {
setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
}总之,您不能为了使用资源mp3文件而使用setDataSource(字符串)覆盖。相反,如果您希望使用字符串播放资源文件,则可以使用上面给出MediaPlayer.create()静态函数和getIdentifier(),或者使用Update#1中给出的setDataSource(context,uri)。
要了解更多信息,请参阅完整的源代码:Android MediaPlayer
https://stackoverflow.com/questions/66616920
复制相似问题