gpt4 book ai didi

java - Android 音乐播放器无法从 onCreate 自动启动

转载 作者:行者123 更新时间:2023-12-02 13:01:41 25 4
gpt4 key购买 nike

您好,我有音乐服务的实现代码,我想创建音乐播放器的演示,但它不会从 onCreate() 自动启动,我想在 Activity 打开时自动启动播放器。下面我放置了 Activity 和服务的代码,请帮助我,任何帮助将不胜感激。

public class MainActivity extends Activity {
private ArrayList<Song> songList;
private ListView songView;
private MusicService musicSrv;
private Intent playIntent;
private boolean musicBound = false;
private MusicController controller;
private boolean paused = false, playbackPaused = false;
private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicSrv = binder.getService();
musicSrv.setList(songList);
musicBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songView = (ListView) findViewById(R.id.song_list);
songList = new ArrayList<Song>();
getSongList();
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
songPicked();
}
@Override
protected void onStart() {
super.onStart();
if (playIntent == null) {
playIntent = new Intent(this, MusicService.class);
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
startService(playIntent);
}
}
public void songPicked() {
musicSrv.setSong(0);
musicSrv.playSong();
}

@Override
protected void onPause() {
super.onPause();
paused = true;
}

@Override
protected void onResume() {
super.onResume();
if (paused) {
paused = false;
}
}

@Override
protected void onStop() {
controller.hide();
super.onStop();
}

@Override
protected void onDestroy() {
stopService(playIntent);
musicSrv = null;
super.onDestroy();
}
}

下面我还放置了服务代码。

public class MusicService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {

private final IBinder musicBind = new MusicBinder();
private MediaPlayer player;
private ArrayList<Song> songs;
private int songPosn;
private String songTitle = "";
private Random rand;

public void onCreate() {
super.onCreate();
songPosn = 0;
rand = new Random();
player = new MediaPlayer();
initMusicPlayer();
}

public void initMusicPlayer() {
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}

public void setList(ArrayList<Song> theSongs) {
songs = theSongs;
}

@Override
public IBinder onBind(Intent intent) {
return musicBind;
}

@Override
public boolean onUnbind(Intent intent) {
player.stop();
player.release();
return false;
}

public void playSong() {
player.reset();
Song playSong = songs.get(0);
songTitle = playSong.getTitle();
long currSong = playSong.getID();
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
try {
player.setDataSource(getApplicationContext(), trackUri);
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}

public void setSong(int songIndex) {
songPosn = songIndex;
}

@Override
public void onCompletion(MediaPlayer mp) {
if (player.getCurrentPosition() > 0) {
mp.reset();
}
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.reset();
return false;
}

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

@Override
public void onDestroy() {
stopForeground(true);
}

public class MusicBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
}

我只想在应用程序自动启动时启动播放器,无需任何点击。但不幸的是,它显示因媒体播放器的空对象引用而停止。我从未使用过服务和音乐播放器。

由于音乐服务对象返回 null,因此引发空指针异常。

最佳答案

终于按照@vladMatvienko的回答解决了,感谢您的支持。

private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicSrv = binder.getService();
musicSrv.setList(songList);
musicBound = true;
songPicked();
}
@Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};

关于java - Android 音乐播放器无法从 onCreate 自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276131/

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