我正在学习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应该使用哪个文件来播放。
https://stackoverflow.com/questions/66616920
复制相似问题