我正在尝试在MediaPlayer中播放.mp4文件,但它总是在mediaplayer.prepare()中抛出IOException:
09-21 12:59:33.570 14926-14937 / com.alex.videoplayertest E / MediaPlayer:错误(-2147483648,0) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:java.io.IOException:准备失败:状态= 0x80000000 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.media.MediaPlayer.prepare(Native Method) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at com.alex.videoplayertest.MainActivity.onResume(MainActivity.java:53) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.Activity.performResume(Activity.java:5182) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread.access $ 600(ActivityThread.java:141) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.os.Handler.dispatchMessage(Handler.java:99) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.os.Looper.loop(Looper.java:137) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5041) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at java.lang.reflect.Method.invokeNative(Native Method) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at java.lang.reflect.Method.invoke(Method.java:511) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-21 12:59:33.570 14926-14926 / com.alex.videoplayertest W / System.err:at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public class MainActivity extends Activity
{
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume()
{
super.onResume();
String file = "/storage/emulated/0/Download/media/Light_Bulb.mp4";
File f = new File(file);
if (f.exists())
{
mediaPlayer = new MediaPlayer();
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
if (mediaPlayer != null)
mediaPlayer.start();
}
});
mediaPlayer.reset();
try
{
FileInputStream fileInputStream = new FileInputStream(file);
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(file));
fileInputStream.close();
mediaPlayer.prepare();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
有人可以帮助我吗?
答案 0 :(得分:0)
尝试使用
String file= Environment.getExternalStorageDirectory().toString()+"/Download/media/_29187_v.1_Light_Bulb_16HD_25fps.mp4";
File f = new File(file);
并添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到您的清单文件
答案 1 :(得分:0)
我现在有了解决问题的方法。我真的不知道为什么MediaPlayer不能在我的测试应用程序中播放这个视频,但是使用VideoView就可以了。
以下是我使用Videoview的代码:
public class VideoViewActivity extends Activity
{
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
videoView = (VideoView) findViewById(R.id.videoView);
}
@Override
protected void onResume()
{
super.onResume();
String file = Environment.getExternalStorageDirectory().toString() + "/Download/media/Light_Bulb.mp4";
File f = new File(file);
if (f.exists())
{
videoView.setMediaController(null);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
videoView.start();
}
});
videoView.setVideoURI(Uri.parse(file));
}
findViewById(R.id.closeVideoView).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
System.exit(0);
}
});
}
@Override
protected void onPause()
{
super.onPause();
if (videoView != null)
videoView.stopPlayback();
}