SoundPool API 加载字符串路径时不播放
SoundPool API not playing when loaded with string path
我正在尝试 SoundPool API,我制作了一个小应用程序来学习和了解 SoundPool 的动态性。但我不知道为什么当我加载一个字符串路径(字符串格式的 URI)时,它简单地不播放!
在调试模式下,我意识到当我使用我的 Intent 选择声音时,我将声音路径加载到 SoundPool,它始终给出 0 的 SoundID。不确定这是否是一个问题..
如有任何帮助,我们将不胜感激!
代码
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;
}
}
}
path = data.getData().toString();
首先,这将return Uri
的字符串表示形式。这将包含一个方案,例如 content:
。据我所知,SoundPool
load()
采用文件系统路径。
其次,您取回的 Uri
可能 不是 文件,至少在您可以访问的路径上。
考虑到 load()
的可用版本,我建议您尝试使用 AssetFileDescriptor
作为输入的版本,使用 openAssetFileDescriptor()
on a ContentResolver
获得 AssetFileDescriptor
,看看你的运气好不好
我正在尝试 SoundPool API,我制作了一个小应用程序来学习和了解 SoundPool 的动态性。但我不知道为什么当我加载一个字符串路径(字符串格式的 URI)时,它简单地不播放!
在调试模式下,我意识到当我使用我的 Intent 选择声音时,我将声音路径加载到 SoundPool,它始终给出 0 的 SoundID。不确定这是否是一个问题..
如有任何帮助,我们将不胜感激!
代码
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;
}
}
}
path = data.getData().toString();
首先,这将return Uri
的字符串表示形式。这将包含一个方案,例如 content:
。据我所知,SoundPool
load()
采用文件系统路径。
其次,您取回的 Uri
可能 不是 文件,至少在您可以访问的路径上。
考虑到 load()
的可用版本,我建议您尝试使用 AssetFileDescriptor
作为输入的版本,使用 openAssetFileDescriptor()
on a ContentResolver
获得 AssetFileDescriptor
,看看你的运气好不好