本人菜鸟 今天在写一个简单的视频播放器的时候遇到了一个问题 就是运行程序的时候总是无法播放视频 LogCat里报错是Unable to create media player
下面是代码
package com.example;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private MediaPlayer mp;
private SurfaceView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
sv = (SurfaceView)findViewById(R.id.surfaceView1);
Button play = (Button)findViewById(R.id.button1);
final Button pause = (Button)findViewById(R.id.button2);
Button stop = (Button)findViewById(R.id.button3);
play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mp.reset();
try {
mp.setDataSource("sdcard/ccc.mp4");
mp.setDisplay(sv.getHolder());
mp.prepare();
mp.start();
sv.setBackgroundResource(R.drawable.bg_playing);
pause.setText("暂停");
pause.setEnabled(true);
} catch (IllegalStateException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(mp.isPlaying()){
mp.stop();
sv.setBackgroundResource(R.drawable.bg_finish);
pause.setEnabled(false);
}
}
});
pause.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.pause();
pause.setText("继续");
}
else{
mp.start();
pause.setText("暂停");
}
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
sv.setBackgroundResource(R.drawable.bg);
Toast.makeText(MainActivity.this, "视频播放完毕", Toast.LENGTH_SHORT).show();
}
});
final AudioManager am = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
MainActivity.this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
SeekBar sb = (SeekBar)findViewById(R.id.seekBar1);
sb.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
int progress = am.getStreamVolume(AudioManager.STREAM_MUSIC);
sb.setProgress(progress);
final TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("当前音量"+progress);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
tv.setText("当前音量"+progress);
am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_PLAY_SOUND);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO 自动生成的方法存根
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO 自动生成的方法存根
}
});
}
@Override
protected void onDestroy() {
if(mp.isPlaying()){
mp.stop();
}
mp.release();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();