我尝试使用ListView
类在mediaPlayer
中播放文件。
像这样:
public void initializeViews() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fileName");
fileName = (TextView) findViewById(R.id.fileName);
duration = (TextView) findViewById(R.id.songDuration);
seekbar = (SeekBar) findViewById(R.id.seekBar);
playBtn = (ImageButton) findViewById(R.id.media_play);
try {
mediaPlayer = new android.media.MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(fName);
mediaPlayer.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
fileName.setText(fName.substring(fName.lastIndexOf("/") + 1));
finalTime = mediaPlayer.getDuration();
seekbar.setOnSeekBarChangeListener(mOnSeek);
seekbar.setMax((int) finalTime);
seekbar.setClickable(true);
mediaPlayer.start();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 200);
}
我在互联网上搜索,最大的问题是检查文件的路径,但fName
获得了文件的完整路径。但我不知道它显示异常:
java.io.IOException: Prepare failed.: status=0x1
应用程序发生异常时的所有日志:
E/MediaPlayer: error (1, -2147483648)
W/System.err: java.io.IOException: Prepare failed.: status=0x1
W/System.err: at android.media.MediaPlayer.prepare(Native Method)
W/System.err: at kr.co.composer.callrecord.media.AudioPlayer.initializeViews(AudioPlayer.java:56)
W/System.err: at kr.co.composer.callrecord.media.AudioPlayer.onCreate(AudioPlayer.java:42)
W/System.err: at android.app.Activity.performCreate(Activity.java:5231)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:135)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
E/MediaPlayer: Error (-38,0)
W/EGL_genymotion: eglSurfaceAttrib not implemented
stop called in state 0
error (-38, 0)
stop called in state 0
error (-38, 0)
Error (-38,0)
Error (-38,0)
stop called in state 0
error (-38, 0)
Error (-38,0)
pause called in state 0
error (-38, 0)
答案 0 :(得分:0)
可能由阅读权限引起。通过在manifest
文件中插入权限进行修复:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />