Android 加载文件时出现 Soundpool 错误
Android Soundpool Error while loading file
我正在尝试使用 SoundPool
动态改变播放速度来播放音频文件。但是,当我尝试使用
加载文件时
soundPool.load(mediaPath, 0);
其中 mediaPath
是我使用 Intent
获得的文件的路径。我在硬编码文件路径时遇到同样的错误。
我收到以下错误:
02-20 14:57:55.570 17079-17079/net.stupidiot.pacekeeper E/SoundPool﹕ error loading /document/audio:190688
我去过 this and this post。但是 none 解决了我的情况。
请帮忙。
我找到了你的问题的解决方案。绝对是路径的问题。我实现了将 soundpool 与音频意图结合使用的代码。起初我从 onActivityResult 中返回的 URI 获取路径,如下所示:
Uri uri = data.getData();
String path = uri.getPath();
它给了我和你一样的错误。然后我硬编码了我的一个音频文件的路径,就像这样 /storage/extSdCard/Extra/predeftones/predefringtones/Ahoy.aac 并且它起作用了。才知道肯定是路径有问题,于是实现了一个方法,returns文件的真实路径,如下:
private String _getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
我将 uri 传递给此方法,它 returns 是我的真实路径,然后我在 soundpool 的加载方法中使用该路径。完整代码如下:
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b;
private boolean loaded;
private int soundID;
private SoundPool soundPool;
private float volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
});
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
//the selected audio.
Uri uri = data.getData();
String path = _getRealPathFromURI(MainActivity.this, uri);
Log.e("test", "" + path);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(path, 1);
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actualVolume / maxVolume;
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
} catch (Exception e) {
Log.e("Test", "error" + e);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private String _getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = {MediaStore.Audio.Media.DATA};
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
注:根据Android reference guidegetPathreturns解码路径。不知道什么是解码路径,但肯定不是真实路径
我正在尝试使用 SoundPool
动态改变播放速度来播放音频文件。但是,当我尝试使用
soundPool.load(mediaPath, 0);
其中 mediaPath
是我使用 Intent
获得的文件的路径。我在硬编码文件路径时遇到同样的错误。
我收到以下错误:
02-20 14:57:55.570 17079-17079/net.stupidiot.pacekeeper E/SoundPool﹕ error loading /document/audio:190688
我去过 this and this post。但是 none 解决了我的情况。
请帮忙。
我找到了你的问题的解决方案。绝对是路径的问题。我实现了将 soundpool 与音频意图结合使用的代码。起初我从 onActivityResult 中返回的 URI 获取路径,如下所示:
Uri uri = data.getData();
String path = uri.getPath();
它给了我和你一样的错误。然后我硬编码了我的一个音频文件的路径,就像这样 /storage/extSdCard/Extra/predeftones/predefringtones/Ahoy.aac 并且它起作用了。才知道肯定是路径有问题,于是实现了一个方法,returns文件的真实路径,如下:
private String _getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
我将 uri 传递给此方法,它 returns 是我的真实路径,然后我在 soundpool 的加载方法中使用该路径。完整代码如下:
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b;
private boolean loaded;
private int soundID;
private SoundPool soundPool;
private float volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
});
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
//the selected audio.
Uri uri = data.getData();
String path = _getRealPathFromURI(MainActivity.this, uri);
Log.e("test", "" + path);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(path, 1);
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actualVolume / maxVolume;
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
} catch (Exception e) {
Log.e("Test", "error" + e);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private String _getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = {MediaStore.Audio.Media.DATA};
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
注:根据Android reference guidegetPathreturns解码路径。不知道什么是解码路径,但肯定不是真实路径