gpt4 book ai didi

java - 如何在 Android 中从数组中提取字符串?

转载 作者:行者123 更新时间:2023-12-01 11:47:19 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,它是一个流媒体广播。要更改广播电台,有一个下拉菜单(基于微调器),允许用户选择广播电台,然后加载它。

问题:当我将其保留为默认值时,第一个 channel 播放正常......当我尝试选择另一个电台时,应用程序崩溃了。我正在使用新的 Android Studio。

我认为我没有从数组中获取 URL 的字符串。有人可以帮我看看我的错误是什么吗?

这是我的代码:

import android.annotation.TargetApi;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class AndroidMediaPlayerExample extends Activity {

private MediaPlayer mediaPlayer;
public TextView songName, duration;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
private SeekBar seekbar;
//String url = "http://radio.miraath.net:7000";
//String url = new String[]{"http://radio.miraath.net:7000", "http://radio.miraath.net:7010", "http://radio.miraath.net:7020", "http://radio.miraath.net:7030","http://radio.miraath.net:7040", };
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//set the layout of the Activity
setContentView(R.layout.activity_main);
Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
String[] items = new String[]{"Radio 1: Arabic", "Radio 2: Live Arabic", "Radio 3: Qur'an", "Radio 4: English Radio", "Radio 5: Arabic Radio 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
dropdown.setAdapter(adapter);

//initialize views
initializeViews();
}

public void initializeViews(){
songName = (TextView) findViewById(R.id.songName);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_song);
finalTime = mediaPlayer.getDuration();
duration = (TextView) findViewById(R.id.songDuration);
seekbar = (SeekBar) findViewById(R.id.seekBar);
songName.setText("Radio1 - Miraath.net");

seekbar.setMax((int) finalTime);
seekbar.setClickable(false);
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mediaPlayer.release();

switch (position) {

case 0:
// Whatever you want to happen when the first item gets selected
url = "http://radio.miraath.net:7000";
break;
case 1:
// Whatever you want to happen when the second item gets selected
url = "http://radio.miraath.net:7010";
break;
case 2:
// Whatever you want to happen when the thrid item gets selected
url = "http://radio.miraath.net:7020";
break;
case 3:
// Whatever you want to happen when the thrid item gets selected
url = "http://radio.miraath.net:7030";
break;
case 4:
// Whatever you want to happen when the thrid item gets selected
url = "http://radio.miraath.net:7040";
break;


}
}

// play mp3 song
public void play(View view) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}

mediaPlayer.start();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
}

//handler to change seekBarTime
private Runnable updateSeekBarTime = new Runnable() {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void run() {
//get current position
timeElapsed = mediaPlayer.getCurrentPosition();
//set seekbar progress
seekbar.setProgress((int) timeElapsed);
//set time remaing
double timeRemaining = finalTime - timeElapsed;
duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

//repeat yourself that again in 100 miliseconds
durationHandler.postDelayed(this, 100);
}
};

// pause mp3 song
public void pause(View view) {
mediaPlayer.pause();
}

// go forward at forwardTime seconds
public void forward(View view) {
//check if we can go forward at forwardTime seconds before song endes
if ((timeElapsed + forwardTime) <= finalTime) {
timeElapsed = timeElapsed + forwardTime;

//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}

// go backwards at backwardTime seconds
public void rewind(View view) {
//check if we can go back at backwardTime seconds after song starts
if ((timeElapsed - backwardTime) > 0) {
timeElapsed = timeElapsed - backwardTime;

//seek to the exact second of the track
mediaPlayer.seekTo((int) timeElapsed);
}
}

}

更新!!!

这是日志中的错误:

03-14 19:20:10.413    1191-1284/system_process W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
03-14 19:20:10.422 1191-1416/system_process I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.javacodegeeks.androidmediaplayerexample/.AndroidMediaPlayerExample (has extras)} from uid 10008 on display 0
03-14 19:20:10.457 4043-4043/? E/libprocessgroup﹕ failed to make and chown /acct/uid_10065: Read-only file system
03-14 19:20:10.457 4043-4043/? W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
03-14 19:20:10.457 4043-4043/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
03-14 19:20:10.461 1191-3310/system_process I/ActivityManager﹕ Start proc com.javacodegeeks.androidmediaplayerexample for activity com.javacodegeeks.androidmediaplayerexample/.AndroidMediaPlayerExample: pid=4043 uid=10065 gids={50065, 9997, 3003} abi=x86
03-14 19:20:10.493 4043-4050/? E/art﹕ Failed sending reply to debugger: Broken pipe
03-14 19:20:10.493 4043-4050/? I/art﹕ Debugger is no longer active
03-14 19:20:10.515 4043-4043/? D/AndroidRuntime﹕ Shutting down VM
03-14 19:20:10.515 4043-4043/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.javacodegeeks.androidmediaplayerexample, PID: 4043
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javacodegeeks.androidmediaplayerexample/com.javacodegeeks.androidmediaplayerexample.AndroidMediaPlayerExample}: java.lang.ClassCastException: com.javacodegeeks.androidmediaplayerexample.AndroidMediaPlayerExample cannot be cast to android.widget.AdapterView$OnItemSelectedListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassCastException: com.javacodegeeks.androidmediaplayerexample.AndroidMediaPlayerExample cannot be cast to android.widget.AdapterView$OnItemSelectedListener
at com.javacodegeeks.androidmediaplayerexample.AndroidMediaPlayerExample.onCreate(AndroidMediaPlayerExample.java:42)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

谢谢!!

最佳答案

您尚未在微调器上添加监听器。设置适配器后按此方式执行

dropdown.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

您的 Activity 还必须实现OnItemSelectedListener

public class AndroidMediaPlayerExample extends Activity  implements OnItemSelectedListener 

关于java - 如何在 Android 中从数组中提取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056070/

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