我正在尝试将音频文件添加到seekbar,该文件将由seekbar控制,而不向我的应用程序添加任何按钮。音频文件将附加到搜寻栏,并且随着搜寻栏的进度,音量会增加或减小。下面是我的代码,我的应用程序一次又一次崩溃,我不知道为什么。在这方面请帮助我。
class MainActivity : AppCompatActivity(){
private var mediaPlayer : MediaPlayer? = null
private var runnable : Runnable? = null
private var handler : Handler? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val start = findViewById<Button>(R.id.start)
val seekbar = findViewById<SeekBar>(R.id.seekBar)
mediaPlayer = MediaPlayer.create(this,R.raw.lowest_frequency)
mediaPlayer!!.setOnPreparedListener { mediaPlayer ->
seekbar.max = mediaPlayer.duration
mediaPlayer.start()
changeSeekBar()
}
seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onStartTrackingTouch(p0: SeekBar?) {
}
override fun onStopTrackingTouch(p0: SeekBar?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
if (p2){
mediaPlayer!!.seekTo(p1)
}
}
})
start.setOnClickListener{
startActivity(Intent(this,StartActivity::class.java))
}
}
private fun changeSeekBar() {
seekBar.progress = mediaPlayer!!.currentPosition
if (mediaPlayer!!.isPlaying){
runnable = Runnable{
changeSeekBar()
}
handler!!.postDelayed(runnable,50)
handler!!.postDelayed(runnable,0)
}
}
}
以下是我的logcat错误
09-24 10:49:58.508 15907-15929/? E/libmtp: [mtp_init_device_info : 0586] -->[mtp_init_device_info 586]Error opening file /data/misc/radio/ahrh,(fd<0) IMEI [
000000000000000
]
09-24 10:49:58.543 15907-15931/? E/SQLiteLog: (1) Process .MtpApplication : Pid (15907) Uid (1000) Euid (1000) Gid (1000) Egid (1000)
(1) osStat failed "/data/data/com.android.providers.settings/databases/settings.db" due to error (2)
(1) osStat failed "/data/data/com.android.providers.settings/databases" due to error (2)
(1) Stat of /data/data/com.android.providers.settings : st_mode(40700) st_uid(1000) st_gid(1000) st_ino(65726)
(1) Stat of /data/data : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(12)
(1) Stat of /data : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(2)
(21) API call with unopened database connection pointer
(21) misuse at line 119179 of [605907e73a]
09-24 10:51:30.576 6373-6373/? E/BufferItemConsumer: [unnamed-6373-935] Failed to release buffer: Unknown error -1 (1)
09-24 10:51:30.707 6373-6373/? E/BufferItemConsumer: [unnamed-6373-936] Failed to release buffer: Unknown error -1 (1)
09-24 10:51:34.498 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3L.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3T.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num3T:null
Error mapping font file /system/fonts/SamsungSans-Num4L.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num4L:null
Error mapping font file /system/fonts/SamsungSans-Num4T.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num4T:null
Error mapping font file /system/fonts/SamsungSans-Num35.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num45.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num45:null
Error mapping font file /system/fonts/SamsungSans-Num3Lv.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num3LV:null
Error mapping font file /system/fonts/SamsungSans-Num3R.ttf
09-24 10:51:34.501 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num4Tv.ttf
09-24 10:51:34.502 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num4Lv.ttf
09-24 10:51:36.101 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3L.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3T.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3T:null
Error mapping font file /system/fonts/SamsungSans-Num4L.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4L:null
Error mapping font file /system/fonts/SamsungSans-Num4T.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4T:null
Error mapping font file /system/fonts/SamsungSans-Num35.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num45.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num45:null
Error mapping font file /system/fonts/SamsungSans-Num3Lv.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3LV:null
Error mapping font file /system/fonts/SamsungSans-Num3R.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3R:null
Error mapping font file /system/fonts/SamsungSans-Num4Tv.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4Tv:null
Error mapping font file /system/fonts/SamsungSans-Num4Lv.ttf
09-24 10:57:19.587 4405-4422/? E/zygote: Error encountered killing process cgroup uid 99472 pid 15122: No such file or directory
答案 0 :(得分:1)
我认为这会对您有所帮助
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
SeekBar seekBar;
Handler mHandler;
Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler=new Handler();
seekBar=findViewById(R.id.seekBar);
mp=MediaPlayer.create(MainActivity.this,R.raw.ed);
final int Duration=mp.getDuration();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
seekBar.setMax(mp.getDuration());
updateSeekBar();
mp.start();
}
});
runnable=new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
//Seekbar Listener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mp.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
//update method
private void updateSeekBar() {
seekBar.setProgress(mp.getCurrentPosition());
mHandler.postDelayed(runnable, 100);
}
}