我在SoundPool API上沾沾自喜,我做了一个小应用程序来学习和看看SoundPool的动态。但我不知道为什么当我加载一个字符串路径(字符串格式的URI)它简单不播放!
在调试模式下,我意识到当我使用我的Intent和他们选择声音时,我将声音路径加载到SoundPool,它一直给出SoundID为0.不确定是否有问题。
任何帮助将不胜感激!
代码
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mKey1;
private Button mKey2;
private Button mChange;
private Button mUpload;
int mSound1;
int mSound2;
String path;
private SoundPool mSoundPool;
private boolean mSelectKey = false;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mKey1 = (Button) findViewById(R.id.key1);
mKey2 = (Button) findViewById(R.id.key2);
mChange = (Button) findViewById(R.id.Change);
mUpload = (Button) findViewById(R.id.Upload);
mKey1.setOnClickListener(this);
mKey2.setOnClickListener(this);
mChange.setOnClickListener(this);
mUpload.setOnClickListener(this);
if ( Build.VERSION.SDK_INT >= 21 ) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder().setMaxStreams(2).setAudioAttributes(audioAttributes).build();
}else {
mSoundPool = new SoundPool(50, AudioManager.STREAM_MUSIC,0);
}
mSound1 = mSoundPool.load(this, R.raw.sound1, 1);
mSound2 = mSoundPool.load(this, R.raw.sound2, 1);
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Toast.makeText(MainActivity.this,"ready!",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
path = data.getData().toString();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.key1:
if (mSelectKey){
mSound1 = mSoundPool.load(path,1);
mSelectKey = false;
break;
}
mSoundPool.play(mSound1,1,1,1,0,1);
break;
case R.id.key2:
if (mSelectKey){
mSound2 = mSoundPool.load(path,1);
mSelectKey = false;
break;
}
mSoundPool.play(mSound2,1,1,1,0,1);
break;
case R.id.Change:
mSound1 = mSoundPool.load(this,R.raw.sound6,1);
mSound2= mSoundPool.load(this,R.raw.sound3,1);
break;
case R.id.Upload:
mSelectKey = true;
Intent uploadFile = new Intent(Intent.ACTION_GET_CONTENT);
uploadFile.setType("audio/mp3");
startActivityForResult(uploadFile,1);
break;
}
}
}
答案 0 :(得分:0)
path = data.getData().toString();
首先,这将返回Uri
的字符串表示形式。这将包含一个方案,例如content:
。 AFAIK,SoundPool
load()
采用文件系统路径。
其次,您获得的Uri
可能 一个文件,至少在您可以访问的路径上。
考虑到load()
的可用风格,我建议您尝试使用AssetFileDescriptor
作为输入的风格,使用openAssetFileDescriptor()
on a ContentResolver
获取AssetFileDescriptor
,并查看如果你运气好的话。