¿Cómo juega Android InputStream en MediaPlayer?

Así que tengo un pequeño archivo de audio en mi carpeta de activos y quería abrir un InputStream para escribir en un búfer, luego escribir en un archivo temporal, a continuación, abrir el MediaPlayer para reproducir ese archivo temporal. El problema es que cuando el reproductor de medios golpea mp.Prepare (), no se reproduce y nunca llega al brindis. ¿Alguien había echo esto antes?

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); InputStream str; try { str = this.getAssets().open("onestop.mid"); Toast.makeText(this, "Successful Input Stream Opened.", Toast.LENGTH_SHORT).show(); takeInputStream(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//end on create public void takeInputStream(InputStream stream) throws IOException { //fileBeingBuffered = (FileInputStream) stream; //Toast.makeText(this, "sucessful stream conversion.", Toast.LENGTH_SHORT).show(); try { convertedFile = File.createTempFile("convertedFile", ".dat", getDir("filez", 0)); Toast.makeText(this, "Successful file and folder creation.", Toast.LENGTH_SHORT).show(); out = new FileOutputStream(convertedFile); Toast.makeText(this, "Success out set as output stream.", Toast.LENGTH_SHORT).show(); //RIGHT AROUND HERE ----------- byte buffer[] = new byte[16384]; int length = 0; while ( (length = stream.read(buffer)) != -1 ) { out.write(buffer,0, length); } //stream.read(buffer); Toast.makeText(this, "Success buffer is filled.", Toast.LENGTH_SHORT).show(); out.close(); playFile(); }catch(Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); }//end catch }//end grabBuffer public void playFile() { try { String path = convertedFile.getAbsolutePath(); mp = new MediaPlayer(); mp.setDataSource(path); Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.prepare(); Toast.makeText(this, "Media Player prepared", Toast.LENGTH_SHORT).show(); mp.start(); Toast.makeText(this, "Media Player playing", Toast.LENGTH_SHORT).show(); } catch (IllegalArgumentException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } catch (IllegalStateException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } catch (IOException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } }//end playFile 

Arreglado. Resulta que después de escribir el búfer en el archivo temporal creado por "Archivo", puede abrir ese archivo usando FileInputStream, luego proceda a reproducirlo como se muestra a continuación. Gracias por toda su ayuda chicos.

 mp = new MediaPlayer(); FileInputStream fis = new FileInputStream(convertedFile); mp.setDataSource(fis.getFD()); Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show(); mp.prepare(); mp.start(); 

Este es el código que funcionó para mí

 //preserved to stop previous actions MediaPlayer lastmp; public void playSound(String file){ try{ if(lastmp!=null)lastmp.stop(); MediaPlayer mp = new MediaPlayer(); lastmp=mp; AssetFileDescriptor descriptor; AssetManager assetManager = act.getAssets(); descriptor = assetManager.openFd(fileName); mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() ); descriptor.close(); mp.prepare(); mp.start(); }catch(Exception e){ e.printStackTrace();} } 

El archivo debe estar en la carpeta de activos

¿Ha intentado cambiar la extensión de su archivo temporal de ".dat" a ".mid"?

  • BitmapFactory.decodeStream (inputStream) devuelve siempre null cuando algunos bytes son incorrectos
  • cómo leer el flujo de entrada de socket en Android
  • Android InputStream que deja caer dos primeros bytes (BluetoothChat modificado)
  • No se puede entender mHandler.obtainMessage () en la muestra Bluetooth de Android
  • Implementación cliente / servidor de socket TCP de Android
  • cómo reproducir un video usando el videoView y un inputStream
  • Android cómo obtener acceso a los recursos crudos que poner en la carpeta res?
  • No se puede leer socket InputStream en Jelly Bean
  • Obtener longitud desconocidaHttpInputStream al obtener InputStream de HttpURLConnection en android
  • Copiar archivo a carpeta de activos
  • Convertir InputStream a BufferedReader
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.