点击左上角的ImageButton会播放存入bmob中的音频
1.除了导入使用bmob需要的jar包和权限,还需要加入显示图片的clide的gar包:
app的build.gradle页面中,dependencies内加入以下两行:
implementation ("com.github.bumptech.glide:glide:4.9.0")
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
2.bmob手动存入需要的音频和图片
3.Video.java页面:
package com.example.two;
import android.graphics.drawable.Drawable;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BmobFile;
public class Video extends BmobObject {
private BmobFile PictureVideo;
private BmobFile Picture;
public BmobFile getPicture() {
return Picture;
}
public void setPicture(BmobFile picture) {
Picture = picture;
}
public BmobFile getPictureVideo() {
return PictureVideo;
}
public void setPictureVideo(BmobFile pictureVideo) {
PictureVideo = pictureVideo;
}
}
4.activity_picture.xml页面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PictureActivity">
<ImageButton
android:id="@+id/ibCogVideo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/ivCogPicture"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="100dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
5.PictureActivity.java页面:
package com.example.two;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.SoundPool;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DownloadFileListener;
import cn.bmob.v3.listener.FindListener;
public class PictureActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton ibCogVideo;
private ImageView ivCogPicture;
SoundPool mSoundPool; //一般用来播放短音频
private ArrayList<Video> vpList=new ArrayList<Video>();
private int index=0;
private String videoUrl;
HashMap<Integer,Integer> map=new HashMap<>(); //创建集合存放数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
Bmob.initialize(this,"d5fae4243d06df5c1fbfa8a8e4f16e63");
initViews();
bindViews();
initDatas();
}
/*初始化数据*/
private void initDatas() {
BmobQuery<Video> query=new BmobQuery<Video>(); //查询Bmob数据库中的信息
query.findObjects(new FindListener<Video>() {
@Override
public void done(List<Video> list, BmobException e) {
try {
for(int i=0;i<list.size();i++)
vpList.add(list.get(i)); //获取查询到的数据存入内存
Glide.with(PictureActivity.this).load(vpList.get(0).getPicture().getUrl()).into(ivCogPicture); //显示图片
/*下载音频*/
vpList.get(0).getPictureVideo().download(new DownloadFileListener() {
@Override
public void done(String s, BmobException e) {
videoUrl=s; //保存音频路径
map.put(1,mSoundPool.load(videoUrl,100)); //设置第一个音频
}
@Override
public void onProgress(Integer integer, long l) {
}
});
}catch (Exception ex){
ex.printStackTrace();
}
}
});
mSoundPool=new SoundPool(3, AudioManager.STREAM_MUSIC,0); //创建音频对象,参数为(可容纳音频个数,声音类型,音频品质默认为0)
}
/*绑定点击事件*/
private void bindViews() {
ibCogVideo.setOnClickListener(this);
ivCogPicture.setOnClickListener(this);
}
/*初始化控件*/
private void initViews() {
ibCogVideo=findViewById(R.id.ibCogVideo);
ivCogPicture=findViewById(R.id.ivCogPicture);
}
/*点击事件*/
@Override
public void onClick(View v) {
mSoundPool.play(map.get(1),1,1,100,0,1); //参数为(要播放的音频,左声道音量,右声道音量,音频优先级,循环次数,速率)
switch (v.getId()){
case R.id.ibCogVideo:
break;
case R.id.ivCogPicture:
index=index>vpList.size()?0:index+1;
Glide.with(PictureActivity.this).load(vpList.get(index).getPicture().getUrl()).into(ivCogPicture);
break;
}
}
}