[英]Adding Shuffle, Repeat & Random to Android Music Player App
I'm working on a open source Android Music player app I'm using some code I found from a tutorial but it doesn't have Shuffle, Repeat & Random buttons included so I need help on adding it to my code. 我正在使用开放源代码Android Music Player应用程序,正在使用从教程中找到的一些代码,但其中没有随机播放,重复和随机按钮,因此需要帮助将其添加到代码中。 I have no idea how to add it to my app so I'm looking for assistance. 我不知道如何将其添加到我的应用程序,因此我正在寻求帮助。 I am a complete noob when it comes to Android coding and I'm trying to learn. 关于Android编码,我是一个完全菜鸟,而且我正在尝试学习。
Here's my code: 这是我的代码:
/**
* 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();
}
}
}
Here's the MusicService.java code: https://ghostbin.com/paste/x4fyf 这是MusicService.java代码: https ://ghostbin.com/paste/x4fyf
I'd like to replace the image shuffle button with a actual shuffle button. 我想将图像随机播放按钮替换为实际的随机播放按钮。 I want to add the repeat & random buttons to the menu. 我想将重复和随机按钮添加到菜单中。 Also not in the questions title but how would I go about replacing the images included with the app in the code with the now playing artwork? 同样不是在问题标题中,而是如何用正在播放的插图替换代码中应用程序随附的图像?
Here's the source code to my project with screens: https://github.com/Technologx/Blaze-Player 这是带有屏幕的我的项目的源代码: https : //github.com/Technologx/Blaze-Player
you should shuffle the list onClick of shuffle method and get the url of song from shuffled list. 您应该按随机播放方法onClick随机播放列表,并从随机播放的列表中获取歌曲的网址。 same goes for others also. 其他人也一样。
Try to use this: 尝试使用此:
int position = (Math.Random() * mySongsArray.size());
play(position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.