MediaPlayer setDataSource,最好使用PATH还是FileDescriptor?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (885)

假设我有一个完整的文件路径。哪种方法更好地将文件加载到MediaPlayer中?

String filePath = "somepath/somefile.mp3";
mediaPlayer.setDataSource(filePath);

String filePath = "somepath/somefile.mp3";
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
提问于
用户回答回答于

在某些情况下是有区别的。

mediaPlayer.setDataSource(String path)mediaPlayer.prepare()如果您尝试从中加载文件,则会在调用时失败getApplicationContext().getFilesDir(),具体取决于文件的保存方式。

用户回答回答于

java具有setDataSource()签名

/**
 * Sets the data source (file-path or http/rtsp URL) to use. Call this after 
 * reset(), or before any other method (including setDataSource()) that might
 * throw IllegalStateException in this class.
 * 
 * @param path the path of the file, or the http/rtsp URL of the stream you want to play
 * @throws IllegalStateException if it is called
 * in an order other than the one specified above
 */
public native void setDataSource(String path) throws IOException, IllegalArgumentException, IllegalStateException;

/**
 * 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.
 * Call this after reset(), or before any other method (including setDataSource()) 
 * that might throw IllegalStateException in this class.
 * 
 * @param fd the FileDescriptor for the file you want to play
 * @throws IllegalStateException if it is called
 * in an order other than the one specified above
 */
public void setDataSource(FileDescriptor fd) 
        throws IOException, IllegalArgumentException, IllegalStateException {
    // intentionally less than LONG_MAX
    setDataSource(fd, 0, 0x7ffffffffffffffL);
}

/**
 * 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.
 * Call this after reset(), or before any other method (including setDataSource()) 
 * that might throw IllegalStateException in this class.
 * 
 * @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
 * @throws IllegalStateException if it is called
 * in an order other than the one specified above
 */
public native void setDataSource(FileDescriptor fd, long offset, long length) 
        throws IOException, IllegalArgumentException, IllegalStateException;

所属标签

可能回答问题的人

  • 学生

    3 粉丝476 提问7 回答
  • uncle_light

    5 粉丝518 提问6 回答
  • 最爱开车啦

    8 粉丝503 提问5 回答
  • 骑牛看晨曦

    4 粉丝522 提问5 回答

扫码关注云+社区

领取腾讯云代金券