安卓音、视频播放功能简单实现 --Android基础
程序员文章站
2022-04-30 22:50:38
1、音乐播放功能 关键代码: MainActivity.java: 2、视频播放功能(VideoView实现) MainActivity.java: ......
1、音乐播放功能
关键代码:
MainActivity.java:
package thonlon.example.cn.musicdemowithoutservice;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_play, btn_pause, btn_stop;
private MediaPlayer mediaPlayer = new MediaPlayer();//实例化MediaPlayer类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniview();
initMediaPlayer();
}
private void iniview() {
btn_play = (Button) findViewById(R.id.btn_play);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
}
private void initMediaPlayer() {
Uri uri = Uri.parse("http://www.nxl123.cn/snh.mp3");//网络中的音乐文件
try {
mediaPlayer.setDataSource(this, uri);//音乐文件路径
mediaPlayer.prepare();//资源文件准备
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initMediaPlayer();
} else {
Toast.makeText(this, "拒绝权限无法继续使用程序", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();//播放
}
break;
case R.id.btn_pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();//暂停
}
break;
case R.id.btn_stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();//停止
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
2、视频播放功能(VideoView实现)
MainActivity.java:
package thonlon.example.cn.videoplaydemowithoutservice;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_play, btn_pause, btn_replay;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_play = (Button) findViewById(R.id.btn_play);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_replay = (Button) findViewById(R.id.btn_replay);
videoView = (VideoView) findViewById(R.id.vv_show);
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_replay.setOnClickListener(this);
initVideoPath();
}
private void initVideoPath() {
//播放本地资源文件
videoView.setVideoURI(Uri.parse("android.resource://thonlon.example.cn.videoplaydemowithoutservice/"+R.raw.demo));//设置资源路径
// videoView.setMediaController(new MediaController(this));//播放时点击videoView显示控制条
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
if (!videoView.isPlaying()) {
videoView.start();//开始播放
}
break;
case R.id.btn_pause:
if (videoView.isPlaying()) {
videoView.pause();//暂停播放
}
break;
case R.id.btn_replay:
if (videoView.isPlaying()) {
videoView.resume();//重新播放
}
break;
default:
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideoPath();
} else {
Toast.makeText(this, "无法播放", Toast.LENGTH_SHORT).show();
finish();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null){
videoView.suspend();
}
}
}
上一篇: android连续拖动导致挂起的解决方法
下一篇: 用画布canvas画安卓logo