Java 添加洗牌、重复和;随机下载到Android音乐播放器应用程序
我正在开发一个开源的Android音乐播放器应用程序,我正在使用从教程中找到的一些代码,但它没有随机、重复和随机按钮,所以我需要帮助将其添加到我的代码中。我不知道如何将其添加到我的应用程序中,因此我正在寻求帮助。说到Android编码,我是一个彻头彻尾的傻瓜,我正在努力学习 这是我的密码:Java 添加洗牌、重复和;随机下载到Android音乐播放器应用程序,java,android,audio-player,android-music-player,Java,Android,Audio Player,Android Music Player,我正在开发一个开源的Android音乐播放器应用程序,我正在使用从教程中找到的一些代码,但它没有随机、重复和随机按钮,所以我需要帮助将其添加到我的代码中。我不知道如何将其添加到我的应用程序中,因此我正在寻求帮助。说到Android编码,我是一个彻头彻尾的傻瓜,我正在努力学习 这是我的密码: /** * Created by Technologx * Visit & signup on https://technologx.com * Do not remove this remov
/**
* 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();
}
}
}
/**
*由Technologx创建
*访问并注册https://technologx.com
*请勿删除此项删除此项可避免许可证
*/
包com.technologx.blaze.player;
导入android.content.ComponentName;
导入android.content.ContentResolver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.ServiceConnection;
导入android.content.res.TypedArray;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.os.PersistableBundle;
导入android.provider.MediaStore;
导入android.support.design.widget.FloatingActionButton;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ImageView;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
公共静态最终字符串广播\u PLAY\u NEW\u AUDIO=“com.technologx.blaze.player.PlayNewAudio”;
私人媒体播放器;
布尔serviceBound=false;
ArrayList
我想用实际的洗牌按钮替换图像洗牌按钮。我想将重复和随机按钮添加到菜单中。也不在问题标题中,但我如何用正在播放的图片替换代码中应用程序包含的图像
以下是我的屏幕项目的源代码:你应该一次点击shuffle方法就洗牌列表,然后从洗牌列表中获取歌曲的url。其他人也是如此。你应该一次点击shuffle方法就洗牌列表,然后从洗牌列表中获取歌曲的url。其他人也是如此。尝试使用以下方法:
int position = (Math.Random() * mySongsArray.size());
play(position);
尝试使用以下方法:
int position = (Math.Random() * mySongsArray.size());
play(position);
你能详细说明一下吗?你有歌曲列表吗?是的,它会显示歌曲列表,很抱歉,我是一个使用Android编码的noob,所以你必须与我合作。在混乱列表后,从列表中选择第一首歌曲url并将其传递给媒体播放器api。我不知道如何做。你能详细说明一下吗?你有li吗st of songs?是的,它会显示歌曲列表,很抱歉,我是一个Android编码的noob,所以你必须与我合作。在无序移动列表后,从列表中选择第一首歌曲url并将其传递给媒体播放器api。我不知道如何做