我想播放用户以前选择的音乐文件。选择文件似乎是可行的(我得到了一个看起来合理的路径),获得权限也是可行的,但是媒体播放器抛出filenotfound。因为我提前10秒选择了这个文件,所以它必须存在。
错误出现在“播放音乐”功能中
我的代码是:
public class Settings extends Fragment {
View mView;
final int MY_STORAGE_PERMISSION_CODE = 101;
public Settings() {}
public static Settings newInstance(String param1, String param2) {
Settings fragment = new Settings();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
mView = view;
String musicFileFromDatabase = DatabaseInteractor.getDatabaseInteractor(view.getContext()).
getSetting(DatabaseInteractor.SETTING_MUSIC_FILE); //gets saved path to music file from database
view.findViewById(R.id.musicFileLabel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), 1);
}
});
view.findViewById(R.id.button_play_music).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(DatabaseInteractor.getDatabaseInteractor(getContext()).getSetting(DatabaseInteractor.SETTING_MUSIC_FILE).equals("")) {
Toast.makeText(getContext(), R.string.select_a_file_first, Toast.LENGTH_LONG).show();
return;
}
if(ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE },MY_STORAGE_PERMISSION_CODE);
} else {
playMusic();
}
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
if ((data != null) && (data.getData() != null)) {
Uri audioFileUri = data.getData();
String path = audioFileUri.getPath();
//put the path as String into database
DatabaseInteractor.getDatabaseInteractor(this.getContext()).
updateSetting(DatabaseInteractor.SETTING_MUSIC_FILE, path);
Navigation.findNavController(mView).navigate(SettingsDirections.actionSettingsSelf());
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if (requestCode == MY_STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
playMusic();
}
else {
Toast.makeText(getContext(),
R.string.app_needs_permission,
Toast.LENGTH_LONG)
.show();
}
}
}
private void playMusic() {
String filePath = "file:///" + DatabaseInteractor.getDatabaseInteractor(getContext()).
getSetting(DatabaseInteractor.SETTING_MUSIC_FILE);
Uri uri= Uri.parse(filePath);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getActivity().getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的目标是:
W/System.err: java.io.FileNotFoundException: /external/audio/media/167714: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:159)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1264)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1208)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1173)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1103)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1049)
at de.annikaknepper.archery.Settings.playMusic(Settings.java:130)
at de.annikaknepper.archery.Settings.access$000(Settings.java:34)
at de.annikaknepper.archery.Settings$2.onClick(Settings.java:85)
at android.view.View.performClick(View.java:7192)
at android.view.View.performClickInternal(View.java:7166)
W/System.err: at android.view.View.access$3500(View.java:824)
at android.view.View$PerformClick.run(View.java:27592)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err: at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8046)
at libcore.io.IoBridge.open(IoBridge.java:482)
W/System.err: ... 20 more
暂无答案!
目前还没有任何答案,快来回答吧!