【问题标题】:How do you play Android InputStream on MediaPlayer?你如何在 MediaPlayer 上播放 Android InputStream?
【发布时间】:2011-08-10 11:06:54
【问题描述】:

所以我的资产文件夹中有一个小音频文件,我想打开一个 InputStream 来写入缓冲区,然后写入一个临时文件,然后我打开 MediaPlayer 来播放那个临时文件。问题是,当媒体播放器点击 mp.Prepare() 时,它不会播放并且永远不会到达 toast。以前有人做过吗?

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

【问题讨论】:

  • “它停止”是什么意思?
  • 哦,对不起,我的意思是,在准备好之后它“从不播放”。它永远不会到达 Toast。
  • 你有什么异常吗?另外,这不是使用 MediaPlayer 的正确方法,您必须等到 onPrepared 事件到达,但我不确定它是否相关。
  • 我看不到。如果不使用 onPrepared,所有其他示例似乎都很好。我会尝试一下,看看会发生什么。
  • @Rion BTW - 正常的音频文件播放成功了吗?

标签: android file inputstream fileoutputstream


【解决方案1】:

修复它。事实证明,在“File”创建的临时文件中写入缓冲区后,您可以使用 FileInputStream 打开该文件,然后继续播放它,如下所示。感谢大家的帮助。

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

【讨论】:

  • 很好的答案,但我有疑问。什么时候是关闭“fis”的好时机?
  • 您应该在调用 setDataSource 后立即关闭 FIS。文档说“设置要使用的数据源(FileDescriptor)。调用者有责任关闭文件描述符。一旦调用返回,这样做是安全的。”
  • 一个简单的“Uri.fromFile(convertedFile)”有什么问题?!
【解决方案2】:

这是对我有用的代码

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

文件应该在 assets 文件夹中

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2017-05-19
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多