Android音乐播放器简单实现案例
目录
- 音乐播放器开发
- SoundPool 播放多个音频
- video View播放视频
- 控制摄像头摄像
音乐播放器开发
MediaPlayer 是Android 控制音频和视频文件播放类
1.创建MediaPlayer 对象 的Create方法
2.无can构造方法 -> setDataSorce -> prepare()加载创造文件
注意访问SDK需要授予权限
当Mediaplay.stop() 资源后需要重新加载资源,使用Mediaplay.setDataSource() 方法进行加载 ,最后还需要释放资源,防止内存泄漏
缺点:
1.延迟低长,占有资源多
2. 不支持同时播放多个音频
package com.example.myapplication; import android.media.MediaPlayer; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.IOException; /** * MediaPlay 实现简易的播放器 */ public class MainActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; //定义MediaPlayer对象 private boolean isPause = false; //定义是否暂停 private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageButton btn_play = (ImageButton) findViewById(R.id.btn_play); //获取“播放/暂停”按钮 final ImageButton btn_stop = (ImageButton) findViewById(R.id.btn_stop); //获取“停止”按钮 ; file = new File("/res/raw/bird.ogg"); if (file.exists()) { mediaPlayer = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath())); } else { Toast.makeText(MainActivity.this, "要播放的音频文件不存在!", Toast.LENGTH_SHORT).show(); return; } // 为MediaPlayer 添加监听事件 完成事件监听器,当media.play.stop 后在重新play mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { play(); } }); btn_play.setOnClickListener(new View.OnClickListener(){ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View view) { if(mediaPlayer.isPlaying() && !isPause){ mediaPlayer.pause(); isPause = true; // 暂停播放 // 更好播放图标 ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.drawable.play, null)); }else{ mediaPlayer.start(); //继续播放 // 更换为暂停图标 ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.drawable.pause, null)); isPause = false; //设置为播放状态 } } }); btn_stop.setOnClickListener(new View.OnClickListener() { //单击停止按钮,实现停止播放音频 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View v) { mediaPlayer.stop(); //停止播放 //更换为播放图标 btn_play.setImageDrawable(getResources().getDrawable(R.drawable.play, null)); } }); } private void play(){ // 重置MediaPlay mediaPlayer.reset(); try { mediaPlayer.setDataSource(file.getAbsolutePath()); mediaPlayer.prepare(); // 预加载 } catch (IOException e) { e.printStackTrace(); } } @Override protected void onDestroy() { if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } mediaPlayer.release(); super.onDestroy(); } }
SoundPool 播放多个音频
优点:
1. 延迟低
2.占有资源少
3. 支持多个音频播放
缺点: 不能播放大文件音频
应用场景: 小型游戏中多个飞机被击破声音
创建SoundPool 对象,load , play
package com.example.myapplication; import android.media.AudioAttributes; import android.media.SoundPool; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import java.util.HashMap; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.listView); // 创建SoundPool 对象设置音频相关特性 //1.音频对象 AudioAttributes attr = new AudioAttributes.Builder() //设置音效相关属性 .setUsage(AudioAttributes.USAGE_GAME) // 设置音效使用场景 .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) // 设置音效的类型 .build(); final SoundPool soundpool = new SoundPool.Builder() // 创建SoundPool对象 .setAudioAttributes(attr) // 设置音效池的属性 .setMaxStreams(10) // 设置最多可容纳10个音频流, .build(); // 要播放音频保存到HashMap中 final HashMap<Integer, Integer> soundmap = new HashMap<Integer, Integer>(); soundmap.put(0, soundpool.load(this, R.raw.cuckoo, 1)); soundmap.put(1, soundpool.load(this, R.raw.chimes, 1)); soundmap.put(2, soundpool.load(this, R.raw.notify, 1)); soundmap.put(3, soundpool.load(this, R.raw.ringout, 1)); soundmap.put(4, soundpool.load(this, R.raw.bird, 1)); soundmap.put(5, soundpool.load(this, R.raw.water, 1)); soundmap.put(6, soundpool.load(this, R.raw.cock, 1)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { soundpool.play(soundmap.get(i),1,1,0,0,1); } }); } }
video View播放视频
在XML中添加Videoview 组件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
读取SDK上数据 设置Mainifest.xml 设置访问权限,同时在Android 机器上设置权限打开
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> ----------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <manifest package="com.mingrisoft" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
package com.example.myapplication; import android.media.MediaPlayer; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.os.Environment; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import java.io.File; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); VideoView videoView = findViewById(R.id.video); File file = new File(Environment.getExternalStorageDirectory()+"/video.mp4"); if(file.exists()){ videoView.setVideoPath(file.getAbsolutePath()); }else{ return ; } // 控制视频播放 MediaController mc = new MediaController(MainActivity.this); videoView.setMediaController(mc); // videoiew 获取焦点 videoView.requestFocus(); videoView.start(); // 完成监听器 videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { Toast.makeText(MainActivity.this,"视频播放完成",Toast.LENGTH_SHORT).show(); } }); } }
控制摄像头摄像
<!-- 授予程序可以向SD卡中保存文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 授予程序使用摄像头的权限 --> <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera.autofocus"/> <uses-feature android:name="android.hardware.camera"/>
package com.example.myapplication; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Camera camera; private boolean isPreview = false; // 设置是否是预览状态 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置全屏显示 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); if (!android.os.Environment.getExternalStorageState().equals( //判断手机是否安装SD卡 android.os.Environment.MEDIA_MOUNTED)) { Toast.makeText(this, "请安装SD卡!", Toast.LENGTH_SHORT).show(); // 提示安装SD卡 } // 设置预览 final SurfaceView surfaceView = findViewById(R.id.surfaceView); final SurfaceHolder surfaceHolder = surfaceView.getHolder(); // 设置自己维护缓存 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); ImageButton preview = (ImageButton) findViewById(R.id.preview); //获取“预览”按钮 ImageButton takePicture = (ImageButton) findViewById(R.id.takephoto); //获取“拍照”按钮 preview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!isPreview) { camera = Camera.open(); isPreview = true; } try { camera.setPreviewDisplay(surfaceHolder); // 设置用于显示预览的SurfaceView Camera.Parameters parameters = camera.getParameters(); //获取相机参数 parameters.setPictureFormat(PixelFormat.JPEG); //指定图片为JPEG图片 parameters.set("jpeg-quality", 80); //设置图片的质量 camera.setParameters(parameters); //重新设置相机参数 camera.startPreview(); //开始预览 camera.autoFocus(null); //设置自动对焦 } catch (IOException e) { //输出异常信息 e.printStackTrace(); } } }); // 保存拍照 //实现相机拍照功能 takePicture.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (camera != null) { //相机不为空 camera.takePicture(null, null, jpeg); //进行拍照 } } }); } final Camera.PictureCallback jpeg = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // 根据拍照所得的数据创建位图 final Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); camera.stopPreview(); //停止预览 isPreview = false; //设置为非预览状态 //获取sd卡根目录 File appDir = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/"); if (!appDir.exists()) { //如果该目录不存在 appDir.mkdir(); //就创建该目录 } String fileName = System.currentTimeMillis() + ".jpg"; //将获取当前系统时间设置为照片名称 File file = new File(appDir, fileName); //创建文件对象 try { //保存拍到的图片 FileOutputStream fos = new FileOutputStream(file); //创建一个文件输出流对象 bm.compress(Bitmap.CompressFormat.JPEG, 100, fos); //将图片内容压缩为JPEG格式输出到输出流对象中 fos.flush(); //将缓冲区中的数据全部写出到输出流中 fos.close(); //关闭文件输出流对象 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 将照片插入到图库当中 try { MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), file.getAbsolutePath(),fileName,null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 更新通知图库 MainActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + ""))); restCamera(); } }; private void restCamera(){ if(!isPreview){ camera.startPreview(); isPreview = true; } } @Override protected void onPause() { //当暂停Activity时,停止预览并释放资源 if (camera != null) { //如果相机不为空 camera.stopPreview(); //停止预览 camera.release(); //释放资源 } super.onPause(); } }
到此这篇关于Android音乐播放器简单实现案例的文章就介绍到这了,更多相关Android音乐播放器内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)