[英]Android can't play a wav file in my res/raw folder
I'm building an android app. 我正在构建一个Android应用程序。 And I just want to play a wav file if it exists in my /res/raw folder.
我只想播放一个wav文件(如果它存在于我的/ res / raw文件夹中)。 The following code does not work because the f.exists() always evaluates to false:
由于f.exists()始终为false,因此以下代码不起作用:
String filePathString = "android.resource://"+_context.getPackageName()+"/res/raw/oldphone_mono.wav";
File f = new File(filePathString);
java.io.InputStream inputStream = _context.getResources().openRawResource(com.hello20.R.raw.oldphone_mono);
if(f.exists()) {
Uri ringUri = Uri.parse(filePathString);
_ring = new MediaPlayer();
_ring.setDataSource(_context, ringUri);
final AudioManager audioManager = (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
_ring.setAudioStreamType(AudioManager.STREAM_RING);
_ring.setLooping(true);
_ring.prepare();
_ring.start();
}
What else should I do to make this work? 我还应该做些什么才能使这项工作?
Try to load wav by other way: 尝试通过其他方式加载wav:
_ring=MediaPlayer.create(_context,R.raw.oldphone_mono);
_ring.start();
_ring.setLooping( true );
Comment 1 评论1
You don't need in my case to use _ring.prepare();
在我的情况下,您不需要使用
_ring.prepare();
. 。
MediaPlayer.create
takes care about MediaPlayer.create
关心
Comment 2 评论2
Further, since you load wav resource from raw, you don't need if(f.exists())
statemant 此外,由于您是从raw加载WAV资源,因此不需要
if(f.exists())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.