SVP ! Spinner onItemSelected ==> MediaPlayer
Bonjour à tous et toutes ! ="D
Cela fait maintenant 4 jours que je suis bloquer avec ce Mystère ! ="(
Je recherche la méthode pour lier les différents choix du Spinner au MediaPlayer tout simplement.
SVP évitez les subtilités de code ou les demi réponse car je suis un pure novice et je ne risque pas de trouver par magie x"D. J'ai littéralement chercher PARTOUT ! comme un acharné : Tutos, Forums et autres.
Aujourd'hui j'ai eu l'idée de jeter un coup d'oeil sur GitHub afin de chercher des codes source (open source) de Lecteur Audio qui utilisent un Spinner tout comme mon App afin de trouver cette méthode mais à chaque fois c'est
==> MediaPlayer + .mp3 sur Carte SD ou encore MediaPlayer + Button en ListView pffff...
Voici le XML :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fond_voxov1"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="270dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner_pays"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/back_btn"
android:scrollbarSize="10dp"
android:popupBackground="@color/transparent1"
android:layout_weight="1"
android:entries="@array/pays"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/text_logo">
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="333dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/elapsedTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="0"
android:text="0:11" />
<TextView
android:id="@+id/remainingTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_weight="0"
android:text="-1:49" />
</LinearLayout>
<SeekBar
android:id="@+id/positionBar"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/playBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/play"
android:layout_marginTop="10dp"
android:onClick="playBtnClick" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="10dp"
android:src="@drawable/sound"
android:layout_marginLeft="20dp" />
<SeekBar
android:id="@+id/volumeBar"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginVertical="15dp"
android:max="100"
android:progress="50" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:src="@drawable/sound2"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout> |
Le MainActivity :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| package be.umwami.looply;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.File;
import java.io.FileReader;
import java.sql.ResultSet;
import static android.media.CamcorderProfile.get;
public class MainActivity extends AppCompatActivity {
//Spinner
Spinner spinner_pays;
int currentItem = 0;
//Eléments de l'activitée
Button playBtn;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_pays = (Spinner)findViewById(R.id.spinner_pays);
spinner_pays.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
???????????????????????????????????????????????????????????????????????????????????????????
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//Paramètres Musique
playBtn = (Button) findViewById(R.id.playBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel);
// Media Player
mp = MediaPlayer.create(this, R.raw.afghanistan);
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(1f, 1f);
totalTime = mp.getDuration();
// Position Bar
positionBar = (SeekBar) findViewById(R.id.positionBar);
positionBar.setMax(totalTime);
positionBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
positionBar.setProgress(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
// Volume Bar
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
volumeBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float volumeNum = progress / 100f;
mp.setVolume(volumeNum, volumeNum);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
// Thread (Update positionBar & timeLabel)
new Thread(new Runnable() {
@Override
public void run() {
while (mp != null) {
try {
Message msg = new Message();
msg.what = mp.getCurrentPosition();
handler.sendMessage(msg);
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}).start();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int currentPosition = msg.what;
// Update positionBar.
positionBar.setProgress(currentPosition);
// Update Labels.
String elapsedTime = createTimeLabel(currentPosition);
elapsedTimeLabel.setText(elapsedTime);
String remainingTime = createTimeLabel(totalTime-currentPosition);
remainingTimeLabel.setText("- " + remainingTime);
}
};
public String createTimeLabel(int time) {
String timeLabel = "";
int min = time / 1000 / 60;
int sec = time / 1000 % 60;
timeLabel = min + ":";
if (sec < 10) timeLabel += "0";
timeLabel += sec;
return timeLabel;
}
public void playBtnClick(View view) {
if (!mp.isPlaying()) {
// Stopping
mp.start();
playBtn.setBackgroundResource(R.drawable.stop);
} else {
// Playing
mp.pause();
playBtn.setBackgroundResource(R.drawable.play);
}
}
} |
Et enfin le strings.xml où vont se trouver les noms des choix (Spinner) :
Code:
1 2 3 4 5 6 7 8 9
| <resources>
<string name="app_name">looply</string>
<string-array name="pays">
<item>Afghanistan</item>
<item>Afrique du Sud</item>
<item>Albanie</item>
</string-array>
</resources> |
Un Giga merci d'avance à celle ou celui qui aura la gentillesse de m'aider ! ❤ ="D