刚拿到我的 Nexus 7,当我播放音频文件时出现此错误,然后强制关闭。我整天都在寻找解决方案。以下是相关代码:
protected void onListItemClick(ListView l, View v, int position, long id) {
playSong(MEDIA_PATH + songs.get(position));
textViewRecordPlaying.setText(songs.get(position));
}
private void playSong(String songPath) {
final Intent i = new Intent(MusicService.ACTION_URL);
File file = new File(songPath);
Uri uri = Uri.fromFile(file);
i.setData(uri);
startService(i);
}
playSong(MEDIA_PATH + songs.get(position));
我的MEDIA_PATH
:
private static final String MEDIA_PATH = new String(Environment
.getExternalStorageDirectory().getPath() + "/AudioStreamRecorder/");
此代码在我的 Galaxy S3 和 Thunderbolt 上运行良好,但是当我单击列表项时,我Failed to open file. Permission denied.
在 Nexus 7 和 Asus Transformer Prime TF201 上出现错误,然后强制关闭。
我已经尝试过Environment.getExternalStorageDirectory();
同样Environment.getExternalStorageDirectory()getAbsolutePath();
的错误。我已经宣布<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
,
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
有人请帮助我已经没有想法了......
05-07 16:47:35.714: I/Streaming Radio Recorder Pro(4427): debug: Creating service
05-07 16:47:35.724: I/MusicRetriever(4427): Querying media...
05-07 16:47:35.724: I/MusicRetriever(4427): URI: content://media/external/audio/media
05-07 16:47:35.724: I/Streaming Radio Recorder Pro(4427): Playing from URL/path: file:///storage/emulated/0/AudioStreamRecorder/The%20Dining%20Rooms%20-%20triste%2C%20solitario%20y%20final-3.54.44%20Tuesday%2C%20May%207%2C%202013.mp3
05-07 16:47:35.724: I/AudioService(436): AudioFocus requestAudioFocus() from android.media.AudioManager@42ffe928com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@42ffe2b8
05-07 16:47:35.724: I/AwesomePlayer(128): setDataSource_l(URL suppressed)
05-07 16:47:35.724: I/MusicRetriever(4427): Query finished. Returned a cursor.
05-07 16:47:35.724: I/MusicRetriever(4427): Listing...
05-07 16:47:35.724: I/MusicRetriever(4427): Title column index: 8
05-07 16:47:35.724: I/MusicRetriever(4427): ID column index: 8
05-07 16:47:35.724: I/MusicRetriever(4427): ID: 21 Title: Join Hangout
05-07 16:47:35.724: I/MusicRetriever(4427): ID: 2305 Title: The Dining Rooms - triste, solitario y final-3.54.44 Tuesday, May 7, 2013
05-07 16:47:35.724: I/MusicRetriever(4427): Done querying media. MusicRetriever is ready.
05-07 16:47:35.734: E/(128): Failed to open file '/storage/emulated/0/AudioStreamRecorder/The Dining Rooms - triste, solitario y final-3.54.44 Tuesday, May 7, 2013.mp3'. (Permission denied)
05-07 16:47:35.734: E/MediaPlayer(4427): error (1, -2147483648)
05-07 16:47:35.734: E/MediaPlayer(4427): Error (1,-2147483648)
05-07 16:47:35.734: E/Streaming Radio Recorder Pro(4427): Error: what=1, extra=-2147483648
05-07 16:47:35.764: I/AudioService(436): AudioFocus abandonAudioFocus() from android.media.AudioManager@42ffe928com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@42ffe2b8
05-07 16:47:36.704: D/AndroidRuntime(4427): Shutting down VM
05-07 16:47:36.704: W/dalvikvm(4427): threadid=1: thread exiting with uncaught exception (group=0x41f66930)
05-07 16:47:36.714: E/AndroidRuntime(4427): FATAL EXCEPTION: main
05-07 16:47:36.714: E/AndroidRuntime(4427): java.lang.NullPointerException
05-07 16:47:36.714: E/AndroidRuntime(4427): at com.LudwigAppDesign.streamingradioplayerpro.Songs$8.run(Songs.java:407)
05-07 16:47:36.714: E/AndroidRuntime(4427): at android.os.Handler.handleCallback(Handler.java:725)
05-07 16:47:36.714: E/AndroidRuntime(4427): at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 16:47:36.714: E/AndroidRuntime(4427): at android.os.Looper.loop(Looper.java:137)
05-07 16:47:36.714: E/AndroidRuntime(4427): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 16:47:36.714: E/AndroidRuntime(4427): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 16:47:36.714: E/AndroidRuntime(4427): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 16:47:36.714: E/AndroidRuntime(4427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 16:47:36.714: E/AndroidRuntime(4427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 16:47:36.714: E/AndroidRuntime(4427): at dalvik.system.NativeStart.main(Native Method)
05-07 16:47:36.724: W/ActivityManager(436): Force finishing activity com.LudwigAppDesign.streamingradioplayerpro/.Songs
05-07 16:47:36.784: D/dalvikvm(436): GC_FOR_ALLOC freed 3517K, 29% free 17724K/24840K, paused 57ms, total 57ms
05-07 16:47:37.294: W/ActivityManager(436): Activity pause timeout for ActivityRecord{42c1cd18 u0 com.LudwigAppDesign.streamingradioplayerpro/.Songs}
05-07 16:47:46.794: W/ActivityManager(436): Launch timeout has expired, giving up wake lock!
05-07 16:47:47.304: W/ActivityManager(436): Activity idle timeout for ActivityRecord{42b7ced0 u0 com.LudwigAppDesign.streamingradioplayerpro/.Main}
05-07 16:48:05.824: W/ThrottleService(436): unable to find stats for iface rmnet0
我把它排除在playSong
方法之外......这会导致 NullPoingterException 吗?
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MusicService.mPlayer
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
}
}, 1000);
}
private void nextSong() {
if (++currentPosition >= songs.size()) {
currentPosition = 0;
} else {
// Play next song
playSong(MEDIA_PATH + songs.get(currentPosition));
textViewRecordPlaying.setText(songs.get(currentPosition));
SharedPreferences settings = getSharedPreferences(
RECORDINGS_NOW_PLAYING, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("nowPlayingTitle", (songs.get(currentPosition).toString().trim()));
// Commit the edits!
editor.commit();
}
}
编辑---您好我删除了这段代码并且 NullPointerException 消失了:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MusicService.mPlayer
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
}
}, 1000);
现在我只剩下 Permission Denied 的原始错误:
05-08 12:23:26.838: I/Streaming Radio Recorder Pro(2661): Playing from URL/path: file:///storage/emulated/0/AudioStreamRecorder/Goudron%20-%20Art%20School%20Fuckheads-5.55.51%20Tuesday%2C%20May%207%2C%202013.mp3
05-08 12:23:26.838: I/AudioService(482): AudioFocus requestAudioFocus() from android.media.AudioManager@41cfdfd0com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@41cfdaf8
05-08 12:23:26.848: I/AwesomePlayer(128): setDataSource_l(URL suppressed)
05-08 12:23:26.848: I/MusicRetriever(2661): Querying media...
05-08 12:23:26.858: I/MusicRetriever(2661): URI: content://media/external/audio/media
05-08 12:23:26.858: E/(128): Failed to open file '/storage/emulated/0/AudioStreamRecorder/Goudron - Art School Fuckheads-5.55.51 Tuesday, May 7, 2013.mp3'. (Permission denied)
05-08 12:23:26.858: E/MediaPlayer(2661): error (1, -2147483648)
05-08 12:23:26.858: E/MediaPlayer(2661): Error (1,-2147483648)
05-08 12:23:26.898: D/dalvikvm(584): GC_CONCURRENT freed 518K, 7% free 9075K/9736K, paused 3ms+3ms, total 32ms
05-08 12:23:26.898: E/Streaming Radio Recorder Pro(2661): Error: what=1, extra=-2147483648
05-08 12:23:26.928: I/AudioService(482): AudioFocus abandonAudioFocus() from android.media.AudioManager@41cfdfd0com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@41cfdaf8
05-08 12:23:26.928: I/MusicRetriever(2661): Query finished. Returned a cursor.
05-08 12:23:26.928: I/MusicRetriever(2661): Listing...
05-08 12:23:26.928: I/MusicRetriever(2661): Title column index: 8
05-08 12:23:26.928: I/MusicRetriever(2661): ID column index: 8
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 21 Title: Join Hangout
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 1553 Title: Goudron - Art School Fuckheads-5.55.51 Tuesday, May 7, 2013
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 1694 Title: Lusine Icl - Mod-10.47.59 Tuesday, May 7, 2013
05-08 12:23:26.928: I/MusicRetriever(2661): Done querying media. MusicRetriever is ready.
我希望这会有所帮助...如果您需要更多信息,请告诉我,并感谢您的尝试。