package cn.itcast.playonline;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
public class PlayonlineActivity extends Activity {
EditText et_path;
SurfaceView sv;
SurfaceHolder holder;
MediaPlayer player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) this.findViewById(R.id.editText1);
sv = (SurfaceView) this.findViewById(R.id.sv);
holder = sv.getHolder();
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void play(View view) {
try {
String path = et_path.getText().toString().trim();
player = new MediaPlayer();
player.setDataSource(path);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(holder);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
} catch (Exception e) {
System.out.println("error ");
e.printStackTrace();
}
}
}
Android 网络视频播放
最新推荐文章于 2024-06-22 18:39:15 发布