Android项目实战 —— 手把手教你实现一款本地音乐播放器Dora Music

今天带大家实现一款基于Dora SDK的Android本地音乐播放器app,本项目也作为Dora SDK的实践项目或使用教程。使用到开源库有[https://github.com/dora4/dora] 、[https://github.com/dora4/dcache-android] 等。先声明一点,本项目主要作为框架的使用教程,界面风格不喜勿喷。

效果演示

1031691281506_.pic.jpg

1071691282562_.pic.jpg

1051691281511_.pic.jpg

1041691281509_.pic.jpg

实现功能

  1. 基本播放功能,包括播放、暂停、缓冲、后台播放等
  2. 播放模式切换
  3. 均衡器和重低音增强
  4. 耳机拔出暂停
  5. 音频焦点处理,和其他音乐播放器互斥
  6. 摇一摇切换歌曲
  7. 更换皮肤

知识产权

软著.jpeg

作品登记证书.png

框架搭建

我们要开发一款Android App,首先要搭建基础框架,比如使用MVP还是MVVM架构?使用什么网络库?使用什么ORM库?很显然,作为Dora SDK的使用教程,肯定是要依赖Dora SDK的。

 // Dora全家桶implementation("com.github.dora4:dcache-android:1.7.9")implementation("com.github.dora4:dora:1.1.9")implementation("com.github.dora4:dora-arouter-support:1.1")implementation("com.github.dora4:dora-apollo-support:1.1")implementation("com.github.dora4:dora-pgyer-support:1.0")
//    implementation 'com.github.dora4:dora-eventbus-support:1.1'implementation("com.github.dora4:dview-toggle-button:1.0")implementation("com.github.dora4:dview-alert-dialog:1.0")implementation("com.github.dora4:dview-loading-dialog:1.2")implementation("com.github.dora4:dview-colors:1.0")implementation("com.github.dora4:dview-skins:1.4")implementation("com.github.dora4:dview-bottom-dialog:1.1")
//    implementation 'com.github.dora4:dview-avatar:1.4'implementation("com.github.dora4:dview-titlebar:1.9")

列表功能使用BRVAH

implementation("io.github.cymchad:BaseRecyclerViewAdapterHelper:3.0.6")

运行时权限申请使用XXPermissions

implementation("com.github.getActivity:XXPermissions:18.2")

图片加载使用Glide

implementation("com.github.bumptech.glide:glide:4.11.0")

主要依赖的就是这些库。

应用入口MusicApp类编写

package site.doramusic.appimport dora.BaseApplication
import dora.db.Orm
import dora.db.OrmConfig
import dora.http.log.FormatLogInterceptor
import dora.http.retrofit.RetrofitManager
import site.doramusic.app.base.conf.AppConfig
import site.doramusic.app.db.Album
import site.doramusic.app.db.Artist
import site.doramusic.app.db.Folder
import site.doramusic.app.db.Music
import site.doramusic.app.http.service.CommonService
import site.doramusic.app.http.service.MusicService
import site.doramusic.app.http.service.UserService
import site.doramusic.app.media.MediaManagerclass MusicApp : BaseApplication(), AppConfig {/*** 全局的音乐播放控制管理器。*/var mediaManager: MediaManager? = nullprivate setcompanion object {/*** 全局Application单例。*/var instance: MusicApp? = nullprivate set}override fun onCreate() {super.onCreate()instance = thisinit()}private fun init() {initHttp()   // 初始化网络框架initDb()    // 初始化SQLite数据库的表initMedia() // 初始化媒体管理器}private fun initMedia() {mediaManager = MediaManager(this)}private fun initHttp() {RetrofitManager.initConfig {okhttp {interceptors().add(FormatLogInterceptor())build()}mappingBaseUrl(MusicService::class.java, AppConfig.URL_APP_SERVER)mappingBaseUrl(UserService::class.java, AppConfig.URL_APP_SERVER)mappingBaseUrl(CommonService::class.java, AppConfig.URL_CHAT_SERVER)}}private fun initDb() {Orm.init(this, OrmConfig.Builder().database(AppConfig.DB_NAME).version(AppConfig.DB_VERSION).tables(Music::class.java, Artist::class.java,Album::class.java, Folder::class.java).build())}
}

网络和ORM库都是来自于dcache-android库。首先初始化4张表,music、artist、album、folder,用来保存一些音乐信息。初始化网络库的时候添加一个FormatLogInterceptor日志拦截器,方便格式化输出网络请求日志。在Application中保存一个MediaManager单例,用来全局控制音乐的播放、暂停等。

MediaManager与整体媒体框架

我们使用MediaManager来统一管理媒体。由于要支持app后台运行时也能继续播放,所以我们考虑使用Service,而我们这不是一个简简单单的服务,而是要实时控制和反馈数据的。对于这样的一种场景,我们考虑将服务运行在单独的进程,并使用AIDL在主进程进行跨进程调用。


/*** 通过它调用AIDL远程服务接口。*/
class MediaManager(internal val context: Context) : IMediaService.Stub(), AppConfig {private var mediaService: IMediaService? = nullprivate val serviceConnection: ServiceConnectionprivate var onCompletionListener: MusicControl.OnConnectCompletionListener? = nullinit {this.serviceConnection = object : ServiceConnection {override fun onServiceConnected(name: ComponentName, service: IBinder) {mediaService = asInterface(service)if (mediaService != null) {//音频服务启动的标志LogUtils.i("MediaManager:connected")onCompletionListener!!.onConnectCompletion(mediaService)}}override fun onServiceDisconnected(name: ComponentName) {//音频服务断开的标志LogUtils.i("MediaManager:disconnected")}}}fun setOnCompletionListener(l: MusicControl.OnConnectCompletionListener) {onCompletionListener = l}fun connectService() {val intent = Intent(AppConfig.MEDIA_SERVICE)intent.setClass(context, MediaService::class.java)context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)}fun disconnectService() {context.unbindService(serviceConnection)context.stopService(Intent(AppConfig.MEDIA_SERVICE))}override fun play(pos: Int): Boolean {try {return mediaService?.play(pos) ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun playById(id: Int): Boolean {try {return mediaService?.playById(id) ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun playByPath(path: String) {try {mediaService?.playByPath(path)} catch (e: RemoteException) {e.printStackTrace()}}override fun playByUrl(music: Music, url: String) {try {mediaService?.playByUrl(music, url)} catch (e: RemoteException) {e.printStackTrace()}}override fun replay(): Boolean {try {return mediaService?.replay() ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun pause(): Boolean {try {return mediaService?.pause() ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun prev(): Boolean {try {return mediaService?.prev() ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun next(): Boolean {try {return mediaService?.next() ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun stop() {try {mediaService?.stop() ?: false} catch (e: RemoteException) {e.printStackTrace()}}override fun duration(): Int {try {return mediaService?.duration() ?: 0} catch (e: RemoteException) {e.printStackTrace()}return 0}override fun setCurMusic(music: Music) {try {mediaService?.setCurMusic(music) ?: false} catch (e: RemoteException) {e.printStackTrace()}}override fun position(): Int {try {return mediaService?.position() ?: 0} catch (e: RemoteException) {e.printStackTrace()}return 0}override fun pendingProgress(): Int {try {return mediaService?.pendingProgress() ?: 0} catch (e: RemoteException) {e.printStackTrace()}return 0}override fun seekTo(progress: Int): Boolean {try {return mediaService?.seekTo(progress) ?: false} catch (e: RemoteException) {e.printStackTrace()}return false}override fun refreshPlaylist(playlist: MutableList<Music>?) {try {mediaService?.refreshPlaylist(playlist)} catch (e: RemoteException) {e.printStackTrace()}}override fun setBassBoost(strength: Int) {try {mediaService?.setBassBoost(strength)} catch (e: RemoteException) {e.printStackTrace()}}override fun setEqualizer(bandLevels: IntArray) {try {mediaService?.setEqualizer(bandLevels)} catch (e: RemoteException) {e.printStackTrace()}}override fun getEqualizerFreq(): IntArray? {try {return mediaService?.equalizerFreq} catch (e: RemoteException) {e.printStackTrace()}return null}override fun getPlayState(): Int {try {return mediaService?.playState ?: 0} catch (e: RemoteException) {e.printStackTrace()}return 0}override fun getPlayMode(): Int {try {return mediaService?.playMode ?: 0} catch (e: RemoteException) {e.printStackTrace()}return 0}override fun setPlayMode(mode: Int) {try {mediaService?.playMode = mode} catch (e: RemoteException) {e.printStackTrace()}}override fun getCurMusicId(): Int {try {return mediaService?.curMusicId ?: -1} catch (e: Exception) {e.printStackTrace()}return -1}override fun loadCurMusic(music: Music): Boolean {try {return mediaService?.loadCurMusic(music) ?: false} catch (e: Exception) {e.printStackTrace()}return false}override fun getCurMusic(): Music? {try {return mediaService?.curMusic} catch (e: RemoteException) {e.printStackTrace()}return null}override fun getPlaylist(): MutableList<Music>? {try {return mediaService?.playlist} catch (e: Exception) {e.printStackTrace()}return null}override fun updateNotification(bitmap: Bitmap, title: String, name: String) {try {mediaService?.updateNotification(bitmap, title, name)} catch (e: RemoteException) {e.printStackTrace()}}override fun cancelNotification() {try {mediaService?.cancelNotification()} catch (e: RemoteException) {e.printStackTrace()}}
}

我们将服务配置在单独的进程,需要在AndroidManifest.xml中给service标签指定android:process,也就是进程标识,这样就分出了区别于应用主进程的一个新的进程。

<serviceandroid:name=".media.MediaService"android:process=":doramedia"android:exported="true"android:label="DoraMusic Media"><intent-filter><action android:name="site.doramusic.app.service.ACTION_MEDIA_SERVICE" /></intent-filter>
</service>

与媒体信息相关表的定义

Music歌曲表

package site.doramusic.app.db;import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;import androidx.annotation.NonNull;import dora.db.constraint.AssignType;
import dora.db.constraint.PrimaryKey;
import dora.db.migration.OrmMigration;
import dora.db.table.Column;
import dora.db.table.Ignore;
import dora.db.table.OrmTable;
import dora.db.table.PrimaryKeyEntry;
import dora.db.table.Table;
import site.doramusic.app.sort.Sort;/*** 歌曲表。*/
@Table("music")
public class Music implements OrmTable, Parcelable, Sort {public static final String COLUMN_ID = "_id";public static final String COLUMN_SONG_ID = "song_id";public static final String COLUMN_ALBUM_ID = "album_id";public static final String COLUMN_DURATION = "duration";public static final String COLUMN_MUSIC_NAME = "music_name";public static final String COLUMN_ARTIST = "artist";public static final String COLUMN_DATA = "data";public static final String COLUMN_FOLDER = "folder";public static final String COLUMN_MUSIC_NAME_KEY = "music_name_key";public static final String COLUMN_ARTIST_KEY = "artist_key";public static final String COLUMN_FAVORITE = "favorite";public static final String COLUMN_LAST_PLAY_TIME = "last_play_time";/*** 数据库中的_id*/@Column(COLUMN_ID)@PrimaryKey(AssignType.AUTO_INCREMENT)public int id;@Column(COLUMN_SONG_ID)public int songId = -1;@Column(COLUMN_ALBUM_ID)public int albumId = -1;@Column(COLUMN_DURATION)public int duration;@Column(COLUMN_MUSIC_NAME)public String musicName;@Column(COLUMN_ARTIST)public String artist;@Column(COLUMN_DATA)public String data;@Column(COLUMN_FOLDER)public String folder;@Column(COLUMN_MUSIC_NAME_KEY)public String musicNameKey;@Column(COLUMN_ARTIST_KEY)public String artistKey;@Column(COLUMN_FAVORITE)public int favorite;@Column(COLUMN_LAST_PLAY_TIME)public long lastPlayTime;@Ignoreprivate String sortLetter;@Ignoreprivate Type type;/*** 封面路径,在线歌曲用。*/@Ignoreprivate String coverPath;@NonNull@Overridepublic OrmMigration[] getMigrations() {return new OrmMigration[0];}public enum  Type {LOCAL, ONLINE}public Music() {}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {Bundle bundle = new Bundle();bundle.putInt(COLUMN_ID, id);bundle.putInt(COLUMN_SONG_ID, songId);bundle.putInt(COLUMN_ALBUM_ID, albumId);bundle.putInt(COLUMN_DURATION, duration);bundle.putString(COLUMN_MUSIC_NAME, musicName);bundle.putString(COLUMN_ARTIST, artist);bundle.putString(COLUMN_DATA, data);bundle.putString(COLUMN_FOLDER, folder);bundle.putString(COLUMN_MUSIC_NAME_KEY, musicNameKey);bundle.putString(COLUMN_ARTIST_KEY, artistKey);bundle.putInt(COLUMN_FAVORITE, favorite);bundle.putLong(COLUMN_LAST_PLAY_TIME, lastPlayTime);dest.writeBundle(bundle);}public static final Creator<Music> CREATOR = new Creator<Music>() {@Overridepublic Music createFromParcel(Parcel source) {Music music = new Music();Bundle bundle = source.readBundle(getClass().getClassLoader());music.id = bundle.getInt(COLUMN_ID);music.songId = bundle.getInt(COLUMN_SONG_ID);music.albumId = bundle.getInt(COLUMN_ALBUM_ID);music.duration = bundle.getInt(COLUMN_DURATION);music.musicName = bundle.getString(COLUMN_MUSIC_NAME);music.artist = bundle.getString(COLUMN_ARTIST);music.data = bundle.getString(COLUMN_DATA);music.folder = bundle.getString(COLUMN_FOLDER);music.musicNameKey = bundle.getString(COLUMN_MUSIC_NAME_KEY);music.artistKey = bundle.getString(COLUMN_ARTIST_KEY);music.favorite = bundle.getInt(COLUMN_FAVORITE);music.lastPlayTime = bundle.getLong(COLUMN_LAST_PLAY_TIME);return music;}@Overridepublic Music[] newArray(int size) {return new Music[size];}};@NonNull@Overridepublic String toString() {return "DoraMusic{" +"id=" + id +", songId=" + songId +", albumId=" + albumId +", duration=" + duration +", musicName='" + musicName + ''' +", artist='" + artist + ''' +", data='" + data + ''' +", folder='" + folder + ''' +", musicNameKey='" + musicNameKey + ''' +", artistKey='" + artistKey + ''' +", favorite=" + favorite +", lastPlayTime=" + lastPlayTime +'}';}@NonNull@Overridepublic PrimaryKeyEntry getPrimaryKey() {return new PrimaryKeyEntry(COLUMN_ID, id);}@Overridepublic boolean isUpgradeRecreated() {return false;}@Overridepublic String getSortLetter() {return sortLetter;}@Overridepublic void setSortLetter(String sortLetter) {this.sortLetter = sortLetter;}public void setType(Type type) {this.type = type;}public Type getType() {return type;}public void setCoverPath(String coverPath) {this.coverPath = coverPath;}public String getCoverPath() {return coverPath;}@Overridepublic int compareTo(Sort sort) {return getSortLetter().compareTo(sort.getSortLetter());}
}

Artist歌手表

package site.doramusic.app.db;import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;import androidx.annotation.NonNull;import dora.db.constraint.AssignType;
import dora.db.constraint.PrimaryKey;
import dora.db.migration.OrmMigration;
import dora.db.table.Column;
import dora.db.table.Ignore;
import dora.db.table.OrmTable;
import dora.db.table.PrimaryKeyEntry;
import dora.db.table.Table;
import site.doramusic.app.sort.Sort;/*** 歌手表。*/
@Table("artist")
public class Artist implements OrmTable, Parcelable, Sort {public static final String COLUMN_ID = "_id";public static final String COLUMN_ARTIST_NAME = "artist_name";public static final String COLUMN_NUMBER_OF_TRACKS = "number_of_tracks";@Ignoreprivate String sortLetter;@Column(COLUMN_ID)@PrimaryKey(AssignType.AUTO_INCREMENT)public int id;@Column(COLUMN_ARTIST_NAME)public String name;/*** 曲目数。*/@Column(COLUMN_NUMBER_OF_TRACKS)public int number_of_tracks;@Overridepublic int describeContents() {return 0;}public Artist() {}@Overridepublic void writeToParcel(Parcel dest, int flags) {Bundle bundle = new Bundle();bundle.putInt(COLUMN_ID, id);bundle.putString(COLUMN_ARTIST_NAME, name);bundle.putInt(COLUMN_NUMBER_OF_TRACKS, number_of_tracks);dest.writeBundle(bundle);}public static final Creator<Artist> CREATOR = new Creator<Artist>() {@Overridepublic Artist createFromParcel(Parcel source) {Bundle bundle = source.readBundle(getClass().getClassLoader());Artist artist = new Artist();artist.id = bundle.getInt(COLUMN_ID);artist.name = bundle.getString(COLUMN_ARTIST_NAME);artist.number_of_tracks = bundle.getInt(COLUMN_NUMBER_OF_TRACKS);return artist;}@Overridepublic Artist[] newArray(int size) {return new Artist[size];}};@NonNull@Overridepublic PrimaryKeyEntry getPrimaryKey() {return new PrimaryKeyEntry(COLUMN_ID, id);}@Overridepublic boolean isUpgradeRecreated() {return false;}@Overridepublic String getSortLetter() {return sortLetter;}@Overridepublic void setSortLetter(String sortLetter) {this.sortLetter = sortLetter;}public int compareTo(Sort sort) {return getSortLetter().compareTo(sort.getSortLetter());}@NonNull@Overridepublic OrmMigration[] getMigrations() {return new OrmMigration[0];}
}

Album专辑表

package site.doramusic.app.db;import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;import androidx.annotation.NonNull;import dora.db.constraint.AssignType;
import dora.db.constraint.PrimaryKey;
import dora.db.migration.OrmMigration;
import dora.db.table.Column;
import dora.db.table.Ignore;
import dora.db.table.OrmTable;
import dora.db.table.PrimaryKeyEntry;
import dora.db.table.Table;
import site.doramusic.app.sort.Sort;/*** 专辑表。*/
@Table("album")
public class Album implements OrmTable, Parcelable, Sort {public static final String COLUMN_ID = "_id";public static final String COLUMN_ALBUM_NAME = "album_name";public static final String COLUMN_ALBUM_ID = "album_id";public static final String COLUMN_NUMBER_OF_SONGS = "number_of_songs";public static final String COLUMN_ALBUM_COVER_PATH = "album_cover_path";@Column(COLUMN_ID)@PrimaryKey(AssignType.AUTO_INCREMENT)public int id;@Ignoreprivate String sortLetter;//专辑名称@Column(COLUMN_ALBUM_NAME)public String album_name;//专辑在数据库中的id@Column(COLUMN_ALBUM_ID)public int album_id = -1;//专辑的歌曲数目@Column(COLUMN_NUMBER_OF_SONGS)public int number_of_songs = 0;//专辑封面图片路径@Column(COLUMN_ALBUM_COVER_PATH)public String album_cover_path;@Overridepublic int describeContents() {return 0;}public Album() {}@Overridepublic void writeToParcel(Parcel dest, int flags) {Bundle bundle = new Bundle();bundle.putInt(COLUMN_ID, id);bundle.putString(COLUMN_ALBUM_NAME, album_name);bundle.putString(COLUMN_ALBUM_COVER_PATH, album_cover_path);bundle.putInt(COLUMN_NUMBER_OF_SONGS, number_of_songs);bundle.putInt(COLUMN_ALBUM_ID, album_id);dest.writeBundle(bundle);}public static final Creator<Album> CREATOR = new Creator<Album>() {@Overridepublic Album createFromParcel(Parcel source) {Album album = new Album();Bundle bundle = source.readBundle(getClass().getClassLoader());album.id = bundle.getInt(COLUMN_ID);album.album_name = bundle.getString(COLUMN_ALBUM_NAME);album.album_cover_path = bundle.getString(COLUMN_ALBUM_COVER_PATH);album.number_of_songs = bundle.getInt(COLUMN_NUMBER_OF_SONGS);album.album_id = bundle.getInt(COLUMN_ALBUM_ID);return album;}@Overridepublic Album[] newArray(int size) {return new Album[size];}};@NonNull@Overridepublic PrimaryKeyEntry getPrimaryKey() {return new PrimaryKeyEntry(COLUMN_ID, id);}@Overridepublic boolean isUpgradeRecreated() {return false;}@Overridepublic String getSortLetter() {return sortLetter;}@Overridepublic void setSortLetter(String sortLetter) {this.sortLetter = sortLetter;}@Overridepublic int compareTo(Sort sort) {return getSortLetter().compareTo(sort.getSortLetter());}@NonNull@Overridepublic OrmMigration[] getMigrations() {return new OrmMigration[0];}
}

Folder文件夹表

package site.doramusic.app.db;import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;import androidx.annotation.NonNull;import dora.db.constraint.AssignType;
import dora.db.constraint.NotNull;
import dora.db.constraint.PrimaryKey;
import dora.db.constraint.Unique;
import dora.db.migration.OrmMigration;
import dora.db.table.Column;
import dora.db.table.Ignore;
import dora.db.table.OrmTable;
import dora.db.table.PrimaryKeyEntry;
import dora.db.table.Table;
import site.doramusic.app.sort.Sort;/*** 文件夹表。*/
@Table("folder")
public class Folder implements OrmTable, Parcelable, Sort {public static final String COLUMN_ID = "_id";public static final String COLUMN_FOLDER_NAME = "folder_name";public static final String COLUMN_FOLDER_PATH = "folder_path";@Ignoreprivate String sortLetter;@Column(COLUMN_ID)@PrimaryKey(AssignType.AUTO_INCREMENT)public int id;@Column(COLUMN_FOLDER_NAME)public String name;@Unique@NotNull@Column(COLUMN_FOLDER_PATH)public String path;@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {Bundle bundle = new Bundle();bundle.putInt(COLUMN_ID, id);bundle.putString(COLUMN_FOLDER_NAME, name);bundle.putString(COLUMN_FOLDER_PATH, path);dest.writeBundle(bundle);}public Folder() {}public static Creator<Folder> CREATOR = new Creator<Folder>() {@Overridepublic Folder createFromParcel(Parcel source) {Folder folder = new Folder();Bundle bundle = source.readBundle(getClass().getClassLoader());folder.id = bundle.getInt(COLUMN_ID);folder.name = bundle.getString(COLUMN_FOLDER_NAME);folder.path = bundle.getString(COLUMN_FOLDER_PATH);return folder;}@Overridepublic Folder[] newArray(int size) {return new Folder[size];}};@NonNull@Overridepublic PrimaryKeyEntry getPrimaryKey() {return new PrimaryKeyEntry(COLUMN_ID, id);}@Overridepublic boolean isUpgradeRecreated() {return false;}@Overridepublic String getSortLetter() {return sortLetter;}@Overridepublic void setSortLetter(String sortLetter) {this.sortLetter = sortLetter;}@Overridepublic int compareTo(Sort sort) {return getSortLetter().compareTo(sort.getSortLetter());}@NonNull@Overridepublic OrmMigration[] getMigrations() {return new OrmMigration[0];}
}

这4张表的类主要演示dcache-android库的orm功能。我们可以看到@Table和@Column可以给表和列重命名,当然,不一定就会使用默认的表和列名规则。不是表字段的属性加上@Ignore。也可以通过@Unique配置唯一约束,通过@NotNull配置非空约束,使用@PrimaryKey配置主键约束。

MusicScanner本地歌曲扫描

package site.doramusic.app.mediaimport android.annotation.SuppressLint
import android.content.Context
import android.database.Cursor
import android.provider.MediaStore
import dora.db.Orm
import dora.db.Transaction
import dora.db.dao.DaoFactory
import dora.db.table.TableManager
import dora.util.PinyinUtils
import dora.util.TextUtils
import site.doramusic.app.base.conf.AppConfig
import site.doramusic.app.db.Album
import site.doramusic.app.db.Artist
import site.doramusic.app.db.Folder
import site.doramusic.app.db.Music
import site.doramusic.app.util.MusicUtils
import site.doramusic.app.util.PreferencesManager
import java.io.File
import java.util.*
import kotlin.collections.ArrayList/*** 媒体扫描器,用来扫描手机中的歌曲文件。*/
@SuppressLint("Range")
object MusicScanner : AppConfig {private val proj_music = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ARTIST_ID,MediaStore.Audio.Media.DURATION)private val proj_album = arrayOf(MediaStore.Audio.Albums.ALBUM,MediaStore.Audio.Albums.NUMBER_OF_SONGS, MediaStore.Audio.Albums._ID,MediaStore.Audio.Albums.ALBUM_ART)private val proj_artist = arrayOf(MediaStore.Audio.Artists.ARTIST,MediaStore.Audio.Artists.NUMBER_OF_TRACKS)private val proj_folder = arrayOf(MediaStore.Files.FileColumns.DATA)private val musicDao = DaoFactory.getDao(Music::class.java)private val artistDao = DaoFactory.getDao(Artist::class.java)private val albumDao = DaoFactory.getDao(Album::class.java)private val folderDao = DaoFactory.getDao(Folder::class.java)private fun recreateTables() {TableManager.recreateTable(Music::class.java)TableManager.recreateTable(Artist::class.java)TableManager.recreateTable(Album::class.java)TableManager.recreateTable(Folder::class.java)}@JvmStaticfun scan(context: Context): List<Music> {recreateTables()var musics = arrayListOf<Music>()Transaction.execute(Music::class.java) {musics = queryMusic(context, AppConfig.ROUTE_START_FROM_LOCAL) as ArrayList<Music>it.insert(musics)}if (musics.size > 0) {// 歌曲都没有就没有必要查询歌曲信息了Transaction.execute {val artists = queryArtist(context)artistDao.insert(artists)val albums = queryAlbum(context)albumDao.insert(albums)val folders = queryFolder(context)folderDao.insert(folders)}}return musics}@JvmStaticfun queryMusic(context: Context, from: Int): List<Music> {return queryMusic(context, null, null, from)}@JvmStaticfun queryMusic(context: Context,selections: String?, selection: String?, from: Int): List<Music> {val sp = PreferencesManager(context)val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URIval cr = context.contentResolverval select = StringBuffer(" 1=1 ")// 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件if (sp.getFilterSize()) {select.append(" and ${MediaStore.Audio.Media.SIZE} > " +"${AppConfig.SCANNER_FILTER_SIZE}")}if (sp.getFilterTime()) {select.append(" and ${MediaStore.Audio.Media.DURATION} > " +"${AppConfig.SCANNER_FILTER_DURATION}")}if (TextUtils.isNotEmpty(selections)) {select.append(selections)}return when (from) {AppConfig.ROUTE_START_FROM_LOCAL -> if (musicDao.count() > 0) {musicDao.selectAll()} else {getMusicList(cr.query(uri, proj_music,select.toString(), null,MediaStore.Audio.Media.ARTIST_KEY))}AppConfig.ROUTE_START_FROM_ARTIST -> if (musicDao.count() > 0) {queryMusic(selection,AppConfig.ROUTE_START_FROM_ARTIST)} else {getMusicList(cr.query(uri, proj_music,select.toString(), null,MediaStore.Audio.Media.ARTIST_KEY))}AppConfig.ROUTE_START_FROM_ALBUM -> {if (musicDao.count() > 0) {return queryMusic(selection,AppConfig.ROUTE_START_FROM_ALBUM)}if (musicDao.count() > 0) {return queryMusic(selection, AppConfig.ROUTE_START_FROM_FOLDER)}if (musicDao.count() > 0) {return queryMusic(selection, AppConfig.ROUTE_START_FROM_FAVORITE)}if (musicDao.count() > 0) {queryMusic(selection, AppConfig.ROUTE_START_FROM_LATEST)} else arrayListOf()}AppConfig.ROUTE_START_FROM_FOLDER -> {if (musicDao.count() > 0) {return queryMusic(selection, AppConfig.ROUTE_START_FROM_FOLDER)}if (musicDao.count() > 0) {return queryMusic(selection, AppConfig.ROUTE_START_FROM_FAVORITE)}if (musicDao.count() > 0) {queryMusic(selection, AppConfig.ROUTE_START_FROM_LATEST)} else arrayListOf()}AppConfig.ROUTE_START_FROM_FAVORITE -> {if (musicDao.count() > 0) {return queryMusic(selection, AppConfig.ROUTE_START_FROM_FAVORITE)}if (musicDao.count() > 0) {queryMusic(selection, AppConfig.ROUTE_START_FROM_LATEST)} else arrayListOf()}AppConfig.ROUTE_START_FROM_LATEST -> {if (musicDao.count() > 0) {queryMusic(selection, AppConfig.ROUTE_START_FROM_LATEST)} else arrayListOf()}else -> arrayListOf()}}@JvmStaticfun queryMusic(selection: String?, type: Int): List<Music> {val db = Orm.getDB()var sql = ""when (type) {AppConfig.ROUTE_START_FROM_ARTIST -> {sql = "select * from music where ${Music.COLUMN_ARTIST} = ?"}AppConfig.ROUTE_START_FROM_ALBUM -> {sql = "select * from music where ${Music.COLUMN_ALBUM_ID} = ?"}AppConfig.ROUTE_START_FROM_FOLDER -> {sql = "select * from music where ${Music.COLUMN_FOLDER} = ?"}AppConfig.ROUTE_START_FROM_FAVORITE -> {sql = "select * from music where ${Music.COLUMN_FAVORITE} = ?"//        } else if (type == ROUTE_START_FROM_DOWNLOAD) {
//            sql = "select * from music where download = ?";}AppConfig.ROUTE_START_FROM_LATEST -> {sql = "select * from music where ${Music.COLUMN_LAST_PLAY_TIME} > ? order by " +"${Music.COLUMN_LAST_PLAY_TIME} desc limit 100"}}return parseCursor(db.rawQuery(sql, arrayOf(selection)))}private fun parseCursor(cursor: Cursor): List<Music> {val list: MutableList<Music> = ArrayList()while (cursor.moveToNext()) {val music = Music()music.id = cursor.getInt(cursor.getColumnIndex(Music.COLUMN_ID))music.songId = cursor.getInt(cursor.getColumnIndex(Music.COLUMN_SONG_ID))music.albumId = cursor.getInt(cursor.getColumnIndex(Music.COLUMN_ALBUM_ID))music.duration = cursor.getInt(cursor.getColumnIndex(Music.COLUMN_DURATION))music.musicName = cursor.getString(cursor.getColumnIndex(Music.COLUMN_MUSIC_NAME))music.artist = cursor.getString(cursor.getColumnIndex(Music.COLUMN_ARTIST))music.data = cursor.getString(cursor.getColumnIndex(Music.COLUMN_DATA))music.folder = cursor.getString(cursor.getColumnIndex(Music.COLUMN_FOLDER))music.musicNameKey = cursor.getString(cursor.getColumnIndex(Music.COLUMN_MUSIC_NAME_KEY))music.artistKey = cursor.getString(cursor.getColumnIndex(Music.COLUMN_ARTIST_KEY))music.favorite = cursor.getInt(cursor.getColumnIndex(Music.COLUMN_FAVORITE))music.lastPlayTime = cursor.getLong(cursor.getColumnIndex(Music.COLUMN_LAST_PLAY_TIME))list.add(music)}cursor.close()return list}/*** 获取包含音频文件的文件夹信息。** @param context* @return*/@JvmStaticfun queryFolder(context: Context): List<Folder> {val sp = PreferencesManager(context)val uri = MediaStore.Files.getContentUri("external")val cr = context.contentResolverval selection = StringBuilder(MediaStore.Files.FileColumns.MEDIA_TYPE+ " = " + MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO + " and " + "("+ MediaStore.Files.FileColumns.DATA + " like '%.mp3' or "+ MediaStore.Files.FileColumns.DATA + " like '%.flac' or "+ MediaStore.Files.FileColumns.DATA + " like '%.wav' or "+ MediaStore.Files.FileColumns.DATA + " like '%.ape' or "+ MediaStore.Files.FileColumns.DATA + " like '%.m4a' or "+ MediaStore.Files.FileColumns.DATA + " like '%.aac')")// 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件if (sp.getFilterSize()) {selection.append(" and " + MediaStore.Audio.Media.SIZE + " > " + AppConfig.SCANNER_FILTER_SIZE)}if (sp.getFilterTime()) {selection.append(" and " + MediaStore.Audio.Media.DURATION + " > " + AppConfig.SCANNER_FILTER_DURATION)}
//        selection.append(") group by ( " + MediaStore.Files.FileColumns.PARENT)return if (folderDao.count() > 0) {folderDao.selectAll()} else {getFolderList(cr.query(uri, proj_folder, selection.toString(), null, null))}}/*** 获取歌手信息。** @param context* @return*/@JvmStaticfun queryArtist(context: Context): List<Artist> {val uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URIval cr = context.contentResolverreturn if (artistDao.count() > 0) {artistDao.selectAll()} else {getArtistList(cr.query(uri, proj_artist,null, null, MediaStore.Audio.Artists.NUMBER_OF_TRACKS+ " desc"))}}/*** 获取专辑信息。** @param context* @return*/@JvmStaticfun queryAlbum(context: Context): List<Album> {val sp = PreferencesManager(context)val uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URIval cr = context.contentResolverval where = StringBuilder(MediaStore.Audio.Albums._ID+ " in (select distinct " + MediaStore.Audio.Media.ALBUM_ID+ " from audio_meta where (1=1 ")if (sp.getFilterSize()) {where.append(" and " + MediaStore.Audio.Media.SIZE + " > " + AppConfig.SCANNER_FILTER_SIZE)}if (sp.getFilterTime()) {where.append(" and " + MediaStore.Audio.Media.DURATION + " > " + AppConfig.SCANNER_FILTER_DURATION)}where.append("))")return if (albumDao.count() > 0) {albumDao.selectAll()} else { // Media.ALBUM_KEY 按专辑名称排序// FIXME:  Android11的Invalid token select问题getAlbumList(cr.query(uri, proj_album,null, null, MediaStore.Audio.Media.ALBUM_KEY))}}private fun getMusicList(cursor: Cursor?): List<Music> {val list: MutableList<Music> = ArrayList()if (cursor == null) {return list}while (cursor.moveToNext()) {val music = Music()val filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))music.songId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))music.albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))val duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))if (duration > 0) {music.duration = duration} else {try {music.duration = MusicUtils.getDuration(filePath)} catch (e: RuntimeException) {continue}}music.musicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))music.artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))music.data = filePathval folderPath = filePath.substring(0,filePath.lastIndexOf(File.separator))music.folder = folderPathmusic.musicNameKey = PinyinUtils.getPinyinFromSentence(music.musicName)music.artistKey = PinyinUtils.getPinyinFromSentence(music.artist)list.add(music)}cursor.close()return list}private fun getAlbumList(cursor: Cursor?): List<Album> {val list: MutableList<Album> = ArrayList()if (cursor == null) {return list}while (cursor.moveToNext()) {val album = Album()album.album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM))album.album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Albums._ID))album.number_of_songs = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS))album.album_cover_path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART))list.add(album)}cursor.close()return list}private fun getArtistList(cursor: Cursor?): List<Artist> {val list: MutableList<Artist> = ArrayList()if (cursor == null) {return list}while (cursor.moveToNext()) {val artist = Artist()artist.name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST))artist.number_of_tracks = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS))list.add(artist)}cursor.close()return list}private fun getFolderList(cursor: Cursor?): List<Folder> {val list: MutableList<Folder> = ArrayList()if (cursor == null) {return list}while (cursor.moveToNext()) {val folder = Folder()val filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA))folder.path = filePath.substring(0,filePath.lastIndexOf(File.separator))folder.name = folder.path.substring(folder.path.lastIndexOf(File.separator) + 1)list.add(folder)}cursor.close()return list}
}

我们可以看到,使用DaoFactory.getDao拿到dao对象就可以以ORM的方式操作数据库中的表了。

MusicControl媒体控制的具体实现

package site.doramusic.app.media;import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.BassBoost;
import android.media.audiofx.Equalizer;
import android.os.Build;
import android.os.PowerManager;import com.lsxiao.apollo.core.Apollo;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;import dora.db.builder.WhereBuilder;
import dora.db.dao.DaoFactory;
import dora.db.dao.OrmDao;
import dora.util.LogUtils;
import dora.util.TextUtils;
import dora.util.ToastUtils;
import site.doramusic.app.base.conf.ApolloEvent;
import site.doramusic.app.base.conf.AppConfig;
import site.doramusic.app.db.Music;
import site.doramusic.app.util.PreferencesManager;/*** 音乐播放流程控制。*/
public class MusicControl implements MediaPlayer.OnCompletionListener, AppConfig {private final Random mRandom;private int mPlayMode;private final MediaPlayerProxy mMediaPlayer;private final List<Music> mPlaylist;private final Context mContext;private int mCurPlayIndex;private int mPlayState;private int mPendingProgress;private final int mCurMusicId;private Music mCurMusic;private boolean mPlaying;private final AudioManager mAudioManager;private final OrmDao<Music> mDao;private final PreferencesManager mPrefsManager;public MusicControl(Context context) {this.mContext = context;this.mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);this.mPrefsManager = new PreferencesManager(context);this.mPlayMode = MPM_LIST_LOOP_PLAY;    //默认列表循环this.mPlayState = MPS_NO_FILE;  //默认没有音频文件播放this.mCurPlayIndex = -1;this.mCurMusicId = -1;this.mPlaylist = new ArrayList<>();this.mDao = DaoFactory.INSTANCE.getDao(Music.class);this.mMediaPlayer = new MediaPlayerProxy();this.mMediaPlayer.setNeedCacheAudio(true);this.mMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK); //播放音频的时候加锁,防止CPU休眠if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {AudioAttributes attrs = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();this.mMediaPlayer.setAudioAttributes(attrs);} else {this.mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);}this.mMediaPlayer.setOnCompletionListener(this);this.mRandom = new Random();this.mRandom.setSeed(System.currentTimeMillis());}/*** 设置重低音参数。** @param strength*/public void setBassBoost(int strength) {int audioSessionId = mMediaPlayer.getAudioSessionId();BassBoost bassBoost = new BassBoost(0, audioSessionId);BassBoost.Settings settings = new BassBoost.Settings();settings.strength = (short) strength;bassBoost.setProperties(settings);bassBoost.setEnabled(true);bassBoost.setParameterListener(new BassBoost.OnParameterChangeListener() {@Overridepublic void onParameterChange(BassBoost effect, int status, int param, short value) {LogUtils.i("重低音参数改变");}});}/*** 获取均衡器支持的频率。** @return*/public int[] getEqualizerFreq() {int audioSessionId = mMediaPlayer.getAudioSessionId();Equalizer equalizer = new Equalizer(0, audioSessionId);short bands = equalizer.getNumberOfBands();int[] freqs = new int[bands];for (short i = 0; i < bands; i++) {int centerFreq = equalizer.getCenterFreq(i) / 1000;freqs[i] = centerFreq;}return freqs;}/*** 设置均衡器。** @param bandLevels*/public void setEqualizer(int[] bandLevels) {int audioSessionId = mMediaPlayer.getAudioSessionId();Equalizer equalizer = new Equalizer(1, audioSessionId);// 获取均衡控制器支持最小值和最大值short minEQLevel = equalizer.getBandLevelRange()[0];//第一个下标为最低的限度范围short maxEQLevel = equalizer.getBandLevelRange()[1];  // 第二个下标为最高的限度范围int distanceEQLevel = maxEQLevel - minEQLevel;int singleEQLevel = distanceEQLevel / 25;for (short i = 0; i < bandLevels.length; i++) {equalizer.setBandLevel(i, (short) (singleEQLevel * bandLevels[i]));}equalizer.setEnabled(true);equalizer.setParameterListener(new Equalizer.OnParameterChangeListener() {@Overridepublic void onParameterChange(Equalizer effect, int status, int param1, int param2, int value) {LogUtils.i("均衡器参数改变:" + status + "," + param1 + "," + param2 + "," + value);}});}/*** 保存收藏。** @param music*/private void saveFavorite(Music music) {music.favorite = 1;mDao.update(WhereBuilder.Companion.create().addWhereEqualTo("_id", music.id), music);}/*** 保存最近播放。** @param music*/private void saveLatest(Music music) {//更新本地缓存歌曲music.lastPlayTime = System.currentTimeMillis();mDao.update(WhereBuilder.Companion.create().addWhereEqualTo("_id", music.id), music);}/*** 设置播放。** @param playState*/public void setPlaying(int playState) {switch (playState) {case MPS_PLAYING:mPlaying = true;break;default:mPlaying = false;}}/*** 设置当前播放的歌曲。** @param music* @return*/public boolean loadCurMusic(Music music) {if (prepare(seekPosById(mPlaylist, music.songId))) {this.mCurMusic = music;return true;}return false;}/*** 修改当前播放歌曲的信息。** @param music* @return*/public void setCurMusic(Music music) {this.mPlaylist.set(mCurPlayIndex, music);this.mCurMusic = music;}/*** 缓冲准备。** @param pos* @return*/public boolean prepare(int pos) {mCurPlayIndex = pos;mPendingProgress = 0;mMediaPlayer.reset();if (mPrefsManager.getBassBoost()) {setBassBoost(1000);} else {setBassBoost(1);}if (!mPrefsManager.getEqualizerDecibels().equals("")) {int[] equalizerFreq = getEqualizerFreq();int[] decibels = new int[equalizerFreq.length];String[] values = mPrefsManager.getEqualizerDecibels().split(",");for (int i = 0; i < decibels.length; i++) {decibels[i] = Integer.valueOf(values[i]);}setEqualizer(decibels);}String path = mPlaylist.get(pos).data;if (TextUtils.isNotEmpty(path)) {try {mMediaPlayer.setDataSource(path);mMediaPlayer.prepare();mPlayState = MPS_PREPARE;} catch (Exception e) {mPlayState = MPS_INVALID;if (pos < mPlaylist.size()) {pos++;playById(mPlaylist.get(pos).songId);}return false;}} else {ToastUtils.showShort(mContext, "歌曲路径为空");}mCurMusic = mPlaylist.get(mCurPlayIndex);sendMusicPlayBroadcast();return true;}/*** 根据歌曲的id来播放。** @param id* @return*/public boolean playById(int id) {if (requestFocus()) {int position = seekPosById(mPlaylist, id);mCurPlayIndex = position;if (mCurMusicId == id) {if (!mMediaPlayer.isPlaying()) {mMediaPlayer.start();mPlayState = MPS_PLAYING;sendMusicPlayBroadcast();mCurMusic = mPlaylist.get(mCurPlayIndex);saveLatest(mCurMusic);} else {pause();}return true;}if (!prepare(position)) {return false;}return replay();} else {return false;}}/*** 根据URL播放歌曲。** @param music* @param url*/public void playByUrl(Music music, String url) {if (requestFocus()) {try {mMediaPlayer.setAudioCachePath(music.data);mMediaPlayer.setOnCachedProgressUpdateListener(new MediaPlayerProxy.OnCachedProgressUpdateListener() {@Overridepublic void updateCachedProgress(int progress) {mPendingProgress = progress;}});String localProxyUrl = mMediaPlayer.getLocalURLAndSetRemoteSocketAddress(url);mPlaylist.add(mCurPlayIndex, music);    //插入到当前播放位置mCurMusic = music;mMediaPlayer.startProxy();mMediaPlayer.reset();mMediaPlayer.setDataSource(localProxyUrl);mMediaPlayer.prepareAsync();mMediaPlayer.start();mPlayState = MPS_PLAYING;sendMusicPlayBroadcast();} catch (Exception e) {e.printStackTrace();}}}/*** 根据本地文件路径播放歌曲。** @param path*/public void play(String path) {if (requestFocus()) {try {mMediaPlayer.stop();mMediaPlayer.reset();mMediaPlayer.setDataSource(path);mMediaPlayer.prepare();mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Overridepublic void onPrepared(MediaPlayer mp) {mMediaPlayer.start();sendMusicPlayBroadcast();}});} catch (IOException e) {e.printStackTrace();}}}/*** 停止播放歌曲。*/public void stop() {if (mMediaPlayer.isPlaying()) {mMediaPlayer.stop();}}AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {public void onAudioFocusChange(int focusChange) {if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {// Pause playbackpause();} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {// Resume playbackreplay();} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {mAudioManager.abandonAudioFocus(audioFocusListener);pause();}}};/*** 请求音频焦点。** @return*/private boolean requestFocus() {// Request audio focus for playbackint result = mAudioManager.requestAudioFocus(audioFocusListener,// Use the music stream.AudioManager.STREAM_MUSIC,// Request permanent focus.AudioManager.AUDIOFOCUS_GAIN);return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;}/*** 根据位置播放列表中的歌曲。** @param pos* @return*/public boolean play(int pos) {if (requestFocus()) {if (mCurPlayIndex == pos) {if (!mMediaPlayer.isPlaying()) {mMediaPlayer.start();mPlayState = MPS_PLAYING;sendMusicPlayBroadcast();mCurMusic = mPlaylist.get(mCurPlayIndex);saveLatest(mCurMusic);} else {pause();}return true;}if (!prepare(pos)) {return false;}return replay();} else {return false;}}/*** 获取当前播放歌曲的索引。** @return*/public int getCurPlayIndex() {return mCurPlayIndex;}/*** 保证索引在播放列表索引范围内。** @param index* @return*/private int reviseIndex(int index) {if (index < 0) {index = mPlaylist.size() - 1;}if (index >= mPlaylist.size()) {index = 0;}return index;}/*** 获取当前歌曲播放的位置。** @return*/public int position() {if (mPlayState == MPS_PLAYING || mPlayState == MPS_PAUSE) {return mMediaPlayer.getCurrentPosition();}return 0;}/*** 获取当前歌曲的时长。** @return 毫秒*/public int duration() {if (mPlayState == MPS_INVALID || mPlayState == MPS_NO_FILE) {return 0;}return mMediaPlayer.getDuration();}/*** 跳到指定进度播放歌曲。** @param progress* @return*/public boolean seekTo(int progress) {if (mPlayState == MPS_INVALID || mPlayState == MPS_NO_FILE) {return false;}int pro = reviseSeekValue(progress);int time = mMediaPlayer.getDuration();int curTime = (int) ((float) pro / 100 * time);mMediaPlayer.seekTo(curTime);return true;}/*** 获取歌曲的播放模式。** @return*/public int getPlayMode() {return mPlayMode;}/*** 设置歌曲的播放模式。** @param mode*/public void setPlayMode(int mode) {this.mPlayMode = mode;}/*** 清空播放列表。*/public void clear() {mMediaPlayer.stop();mMediaPlayer.reset();}/*** 在线缓冲进度。** @return*/public int pendingProgress() {return mPendingProgress;}public interface OnConnectCompletionListener {void onConnectCompletion(IMediaService service);}/*** 获取当前正在播放的歌曲。** @return*/public Music getCurMusic() {return mCurMusic;}/*** 检测当前歌曲是否正在播放中。** @return*/public boolean isPlaying() {return mPlaying;}/*** 暂停当前歌曲的播放。** @return*/public boolean pause() {if (mPlayState != MPS_PLAYING) {return false;}mMediaPlayer.pause();mPlayState = MPS_PAUSE;mCurMusic = mPlaylist.get(mCurPlayIndex);sendMusicPlayBroadcast();return true;}/*** 播放上一首。** @return*/public boolean prev() {switch (mPlayMode) {case AppConfig.MPM_LIST_LOOP_PLAY:    //列表循环return moveLeft();case AppConfig.MPM_ORDER_PLAY:    //顺序播放if (mCurPlayIndex != 0) {return moveLeft();} else {return prepare(mCurPlayIndex);}case AppConfig.MPM_RANDOM_PLAY:   //随机播放int index = getRandomIndex();if (index != -1) {mCurPlayIndex = index;} else {mCurPlayIndex = 0;}if (prepare(mCurPlayIndex)) {return replay();}return false;case AppConfig.MPM_SINGLE_LOOP_PLAY:  //单曲循环prepare(mCurPlayIndex);return replay();default:return false;}}/*** 播放下一首。** @return*/public boolean next() {switch (mPlayMode) {case MPM_LIST_LOOP_PLAY:    //列表循环return moveRight();case MPM_ORDER_PLAY:    //顺序播放if (mCurPlayIndex != mPlaylist.size() - 1) {return moveRight();} else {return prepare(mCurPlayIndex);}case MPM_RANDOM_PLAY:   //随机播放int index = getRandomIndex();if (index != -1) {mCurPlayIndex = index;} else {mCurPlayIndex = 0;}if (prepare(mCurPlayIndex)) {return replay();}return false;case MPM_SINGLE_LOOP_PLAY:  //单曲循环prepare(mCurPlayIndex);return replay();default:return false;}}@Overridepublic void onCompletion(MediaPlayer mp) {next();}/*** 随机播放模式下获取播放索引。** @return*/private int getRandomIndex() {int size = mPlaylist.size();if (size == 0) {return -1;}return Math.abs(mRandom.nextInt() % size);}/*** 修正缓冲播放的进度在合理的范围内。** @param progress* @return*/private int reviseSeekValue(int progress) {if (progress < 0) {progress = 0;} else if (progress > 100) {progress = 100;}return progress;}/*** 刷新播放列表的歌曲。** @param playlist*/public void refreshPlaylist(List<Music> playlist) {mPlaylist.clear();mPlaylist.addAll(playlist);if (mPlaylist.size() == 0) {mPlayState = MPS_NO_FILE;mCurPlayIndex = -1;return;}}/*** 在当前播放模式下播放上一首。** @return*/public boolean moveLeft() {if (mPlayState == MPS_NO_FILE) {return false;}mCurPlayIndex--;mCurPlayIndex = reviseIndex(mCurPlayIndex);if (!prepare(mCurPlayIndex)) {return false;}return replay();}/*** 在当前播放模式下播放下一首。** @return*/public boolean moveRight() {if (mPlayState == MPS_NO_FILE) {return false;}mCurPlayIndex++;mCurPlayIndex = reviseIndex(mCurPlayIndex);if (!prepare(mCurPlayIndex)) {return false;}return replay();}/*** 重头开始播放当前歌曲。** @return*/public boolean replay() {if (requestFocus()) {if (mPlayState == MPS_INVALID || mPlayState == MPS_NO_FILE) {return false;}mMediaPlayer.start();mPlayState = MPS_PLAYING;sendMusicPlayBroadcast();mCurMusic = mPlaylist.get(mCurPlayIndex);saveLatest(mCurMusic);return true;} else {return false;}}/*** 发送音乐播放/暂停的广播。*/private void sendMusicPlayBroadcast() {setPlaying(mPlayState);Intent intent = new Intent(ACTION_PLAY);intent.putExtra("play_state", mPlayState);mContext.sendBroadcast(intent);Apollo.emit(ApolloEvent.REFRESH_LOCAL_NUMS);}/*** 获取当前的播放状态。** @return*/public int getPlayState() {return mPlayState;}/*** 获取播放列表。** @return*/public List<Music> getPlaylist() {return mPlaylist;}/*** 退出媒体播放。*/public void exit() {mMediaPlayer.stop();mMediaPlayer.release();mCurPlayIndex = -1;mPlaylist.clear();}/*** 根据歌曲的ID,寻找出歌曲在当前播放列表中的位置。** @param playlist* @param id* @return*/public int seekPosById(List<Music> playlist, int id) {if (id == -1) {return -1;}int result = -1;if (playlist != null) {for (int i = 0; i < playlist.size(); i++) {if (id == playlist.get(i).songId) {result = i;break;}}}return result;}
}

前面我们提到使用AIDL进行跨进程访问。那么整体调用顺序是,MediaManager->MediaService->MusicControl。MediaManager调用层,相当于一个外包装或者说是门面。MediaService中间层,用于后台访问。MusicControl实现层。

ShakeDetector摇一摇切歌

package site.doramusic.app.shakeimport android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Handlerimport site.doramusic.app.util.PreferencesManager/*** 摇一摇切歌。*/
class ShakeDetector(context: Context) : SensorEventListener {private val sensorManager: SensorManager?private var onShakeListener: OnShakeListener? = nullprivate val prefsManager: PreferencesManagerprivate var lowX: Float = 0.toFloat()private var lowY: Float = 0.toFloat()private var lowZ: Float = 0.toFloat()private var shaking: Boolean = falseprivate val shakeHandler: Handler by lazy {Handler()}companion object {private const val FILTERING_VALUE = 0.1f}init {// 获取传感器管理服务sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManagerprefsManager = PreferencesManager(context)}private val r: Runnable = Runnable {shaking = false}override fun onSensorChanged(event: SensorEvent) {if (prefsManager.getShakeChangeMusic() && event.sensor.type == Sensor.TYPE_ACCELEROMETER) {if (!shaking) {shakeHandler.removeCallbacks(r)val x = event.values[SensorManager.DATA_X]val y = event.values[SensorManager.DATA_Y]val z = event.values[SensorManager.DATA_Z]lowX = x * FILTERING_VALUE + lowX * (1.0f - FILTERING_VALUE)lowY = y * FILTERING_VALUE + lowY * (1.0f - FILTERING_VALUE)lowZ = z * FILTERING_VALUE + lowZ * (1.0f - FILTERING_VALUE)val highX = x - lowXval highY = y - lowYval highZ = z - lowZif (highX >= 10 || highY >= 10 || highZ >= 10) {shaking = trueonShakeListener?.onShake()shakeHandler.postDelayed(r, 2000)}}}}override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {//传感器精度改变}/*** 启动摇晃检测--注册监听器。*/fun start() {sensorManager?.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL)}/*** 停止摇晃检测--取消监听器。*/fun stop() {sensorManager?.unregisterListener(this)}/*** 当摇晃事件发生时,接收通知。*/interface OnShakeListener {/*** 当手机晃动时被调用。*/fun onShake()}fun setOnShakeListener(l: OnShakeListener) {this.onShakeListener = l}
}

摇一摇功能的实现原理很简单,就是使用了Android的重力传感器,当x,y,z轴的加速度超过了预先设定的阈值,就会触发摇一摇功能,我们这里是调用MediaManager播放下一首歌。因为MediaManager管理着整个可以播放的音乐列表,所以随时都可以触发摇一摇功能,当然在设置中关掉了摇一摇功能除外。

拔出耳机或断开蓝牙耳机连接暂停播放音乐

package site.doramusic.app.receiverimport android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHeadset
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Handlerimport site.doramusic.app.MusicApp
import site.doramusic.app.R
import site.doramusic.app.media.SimpleAudioPlayer/*** 耳机拨出监听。*/
class EarphoneReceiver : BroadcastReceiver() {private lateinit var player: SimpleAudioPlayeroverride fun onReceive(context: Context, intent: Intent) {val action = intent.actionif (action == AudioManager.ACTION_AUDIO_BECOMING_NOISY) {changeSpeakerphoneOn(context, true)// 只监听拔出耳机使用这个意图// 耳机拔出时,暂停音乐播放Handler().postDelayed({player = SimpleAudioPlayer(context)player.playByRawId(R.raw.earphone)}, 1000)pauseMusic()} else if (Intent.ACTION_HEADSET_PLUG == action) {//            if (intent.hasExtra("state")) {//                int state = intent.getIntExtra("state", -1);//                if (state == 1) {//                    //插入耳机//                } else if (state == 0) {//                    //拔出耳机//                    pauseMusic();//                }//            }} else if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED == action) {val adapter = BluetoothAdapter.getDefaultAdapter()if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.A2DP) ||BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET) ||BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEALTH) ||BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.GATT)) {changeSpeakerphoneOn(context, true)//蓝牙耳机失去连接Handler().postDelayed({player = SimpleAudioPlayer(context)player.playByRawId(R.raw.bluetooth)}, 1000)pauseMusic()} else if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET) ||BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET) ||BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEALTH) ||BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.GATT)) {//蓝牙耳机已连接}}}private fun pauseMusic() {MusicApp.instance!!.mediaManager!!.pause()}/*** 切换播放模式。** @param connected*/private fun changeSpeakerphoneOn(context: Context, connected: Boolean) {val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManageram.isSpeakerphoneOn = connected}
}

我们通过监听系统广播来实现这样的功能。

MusicTimer全局音乐播放界面刷新

package site.doramusic.app.util;import android.os.Handler;
import android.os.Message;import java.util.Timer;
import java.util.TimerTask;public class MusicTimer {public final static int REFRESH_PROGRESS_EVENT = 0x100;private static final int INTERVAL_TIME = 500;private Handler[] mHandler;private Timer mTimer;private TimerTask mTimerTask;private int what;private boolean mTimerStart = false;public MusicTimer(Handler... handler) {this.mHandler = handler;this.what = REFRESH_PROGRESS_EVENT;mTimer = new Timer();}public void startTimer() {if (mHandler == null || mTimerStart) {return;}mTimerTask = new MusicTimerTask();mTimer.schedule(mTimerTask, INTERVAL_TIME, INTERVAL_TIME);mTimerStart = true;}public void stopTimer() {if (!mTimerStart) {return;}mTimerStart = false;if (mTimerTask != null) {mTimerTask.cancel();mTimerTask = null;}}class MusicTimerTask extends TimerTask {@Overridepublic void run() {if (mHandler != null) {for (Handler handler : mHandler) {Message msg = handler.obtainMessage(what);msg.sendToTarget();}}}}
}

我们所有需要刷新进度条的地方都要用到这个类,一般设置为0.5秒刷新一次,既不过度刷新,又要保证歌曲时间播放进度较准确。

BaseActivity写法体验

package site.doramusic.app.ui.activityimport android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import dora.skin.SkinManager
import dora.skin.base.BaseSkinActivity
import dora.util.DensityUtils
import dora.util.StatusBarUtils
import dora.widget.DoraTitleBar
import site.doramusic.app.R
import site.doramusic.app.annotation.TimeTrace
import site.doramusic.app.base.conf.ARoutePath
import site.doramusic.app.databinding.ActivityChoiceColorBinding
import site.doramusic.app.ui.adapter.ChoiceColorAdapter
import site.doramusic.app.util.PreferencesManager/*** 换肤界面,选择颜色。*/
@Route(path = ARoutePath.ACTIVITY_CHOICE_COLOR)
class ChoiceColorActivity : BaseSkinActivity<ActivityChoiceColorBinding>() {private lateinit var colorDrawable: ColorDrawableprivate var choiceColorAdapter: ChoiceColorAdapter? = nullprivate var colorDatas: MutableList<ColorData>? = nullprivate lateinit var prefsManager: PreferencesManagerdata class ColorData(val backgroundResId: Int, val backgroundColor: Int)override fun getLayoutId(): Int {return R.layout.activity_choice_color}override fun onSetStatusBar() {super.onSetStatusBar()StatusBarUtils.setTransparencyStatusBar(this)}override fun initData(savedInstanceState: Bundle?) {mBinding.statusbarChoiceColor.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,StatusBarUtils.getStatusBarHeight())SkinManager.getLoader().setBackgroundColor(mBinding.statusbarChoiceColor, "skin_theme_color")val imageView = AppCompatImageView(this)val dp24 = DensityUtils.dp2px(24f)imageView.layoutParams = RelativeLayout.LayoutParams(dp24, dp24)imageView.setImageResource(R.drawable.ic_save)mBinding.titlebarChoiceColor.addMenuButton(imageView)mBinding.titlebarChoiceColor.setOnIconClickListener(object : DoraTitleBar.OnIconClickListener {override fun onIconBackClick(icon: AppCompatImageView) {}override fun onIconMenuClick(position: Int, icon: AppCompatImageView) {if (position == 0) {changeSkin()}}})prefsManager = PreferencesManager(this)colorDatas = mutableListOf(ColorData(R.drawable.cyan_bg,resources.getColor(R.color.skin_theme_color_cyan)),ColorData(R.drawable.orange_bg,resources.getColor(R.color.skin_theme_color_orange)),ColorData(R.drawable.black_bg,resources.getColor(R.color.skin_theme_color_black)),ColorData(R.drawable.green_bg,resources.getColor(R.color.skin_theme_color_green)),ColorData(R.drawable.red_bg,resources.getColor(R.color.skin_theme_color_red)),ColorData(R.drawable.blue_bg,resources.getColor(R.color.skin_theme_color_blue)),ColorData(R.drawable.purple_bg,resources.getColor(R.color.skin_theme_color_purple)))choiceColorAdapter = ChoiceColorAdapter()choiceColorAdapter!!.setList(colorDatas!!)mBinding.rvChoiceColor.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false)
//        mBinding.rvChoiceColor.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL))mBinding.rvChoiceColor.itemAnimator = DefaultItemAnimator()mBinding.rvChoiceColor.adapter = choiceColorAdapterchoiceColorAdapter!!.selectedPosition = if (prefsManager.getSkinType() == 0) 0 else prefsManager.getSkinType() - 1colorDrawable = ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimary))mBinding.ivChoiceColorPreview.background = colorDrawablechoiceColorAdapter!!.setOnItemClickListener { adapter, view, position ->val color = colorDatas!![position].backgroundColorcolorDrawable.color = colorchoiceColorAdapter!!.selectedPosition = positionchoiceColorAdapter!!.notifyDataSetChanged()}}/*** 测试AOP。*/@TimeTraceprivate fun changeSkin() {when (choiceColorAdapter!!.selectedPosition) {0 -> {prefsManager.saveSkinType(1)SkinManager.changeSkin("cyan")}1 -> {prefsManager.saveSkinType(2)SkinManager.changeSkin("orange")}2 -> {prefsManager.saveSkinType(3)SkinManager.changeSkin("black")}3 -> {prefsManager.saveSkinType(4)SkinManager.changeSkin("green")}4 -> {prefsManager.saveSkinType(5)SkinManager.changeSkin("red")}5 -> {prefsManager.saveSkinType(6)SkinManager.changeSkin("blue")}6 -> {prefsManager.saveSkinType(7)SkinManager.changeSkin("purple")}}SkinManager.getLoader().setBackgroundColor(mBinding.statusbarChoiceColor, "skin_theme_color")finish()}
}

以换肤界面为例,另外换肤可以看我这篇文章我的又一个神奇的框架——Skins换肤框架 ,我这里就不多说了。使用dora.BaseActivity和dora.BaseFragment,可以统一数据加载都在initData中。

开源项目地址

https://github.com/dora4/DoraMusic

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/18152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity【入门】环境搭建、界面基础、工作原理

Unity环境搭建、界面基础、工作原理 Unity环境搭建 文章目录 Unity环境搭建1、Unity引擎概念1、什么是游戏引擎2、游戏引擎对于我们的意义3、如何学习游戏引擎 2、软件下载和安装3、新工程和工程文件夹 Unity界面基础1、Scene场景和Hierarchy层级窗口1、窗口布局2、Hierarchy层…

多分支拓扑阻抗匹配

最近测试信号质量&#xff0c;发现在有过冲、振铃等问题的时候大部分硬件工程师喜欢直接调大匹配电阻或者减小驱动电流&#xff0c;虽然这种操作是有效果的&#xff0c;但是我认为应该还可以更严谨的计算下&#xff0c;而不是选几个电阻多次尝试&#xff0c;显得不是很专业。 …

推送镜像到私有harbor仓库

本地已制作镜像&#xff1a;tomcat-8.5.100-centos7.9:1.0。 本地已经搭建私有仓库&#xff1a;harbor.igmwx.com。 现在需要把镜像 tomcat-8.5.100-centos7.9:1.0 推送到harbor。 &#xff08;1&#xff09;查看本地镜像&#xff1a;sudo docker images zhangzkzhangzk:~/d…

java 对接农行支付相关业务(二)

文章目录 农行掌银集成第三方APP1:掌银支付对接快e通的流程1.1 在农行网站上注册我们的app信息([网址](https://openbank.abchina.com/Portal/index/index.html))1.2:java整合农行的jar包依赖1.3:把相关配置信息整合到项目中1.4:前端获取授权码信息1.5:后端根据授权码信…

【动态规划】速解简单多状态类问题

目录 17.16 按摩师 题⽬描述&#xff1a; 解法&#xff08;动态规划&#xff09;&#xff1a; 1. 状态表⽰&#xff1a; 2. 状态转移⽅程&#xff1a; 3. 初始化&#xff1a; 4. 填表顺序 5. 返回值 代码 总结&#xff1a; 213.打家劫舍II&#xff08;medium&#x…

C++ 基于vs2019创建并使用动态链接库(dll)

库的基本认识 静态库&#xff08;Static Library&#xff09; 基本概念&#xff1a;静态库是在编译时链接到目标程序中的库文件。它包含了程序运行所需的所有函数和数据&#xff0c;这些函数和数据会被直接嵌入到最终生成的可执行文件中。静态库通常以.a&#xff08;在Unix-l…

分频器对相位噪声影响

本文我们将分析输入时钟被N分频之后的输出时钟的相位噪声如何变化。首先理想分频器的意思是我们假设分频器不会引入附加相位噪声&#xff0c;并且输入和输出时钟之间没有延时。我们假设每一个输出边沿的位置都完美的与输入边沿相对齐&#xff0c;这样便于分析。由于每N个输入时…

[FlareOn6]Overlong

很简单的逻辑 一度让我以为是加保护了 运行告诉我从未编码,懵逼 动调你也发现,你根本没什么可以操作的空间,密文什么的,都是固定的 但是这里大家发现没 我们只加密了28个密文 然后text是128 也就是 0x80 是不是因为密文没加密完呢 我也懒得去写代码了 汇编直接修改push 字…

axios和ts的简单使用

按照官网的使用案例简单记下笔记 1&#xff1a;安装 npm install axios 2&#xff1a;案例 一个简单的config配置信息 // 发起一个post请求 axios({method: post,url: /user/12345,data: {firstName: Fred,lastName: Flintstone} }); case // 在 node.js 用GET请求获取…

总结常见漏洞的代码审计方法

前言 这篇文章主要是总结一下在安全工作中常见漏洞的代码审计方法&#xff0c;以及修复方案&#xff0c;希望能对初学代码审计小伙伴们有所帮助&#xff0c;笔芯♥️ 代码审计的思路 通常做代码审计都是检查敏感函数的参数&#xff0c;然后回溯变量&#xff0c;判断变量是否可…

【Crypto】RSA

文章目录 题目步骤1.计算 &#x1d45b;2.计算欧拉函数 &#x1d719;(&#x1d45b;)3. 扩展欧几里得算法求逆元 &#x1d451; 解题感悟 题目 p473398607161 q4511491 e17 求d 步骤 1.计算 &#x1d45b; 公式&#xff1a;npq n47339860716145114912135733555619387051 …

初识STM32单片机-TIM定时器

初识STM32单片机-TIM定时器 一、定时器概述二、定时器类型2.1 基本定时器(TIM6和TIM7)2.2 通用定时器(TIM2、TIM3、TIM4和TIM5)2.3 高级定时器(TIM1和TIM8) 三、定时中断基本结构和时基单元工作时序3.1 定时器基本结构3.2 预分频器时序3.3 计数器时序3.3.1 计数器有无预装时序(…

Hono 框架使用经验谈

Hono&#x1f525;是一个小型、快速并开源的 Serverless Web 框架&#xff0c;用 TypeScript 写就。它适用于任何JavaScript运行时&#xff1a;Cloudflare Workers&#xff0c;Fastly ComputeEdge&#xff0c;Deno&#xff0c;Bun&#xff0c;Vercel&#xff0c;Netlify&#x…

动手学操作系统(一、搭建实验环境)

动手学操作系统&#xff08;一、搭建实验环境&#xff09; 文章目录 动手学操作系统&#xff08;一、搭建实验环境&#xff09;1. 在VMware虚拟机中安装ubuntu20.042. 安装Bochs3. 启动计算机Reference &#x1f680; 环境配置 &#x1f680; 笔者的环境使用的是 ubuntu 20.04…

python多线程的使用

Python 多线程探索 在 Python 编程中&#xff0c;多线程是一项强大的技术&#xff0c;它允许我们同时执行多个任务&#xff0c;从而提高程序的效率和响应性。 一、线程的基本概念 线程是进程中的一个执行单元&#xff0c;可以与其他线程共享进程的资源。 二、创建线程 在 P…

Linux:confluence8.5.9的部署(下载+安装+破ji)离线部署全流程

0.环境 Confluence也是比较吃运存了&#xff0c;我建议运行运存给到4g或者4g以上就可以了&#xff0c;核数可以给到1核或以上 我部署在centos7.9操作系统上&#xff0c;ip地址为&#xff1a;192.168.6.1&#xff0c;yum仓库使用的是自己的镜像文件&#xff0c;本章所有使用到…

爱普生XC1011SD为电气稳定控制系统组合式传感器推出解决方案

在现代电力系统中&#xff0c;确保系统的安全和稳定运行至关重要。为了应对这一需求&#xff0c;我们推出了XC1011SD组合式传感器解决方案&#xff0c;它专为电气稳定控制系统设计&#xff0c;提供了卓越的性能和可靠性。 XC1011SD是一款集成陀螺仪传感器和双轴加速度计于一体的…

炸裂!AI五分钟模仿爆款IP故事,涨粉速度太绝了!

‍ ‍大家好&#xff0c;我是向阳。 今天我要分享一个利用AI技术模仿爆款账号的小技巧&#xff0c;帮助大家迅速增加粉丝。这个方法简单实用&#xff0c;尤其适用于副业和本地生活领域。接下来&#xff0c;我将为大家详细讲解操作步骤。让我们开始吧。 副业赚钱&#xff1a;模…

合约构成-成员变量、函数、事件event、修饰器modifier及构造函数

合约的基本结构 合约中的成员变量合约中的成员函数Event&#xff08;事件&#xff09;、modifier(修饰器)与constructor&#xff08;构造函数:实例产生的时候执行&#xff09; Event事件 modifier construcor 1、成员变量 概念&#xff1a;存储合约状态的变量 声明方法&a…

北京证券公司港股通交易佣金手续费最低是多少?万0.8?港股通纳入规则是怎么样的?

港股通交易佣金概述 港股通的交易佣金可能会因证券公司和投资者的不同而有所差异。 北京证券公司的港股通交易佣金最低可能万分之零点八&#xff08;0.008%&#xff09;&#xff0c;但这需要投资者与证券公司客户经理了解&#xff0c;进行沟通和申请。 一般来说&#xff0c;…