MediaPlayer.java文件路径:frameworks/base/media/java/android/media/MediaPlayer.java

MediaPlayer的setDataSource()方法主要有四种:

Sets the data source as a content Uri.
@param context the Context to use when resolving the Uri
@param uri the Content URI of the data you want to play
public void setDataSource(Context context, Uri uri)

Sets the data source (file-path or http/rtsp URL) to use.
@param path the path of the file, or the http/rtsp URL of the stream you want to play
public void setDataSource(String path)

Sets the data source (FileDescriptor) to use. It is the caller’s responsibility
to close the file descriptor. It is safe to do so as soon as this call returns.
@param fd the FileDescriptor for the file you want to play
public void setDataSource(FileDescriptor fd)

Sets the data source (FileDescriptor) to use. The FileDescriptor must be
seekable (N.B. a LocalSocket is not seekable). It is the caller’s responsibility
to close the file descriptor. It is safe to do so as soon as this call returns.
@param fd the FileDescriptor for the file you want to play
@param offset the offset into the file where the data to be played starts, in bytes
@param length the length in bytes of the data to be played
public void setDataSource(FileDescriptor fd, long offset, long length)

1. 播放应用的资源文件

1. 直接调用create函数实例化一个MediaPlayer对象,播放位于res/raw/test.mp3文件MediaPlayer  mMediaPlayer = MediaPlayer.create(this, R.raw.test);2. test.mp3放在res/raw/目录下,使用setDataSource(Context context, Uri uri)mp = new MediaPlayer(); Uri setDataSourceuri = Uri.parse("android.resource://com.android.sim/"+R.raw.test);mp.setDataSource(this, uri);说明:此种方法是通过res转换成uri然后调用setDataSource()方法,需要注意格式Uri.parse("android.resource://[应用程序包名Application package name]/"+R.raw.播放文件名);例子中的包名为com.android.sim,播放文件名为:test;特别注意包名后的"/"。3. test.mp3文件放在assets目录下,使用setDataSource(FileDescriptor fd, long offset, long length)AssetManager assetMg = this.getApplicationContext().getAssets();AssetFileDescriptor fileDescriptor = assetMg.openFd("test.mp3");  mp.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); 

2. 播放存储设备的资源文件

MediaPlayer mediaPlayer = new MediaPlayer();  mediaPlayer.setDataSource("/mnt/sdcard/test.mp3");

3. 播放远程的资源文件

Uri uri = Uri.parse("http://**");  MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(Context, uri);  

更多相关文章

  1. android 监听方法
  2. android sdk 自带的非空非空串判断方法
  3. android中的handler的使用方法
  4. No 98 · Android 下载文件及写入SD卡(摘)
  5. 【android开发】android操作文件
  6. Android下调整多媒体音量方法
  7. 今天发现的一些优秀的资源网站
  8. Android Studio出现Failed to open zip file问题的解决方法
  9. android 播放Raw文件夹下的音乐文件

随机推荐

  1. RocketMQ 初探
  2. 看透 Spring MVC 源代码分析与实践 ——
  3. SpringBoot RabbitMQ 整合进阶版
  4. 看透 Spring MVC 源代码分析与实践 ——
  5. SpringBoot ActiveMQ 整合使用
  6. 死磕这50道MySQL面试题,阿里面试官直呼内
  7. 2021-04-02:给定一个正方形或者长方形矩阵
  8. 博士生研究工具和软件掌握,名单向国际一流
  9. 为什么需要对数据转换,和转换的方法
  10. 考考你的经济学水平,不服来战,学霸们