Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在MediaPlayer上播放Android InputStream?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我的资产文件夹里有一个小音频文件,我想打开一个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 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,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(Au@L_427_8@manager.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
@H_404_4@

解决方法

修复.原来在将缓冲区写入“File”创建的临时文件后,可以使用FileInputStream打开该文件,然后继续播放,如下所示.感谢你们所有的帮助. @H_483_2@mp = new MediaPlayer(); FileInputStream fis = new FileInputStream(convertedFilE); mp.setDatasource(fis.getFD()); Toast.makeText(this,Toast.LENGTH_SHORT).show(); mp.prepare(); mp.start(); @H_404_4@ @H_404_4@
本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的如何在MediaPlayer上播放Android InputStream?全部内容,希望文章能够帮你解决如何在MediaPlayer上播放Android InputStream?所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。