gpt4 book ai didi

android - 应用程序在 mp.setDataSource(this, path) 处崩溃;

转载 作者:行者123 更新时间:2023-12-02 08:49:15 24 4
gpt4 key购买 nike

我是 Android 开发新手。我正在尝试根据位置选择歌曲时播放歌曲。它总是崩溃并且有多个错误。我真的不明白我做错了什么。可能是愚蠢的错误,请帮助。

public class BackgroundAudioService extends Service implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener{

MediaPlayer mp;
int mainpositionno;
int itempositionno;
int i;
Uri path;
Intent playbackServiceIntent;

BackgroundAudioService instanceService;
public BackgroundAudioService() {
}

@Override
public void onPrepared(MediaPlayer mp) {
mp = new MediaPlayer();
mp.start();
}

class LocalBinder extends Binder{
public BackgroundAudioService getService(){
return instanceService;
}
}

@Override
public IBinder onBind(Intent intent) {
int mainpositionno = intent.getIntExtra("Mainpositionno", 0);
int itempositionno = intent.getIntExtra("Itempositionno", 0);
this.mainpositionno = mainpositionno;
this.itempositionno = itempositionno;
setSongAtPosition(mainpositionno,itempositionno);
return new LocalBinder();
}

public void setSongAtPosition(int mainposition, int position){
mainpositionno = mainposition;
itempositionno = position;
//String filePath = "somepath/somefile.mp3";
Uri path = null;
Toast.makeText(BackgroundAudioService.this,"main "+mainposition+ " pos"+position,Toast.LENGTH_SHORT).show();

if (mainpositionno == 0){
switch (itempositionno){
case 0:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.namah_shivaya_chanting);
break;
}
case 1:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.om_namasivaya);
break;
}
case 2:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sivamayamana);
break;
}
}
}else
if (mainpositionno == 1) {
switch (itempositionno){
case 0:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.om_namo_narayanaya);
break;
}
case 1:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.suprabhatham);
break;
}
}
}else
if (mainpositionno == 2) {
switch (itempositionno) {
case 0: {
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.saranamiyyapa_chanting);
break;
}
case 1: {
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.harivarasanam);
break;
}
case 2: {
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pallikattu_sabarimalaiku);
break;
}
}
}else
if (mainpositionno == 3) {
switch (itempositionno){
case 0:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kandha_guru_kavasam);
break;
}
case 1:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kandha_sasti_kavasam);
break;
}
}
}else
if (mainpositionno == 4) {
//To set
}
if(mp.isPlaying()) mp.stop(); mp.reset();
mp = MediaPlayer.create(BackgroundAudioService.this, path);
mp.setOnCompletionListener(this);
try {
mp.setDataSource(this, path);
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(this);
mp.prepareAsync();
}

@Override
public void onCreate() {
instanceService = this;
mp = new MediaPlayer();
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public void onCompletion(MediaPlayer mp) {
}

}

08-17 10:12:11.767    6290-6290/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 6290
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference

08-17 10:13:15.827 7386-7386/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 7386
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference

08-17 10:13:23.644 7797-7797/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 7797
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference

08-17 12:54:09.221 5223-5223/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 5223
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference

08-17 12:54:21.419 5366-5366/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 5366
java.lang.RuntimeException: Unable to bind to service com.fishy.meena.hindudevotionalsongs.BackgroundAudioService@25561ca2 with Intent { cmp=com.fishy.meena.hindudevotionalsongs/.BackgroundAudioService (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference

08-18 08:41:40.568 31629-31629/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 31629
java.lang.RuntimeException: Unable to bind to service com.fishy.meena.hindudevotionalsongs.BackgroundAudioService@3ff5f51d with Intent { cmp=com.fishy.meena.hindudevotionalsongs/.BackgroundAudioService (has extras) }: java.lang.IllegalStateException

08-18 08:42:16.058 32172-32172/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fishy.meena.hindudevotionalsongs, PID: 32172
java.lang.RuntimeException: Unable to bind to service com.fishy.meena.hindudevotionalsongs.BackgroundAudioService@1dd66392 with Intent { cmp=com.fishy.meena.hindudevotionalsongs/.BackgroundAudioService (has extras) }: java.lang.IllegalStateException

最佳答案

为了确定,只需检查:

<强>1。 mp 对象不为空

<强>2。 URI 有效

关于android - 应用程序在 mp.setDataSource(this, path) 处崩溃;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com