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 createpublic 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,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 grabBufferpublic 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解决方法 修复.原来在将缓冲区写入“file”创建的临时文件后,可以使用fileinputStream打开该文件,然后继续播放,如下所示.感谢你们所有的帮助.
mp = new MediaPlayer();fileinputStream fis = new fileinputStream(convertedfile);mp.setDataSource(fis.getFD());Toast.makeText(this,Toast.LENGTH_SHORT).show();mp.prepare();mp.start();总结
以上是内存溢出为你收集整理的如何在MediaPlayer上播放Android InputStream?全部内容,希望文章能够帮你解决如何在MediaPlayer上播放Android InputStream?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)