我正在使用开源Android音乐播放器应用我正在使用我从教程中找到的一些代码,但它没有Shuffle,Repeat& amp;包含随机按钮,因此我需要帮助才能将其添加到我的代码中。我不知道如何将它添加到我的应用程序中,所以我正在寻求帮助。在Android编码方面,我是一个完整的菜鸟,我正在努力学习。
这是我的代码:
/**
* Created by Technologx
* Visit & signup on https://technologx.com
* Do not remove this removing this avoids the license
*/
package com.technologx.blaze.player;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final String Broadcast_PLAY_NEW_AUDIO = "com.technologx.blaze.player.PlayNewAudio";
private MediaPlayerService player;
boolean serviceBound = false;
ArrayList<Audio> audioList;
ImageView collapsingImageView;
int imageIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
collapsingImageView = (ImageView) findViewById(R.id.collapsingImageView);
loadCollapsingImage(imageIndex);
loadAudio();
initRecyclerView();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// playAudio("https://upload.wikimedia.org/wikipedia/commons/6/6c/Grieg_Lyric_Pieces_Kobold.ogg");
//play the first audio in the ArrayList
// playAudio(2);
if (imageIndex == 4) {
imageIndex = 0;
loadCollapsingImage(imageIndex);
} else {
loadCollapsingImage(++imageIndex);
}
}
});
}
private void initRecyclerView() {
if (audioList.size() > 0) {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
RecyclerView_Adapter adapter = new RecyclerView_Adapter(audioList, getApplication());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addOnItemTouchListener(new CustomTouchListener(this, new onItemClickListener() {
@Override
public void onClick(View view, int index) {
playAudio(index);
}
}));
}
}
private void loadCollapsingImage(int i) {
TypedArray array = getResources().obtainTypedArray(R.array.images);
collapsingImageView.setImageDrawable(array.getDrawable(i));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putBoolean("serviceStatus", serviceBound);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
serviceBound = savedInstanceState.getBoolean("serviceStatus");
}
//Binding this Client to the AudioPlayer Service
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
player = binder.getService();
serviceBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
serviceBound = false;
}
};
private void playAudio(int audioIndex) {
//Check is service is active
if (!serviceBound) {
//Store Serializable audioList to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(audioList);
storage.storeAudioIndex(audioIndex);
Intent playerIntent = new Intent(this, MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Store the new audioIndex to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudioIndex(audioIndex);
//Service is active
//Send a broadcast to the service -> PLAY_NEW_AUDIO
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
}
}
private void loadAudio() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
audioList = new ArrayList<>();
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
// Save to audioList
audioList.add(new Audio(data, title, album, artist));
}
}
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (serviceBound) {
unbindService(serviceConnection);
//service is active
player.stopSelf();
}
}
}
这里是MusicService.java代码: https://ghostbin.com/paste/x4fyf
我想用实际的随机播放按钮替换图像随机播放按钮。我想添加重复&amp;随机按钮到菜单。也不在问题标题中,但我如何用现在正在播放的艺术作品替换代码中包含的图像?
以下是我的项目的源代码:https://github.com/Technologx/Blaze-Player
答案 0 :(得分:0)
你应该随机播放关于shuffle方法的列表并从洗牌列表中获取歌曲的url。同样也适用于其他人。
答案 1 :(得分:0)
尝试使用它:
int position = (Math.Random() * mySongsArray.size());
play(position);