如何在MediaPlayer上播放Android InputStream?

如何在MediaPlayer上播放Android InputStream?,第1张

概述所以我的资产文件夹里有一个小音频文件,我想打开一个InputStream来写入一个缓冲区,然后写入一个临时文件,然后打开MediaPlayer播放该临时文件.问题是,当媒体播放器命中mp.Prepare()时,它不播放,从不到达吐司.有没有人曾经这样做过? public void onCreate(Bundle savedInstanceState) { super.onCreate(sa 所以我的资产文件夹里有一个小音频文件,我想打开一个inputStream来写入一个缓冲区,然后写入一个临时文件,然后打开MediaPlayer播放该临时文件.问题是,当媒体播放器命中mp.Prepare()时,它不播放,从不到达吐司.有没有人曾经这样做过?
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?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存