Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play Audio and Video with MediaPlay in Android

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

今天小编给大家分享一下Android中如何利用MediaPlay播放音频和视频的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

实现MediaPlay的基本步骤是

创建MediaPlay对象

调用setDataSource对象这是音频文件路径

调用prepare加载音频

调用start开始播放

调用pause暂停

调用stop停止播放

而MediaPlay也有其他方法

getCurrentPosition,获得当前播放位置

getDuration,获得播放时长

isPlay,判断是否处于播放状态

release,释放资源

reset,回到最初的状态

seekTo,设置播放位置

setVolume,设置音量

private MediaPlayer mediaPlayer=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaPlayer=new MediaPlayer(); if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1); }else{ initMediaPlayer(); } Button btPlayMp3= (Button) findViewById(R.id.btPlayMp3); btPlayMp3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!mediaPlayer.isPlaying()){ mediaPlayer.start(); } } }); Button btPauseMp3= (Button) findViewById(R.id.btPauseMp3); btPauseMp3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); } } }); Button btStopMp3= (Button) findViewById(R.id.btStopMp3); btStopMp3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } } } }); } private void initMediaPlayer(){ try { File file=new File(Environment.getExternalStorageDirectory()+"/music","honor.mp3"); mediaPlayer.setDataSource(file.getPath()); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if(requestCode==1){ if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){ initMediaPlayer(); }else{ Toast.makeText(this,"未获得SD卡访问权限",Toast.LENGTH_LONG).show(); finish(); } } } @Override protected void onDestroy() { if(mediaPlayer!=null){ mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer=null; } super.onDestroy(); }以上就是"Android中如何利用MediaPlay播放音频和视频"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report