setdatasource reports mediaplayer example ejemplo crystal android

reports - mediaplayer android example



MediaPlayer setDataSource, ¿es mejor usar path o FileDescriptor? (2)

En realidad, resulta que hay una diferencia en ciertas situaciones.

mediaPlayer.setDataSource(String path) fallará cuando llame a mediaPlayer.prepare() , si está intentando cargar un archivo desde getApplicationContext().getFilesDir() , dependiendo de cómo se guardó el archivo. Por ejemplo, si escribo un archivo usando el new RandomAccessFile(filePath, "rw") , el reproductor multimedia no puede leer el archivo si usas mediaPlayer.setDataSource(String path) . El prepare() activará inmediatamente el error(1, -2147483648) del reproductor multimedia; esencialmente un error de permiso de archivo. El SDK 9 introdujo file.setReadable (boolean readable, boolean ownerOnly) que presumiblemente le permitiría resolver este problema configurando ownerOnly en falso ... pero eso no le ayuda si necesita admitir SDK más antiguos.

SIN EMBARGO, mediaPlayer.setDataSource(FileDescriptor fd) NO tiene este problema y mediaplayer preparará con éxito el mismo archivo exacto sin un problema de permiso.

Digamos que tengo una ruta completa a un archivo. ¿Cuál es el mejor enfoque para cargar ese archivo en un MediaPlayer?

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

O

String filePath = "somepath/somefile.mp3"; File file = new File(filePath); FileInputStream inputStream = new FileInputStream(file); mediaPlayer.setDataSource(inputStream.getFD()); inputStream.close();

¿Importa? Simplemente usar la ruta parece más fácil, pero ¿hay alguna razón para hacer el trabajo adicional para usar un FileDescriptor?


MediaPlayer.java tiene firmas setDataSource () que aceptan tanto una Cadena (ruta) como un FD. Ambos eventualmente ingresan al código C nativo. Aunque uno de estos puede ser un poco más eficiente, será despreciable a menos que configure su fuente de datos más de una vez por segundo.

/** * 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;