我正在尝试从设备的外部存储中删除音频文件(例如在/storage/emulated/0/Music文件夹中)。在分析了MediaStore示例之后,我最终得到了 API 28 及更早版本的以下解决方案:
fun deleteTracks(trackIds: LongArray): Int {
val whereClause = buildWildcardInClause(trackIds.size) // _id IN (?, ?, ?, ...)
return resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, whereClause, Array(trackIds.size) { trackIds[it].toString() })
}
Run Code Online (Sandbox Code Playgroud)
我注意到上面的代码只从 MediaStore 数据库中删除曲目,而不是从设备中删除(重新启动后文件会重新添加到 MediaStore)。因此,我修改了该代码以查询该Media.DATA列,然后使用该信息删除关联的文件。这按预期工作。
但是现在 Android Q 引入了 Scoped Storage,Media.DATA(和Albums.ALBUM_ART)现在已被弃用,因为应用程序可能无法访问这些文件。ContentResolver.openFileDescriptor只能用于读取文件,不能用于删除文件。
那么从 Android Q 开始,推荐的删除曲目的方法是什么?该样品不表明如何从删除多个文件MediaStore,并MediaStore.Audio.Media似乎的工作方式不同MediaStore.Images.Media。
我正在开发一个 Android 音乐播放器应用程序。我想将它与 Google Assistant 集成,以通过语音控制播放和搜索歌曲。我已经根据官方文档的说明在我的应用程序中实现了媒体会话。
当请求助手使用我的应用程序播放某些内容时,我必须说“在MyApp上播放某些艺术家”之类的内容。当我说“播放某个艺术家”时,助手告诉我我没有配置默认音乐提供商。
这会将我带到 Google Assistant 应用程序中的以下屏幕:
我注意到“您的音乐服务”中未列出MyApp 。我的问题如下:我必须在我的应用程序中进行哪些更改才能将其选为默认音乐服务?
供您参考,这是清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="fr.nihilus.music">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature
android:name="android.hardware.audio.output"
android:required="true" />
<application
android:name=".NihilusMusicApplication"
android:allowBackup="true"
android:fullBackupContent="@xml/backup"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<!-- Main activity for music browsing. -->
<activity
android:name=".HomeActivity"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar">
<!-- Make this Activity visible in launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Make this …Run Code Online (Sandbox Code Playgroud) 我有一个用例,我需要连接和断开作为服务的类。只有在服务连接时才能对服务执行操作。当服务连接或断开时通过回调通知客户端:
class Service {
constructor(callback: ConnectionCallback) { ... }
fun connect() {
// Call callback.onConnected() some time after this method returns.
}
fun disconnect() {
// Call callback.onConnectionSuspended() some time after this method returns.
}
fun isConnected(): Boolean { ... }
fun performAction(actionName: String, callback: ActionCallback) {
// Perform a given action on the service, failing with a fatal exception if called when the service is not connected.
}
interface ConnectionCallback {
fun onConnected() // May be called multiple times …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在build.gradle我的Android应用程序项目的文件中配置Kotlin编译器参数的方法.
我在Kotlin官方文档中看到,可以为每个构建版本配置编译器参数(例如debug,release).
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-rc1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.myapp.myapplication"
minSdkVersion 16 …Run Code Online (Sandbox Code Playgroud)