在MediaPlayer的你怎么玩Android的InputStream的? [英] How do you play Android InputStream on MediaPlayer?

查看:618
本文介绍了在MediaPlayer的你怎么玩Android的InputStream的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我的资产文件夹中的小型音频文件,我想开一个的InputStream写入到缓存,然后写入到一个临时文件,然后我打开的MediaPlayer播放该临时文件。问题是,当媒体播放器击中熔点。prepare(),它不玩,从来没有达到敬酒。有没有人这样做过?

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    InputStream的海峡;

    尝试 {

        海峡= this.getAssets()开(onestop.mid)。
        Toast.makeText(这一点,成功的输入流开业。,Toast.LENGTH_SHORT).show();
        takeInputStream(STR);
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

} //上创建结束

公共无效takeInputStream(InputStream的流)抛出IOException异常
{
    // fileBeingBuffered =(的FileInputStream)流;
    //Toast.makeText(this,SUCESSFUL流转换。,Toast.LENGTH_SHORT).show();
    尝试
    {
        convertedFile = File.createTempFile(convertedFile,的.dat,GETDIR(filez,0));
        Toast.makeText(这一点,成功的文件和文件夹的创建。Toast.LENGTH_SHORT).show();

        OUT =新的FileOutputStream(convertedFile);
        Toast.makeText(这一点,成功了设置为输出流。Toast.LENGTH_SHORT).show();

        // RIGHT在这里-----------

        字节的缓冲区[] =新的字节[16384]
        INT长度= 0;
        而((长度= stream.read(缓冲液))!=  -  1)
        {
          out.write(缓冲液,0,长度);
        }

        //stream.read(buffer);
        Toast.makeText(这一点,成功缓冲区已满,Toast.LENGTH_SHORT).show();
        out.close();

        playFile();
    }赶上(例外五)
    {
        Log.e(TAG,e.toString());
        e.printStackTrace();
    } //结束抓
} //结束grabBuffer

公共无效playFile()
{
    尝试 {
        字符串路径= convertedFile.getAbsolutePath();
        熔点为新的MediaPlayer();
        mp.setDataSource(路径);
        Toast.makeText(这一点,成功,路径已定,Toast.LENGTH_SHORT).show();

        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        MP prepare()。
        Toast.makeText(这一点,媒体播放器prepared,Toast.LENGTH_SHORT).show();

        mp.start();
        Toast.makeText(这一点,Media Player中播放,Toast.LENGTH_SHORT).show();
    }赶上(抛出:IllegalArgumentException E){
        Log.e(TAG,e.toString());
        e.printStackTrace();
    }赶上(IllegalStateException异常E){
        Log.e(TAG,e.toString());
        e.printStackTrace();
    }赶上(IOException异常E){
        Log.e(TAG,e.toString());
        e.printStackTrace();
    }

} //结束playFile
 

解决方案

固定它。原来,写由创建的临时文件缓冲区后文件,你就可以打开使用一个FileInputStream该文件,然后继续玩下图所示它。感谢您的帮助球员。

  MP =新的MediaPlayer();

的FileInputStream FIS =新的FileInputStream(convertedFile);
mp.setDataSource(fis.getFD());

Toast.makeText(这一点,成功,路径已定,Toast.LENGTH_SHORT).show();

MP prepare()。
mp.start();
 

So I have a small audio file in my assets folder and I wanted to open a InputStream to write to a buffer, then write to a temporary File, then I open up the MediaPlayer to play that temporary File. Problem is, when the media player hits mp.Prepare(), it doesn't play and never reaches the toast. Has anyone ever done this before?

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

解决方案

Fixed it. Turns out that after writing the buffer in the temporary file created by "File," you can then open that file using a FileInputStream, then proceed to play it shown below. Thanks for all your help guys.

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();

这篇关于在MediaPlayer的你怎么玩Android的InputStream的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆