android media player 视频格式,如何在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,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值